I am using MaterialButtonToggleGroup
with single selection (only one button checked at a time). How to check if none of the buttons is checked?
toggleGroup?.addOnButtonCheckedListener { group, checkedId, isChecked ->
if (isChecked) {
when (checkedId) {
R.id.first_materialButton -> {
// do something when selected
}
R.id.second_materialButton -> {
// do something when selected
}
}
}
}
The solution would be to get the checkedButtonId
from the group on the else branch for isChecked
, and if it the value is -1, then no button is selected.
toggleGroup?.addOnButtonCheckedListener { group, checkedId, isChecked ->
if (isChecked) {
when (checkedId) {
R.id.first_materialButton -> {
// do something when selected
}
R.id.second_materialButton -> {
// do something when selected
}
}
} else {
if (group.checkedButtonId == View.NO_ID) {
// do something when nothing selected
}
}
}