[MaterialButtonToggleGroup] SelectionRequired attribute doesn't enforce checkedButtonId to be a valid id (returns -1)
See original GitHub issueDescription: 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:
- Created 4 years ago
- Reactions:3
- Comments:6 (3 by maintainers)

Top Related StackOverflow Question
@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
checkedButtonIdreturns the correct ID after double tap, but not after the first tap.@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