Material3 checkbox indeterminate state not working

32 views Asked by At

I'm using ExpandableListView to display a list with children. I need to implement multi-select (one child, all children, none) elements with a corresponding checkbox state of the parent element. For this I use the material3 library, which has a checkbox with three states: checked, unchecked, indeterminate.

enter image description here

    <com.google.android.material.checkbox.MaterialCheckBox
        android:id="@+id/cb_report_parent_category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:focusable="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

After each child element selection, I check to see if all children are checked (checked), any (indeterminate) or none (unchecked) and set the parent checkbox to the appropriate state (with notifyDataSetChange()):

    override fun getGroupView(
        groupPosition: Int,
        isExpanded: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View {
        ...

        with(binding) {
            myParentCheckBox.checkedState = when {
                category.children?.all { it in selectedCategory } == true -> STATE_CHECKED
                category.children?.any { it in selectedCategory } == true -> STATE_INDETERMINATE
                category.children?.none { it in selectedCategory } == true -> STATE_UNCHECKED
                else -> STATE_UNCHECKED
            }
            Timber.d("category ${category.name}, state: ${cbReportParentCategory.checkedState}")
            myParentCheckBox.setOnCheckedChangeListener { _, isChecked ->
                selectFullCategory(isChecked, groupPosition)
            }

            ...
        }
        return binding.root
    }

In the checked/unchecked states, everything is fine with the parent checkbox (when I switch manually or does it also update when selecting all/no elements), but in the indeterminate state it does not display anything. Visually it is in unchecked, but when output to the log its checkedState is equal to 2 (which corresponds to the indeterminate I set):

category Item 25, state: 2

What could be the problem?

0

There are 0 answers