question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[MaterialButtonToggleGroup] SelectionRequired attribute doesn't enforce checkedButtonId to be a valid id (returns -1)

See original GitHub issue

Description: The MaterialButtonToggleGroup, selectionRequired attribute doesn’t reflect the selected button when accessing toggleGroup.checkedButtonId.

When the button is pressed for the first time, checkedButtonId returns the correct value, when the same button is selected for the second time, checkedButtonId returns -1 instead of the selected button.

The current workaround that I’ve implemented based upon this StackOverflow answer (https://stackoverflow.com/questions/56164004/required-single-selection-on-materialbuttontogglegroup):

materialButtonToggleGroup.addOnButtonCheckedListener { group, checkedId, isChecked ->
    if (group.checkedButtonId == -1) group.check(checkedId)
}

After calling group.check(checkedId), the method MaterialButtonToggleGroup#checkedButtonId returns the correct button id.

It is worth noting that the checkedId that is returned from the above listener returns the correct value when selecting a button twice, it is only when accessing the state ( MaterialButtonToggleGroup#checkedButtonId) that -1 is returned.

I have set the singleSelection and selectionRequired to be true for this component.

Expected behavior: The method MaterialButtonToggleGroup#checkedButtonId should return the correctly selected button and not -1.

Android API version: 29

Material Library version: 1.2.0-alpha04

Device: Pixel 3

To help us triage faster, please check to make sure you are using the latest version of the library.

We also happily accept pull requests.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
raman-nbgcommented, May 12, 2020

@wcshi I have set singleSelection == true and I’m still getting -1.

The unit test in https://github.com/material-components/material-components-android/commit/e56b141dd5e63c4dcba2ed7ad06d30d3ee5c59cd only checks if checkedButtonId returns the correct ID after double tap, but not after the first tap.

        <com.google.android.material.button.MaterialButtonToggleGroup
            android:id="@+id/trainingWeightButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            app:singleSelection="true"
            app:selectionRequired="true"
            app:layout_constraintBottom_toTopOf="@id/trainingFormLabel"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <Button
                android:id="@+id/trainingWeightButtonYes"
                style="?attr/materialButtonOutlinedStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/yes" />

            <Button
                android:id="@+id/trainingWeightButtonNo"
                style="?attr/materialButtonOutlinedStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/no" />
        </com.google.android.material.button.MaterialButtonToggleGroup>
        val trainingWeightButtonYesId: Int = R.id.trainingWeightButtonYes
        val trainingWeightButtonNoId: Int = R.id.trainingWeightButtonNo
        val trainingWeightButtonGroup =
            view.findViewById<MaterialButtonToggleGroup>(R.id.trainingWeightButton)
        trainingWeightButtonGroup
            .buttonCheckedChanges()
            .onEach {
                // Workaround: On first click on button, the checkedButtonId is -1
                // https://github.com/material-components/material-components-android/issues/956
                when (trainingWeightButtonGroup.checkedButtonId) {
                    -1 -> trainingWeightButtonGroup.check(it.checkedId)
                    trainingWeightButtonYesId -> viewModel?.isTrainingWeightValid = true
                    trainingWeightButtonNoId -> viewModel?.isTrainingWeightValid = false
                }
            }
            .onEach { refreshFinishValidationButtonState() }
            .launchIn(lifecycleScope)
implementation "com.google.android.material:material:1.2.0-alpha06"
0reactions
wcshicommented, May 12, 2020

@raman-nbg I added an OnButtonCheckedListener in the MaterialButtonToggleGroupDemofragment, manually tested it and cannot reproduce your bug. Please provide a small sample app that reproduces the bug you are reporting

Read more comments on GitHub >

github_iconTop Results From Across the Web

Required single selection on MaterialButtonToggleGroup
You can define it in the layout using the app:selectionRequired attribute: <com.google.android.material.button.MaterialButtonToggleGroup ...
Read more >
How to Create a Button Group with MaterialButtonToggleGroup
The first involves setting the “app:selectionRequired” XML attribute on the MaterialButtonToggleGroup to “true”. See an example of a layout ...
Read more >
MaterialButtonToggleGroup not showing
MaterialButton manages its own background drawable, and setting a new background means MaterialButton can no longer guarantee that the new attributes it ...
Read more >
Android cc模块,Android模块化和可定制的 Material Design UI ...
MaterialButtonToggleGroup. Fixed SelectionRequired attribute doesn`t enforce checkedButtonId to be a valid id (returns -1) (e56b141).
Read more >
Modular and customizable Material Design UI components for ...
MaterialButtonToggleGroup. Fixed SelectionRequired attribute doesn`t enforce checkedButtonId to be a valid id (returns -1) (e56b141) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found