I'm trying to following the documentation from Material 3 for checkboxes: https://github.com/material-components/material-components-android/blob/master/docs/components/Checkbox.md
At the very beginning it states:
Note:
<CheckBox>is auto-inflated as<com.google.android.material.button.MaterialCheckBox>viaMaterialComponentsViewInflaterwhen using aTheme.Material3.*theme.
This does not seem to work for me, can someone please explain why the checkbox is not auto-inflated as a MaterialCheckBox?
Both theme files are using: Theme.Material3.*
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.ParentChildCheckboxes" parent="Theme.Material3.Light.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.ParentChildCheckboxes" parent="Theme.Material3.Dark.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_200</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<CheckBox
android:id="@+id/checkbox_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/label_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/checkbox_child_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="@string/label_child_1"
app:layout_constraintStart_toStartOf="@+id/checkbox_parent"
app:layout_constraintTop_toBottomOf="@+id/checkbox_parent" />
<CheckBox
android:id="@+id/checkbox_child_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="@string/label_child_2"
app:layout_constraintStart_toStartOf="@+id/checkbox_parent"
app:layout_constraintTop_toBottomOf="@+id/checkbox_child_1" />
<CheckBox
android:id="@+id/checkbox_child_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="@string/label_child_3"
app:layout_constraintStart_toStartOf="@+id/checkbox_parent"
app:layout_constraintTop_toBottomOf="@+id/checkbox_child_2" />
<CheckBox
android:id="@+id/checkbox_child_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="@string/label_child_4"
app:layout_constraintStart_toStartOf="@+id/checkbox_parent"
app:layout_constraintTop_toBottomOf="@+id/checkbox_child_3" />
</androidx.constraintlayout.widget.ConstraintLayout>
Activity file:
package com.example.parentchildcheckboxes
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.parentchildcheckboxes.databinding.ActivityMainBinding
import com.google.android.material.checkbox.MaterialCheckBox.STATE_INDETERMINATE
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val checkBoxParent = binding.checkboxParent
checkBoxParent.checkedState = STATE_INDETERMINATE
}
}
I cant use .checkedState on checkBoxParent because it's not treated as a MaterialCheckBox:
When I hover on the val checkBoxParent , I see: val checkBoxParent: CheckBox and expected: val checkBoxParent: MaterialCheckBox.
When I change: val checkBoxParent = binding.checkboxParent to: val checkBoxParent = binding.checkboxParent as MaterialCheckBox:
val checkBoxParent = binding.checkboxParent as MaterialCheckBox
checkBoxParent.checkedState = STATE_INDETERMINATE
it works fine.
Why is the checkbox not auto-inflated as a MaterialCheckBox? as stated in the documentation?
Changing the
<CheckBox>tag to:<com.google.android.material.checkbox.MaterialCheckBox>solved the issue for me: