Currently, the MaterialButtonToggleGroup component can be implemented as only allows one button to be checked by setSingleSelection(true). However, if the user taps again on the checked button it becomes unchecked.
I need a switchable behavior that at any instance only one button is checked and when a user taps again on the checked button nothing occurs. This behavior is more like a switch button.
I need this feature also. But I somehow managed to implement this behaviour by creating custom Buttons for MaterialButtonToggleGroup.
public class ToggleButton extends MaterialButton {
public ToggleButton(Context context) {
super(context);
}
public ToggleButton(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public ToggleButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void toggle() {
if(!isChecked())
super.toggle();
}
}
u vx9 9On Jul 14, 2019 8:12 PM, Vahid Mohammadi notifications@github.com wrote:Currently, the MaterialButtonToggleGroup component can be implemented as only allows one button to be checked by setSingleSelection(true). However, if the user taps again on the checked button it becomes unchecked.
I need a switchable behavior that at any instance only one button is checked and when a user taps again on the checked button nothing occurs. This behavior is more like a switch button.
鈥擸ou are receiving this because you are subscribed to this thread.Reply to this email directly, view it on GitHub, or mute the thread.
I need this feature also. But I somehow managed to implement this behaviour by creating custom
ButtonsforMaterialButtonToggleGroup.public class ToggleButton extends MaterialButton { public ToggleButton(Context context) { super(context); } public ToggleButton(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public ToggleButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public void toggle() { if(!isChecked()) super.toggle(); } }
I do something like you, but I think it's better to handle this behavior by MaterialButtonToggleGroup. I think it can be handled by CheckedStateTrackerclass inside the MaterialButtonToggleGroup class. Unfortunately, most of the methods are private and cannot be overridden.
I manage the case in the listener like that:
mbtg.addOnButtonCheckedListener { _, checkedId, isChecked ->
if (isChecked) {
// do stuff
} else {
if (-1 == radioState.checkedButtonId) radioState.check(R.id.defaultButton)
}
}
If we see this :- https://material.io/components/buttons/#toggle-button
In example where the alignment is to set Left, Center or Justify.
We would see, that this feature of selecting the one button at a time is really useful. We should provide this as an option
app:singleSelectionDefaultSelected="@+id/leftAlign"
What do you think @ymarian , does the above use case justify adding the following?
I am expecting this feature too.
In my case I am setting the default value at initializacion from sharedPreferences.
However, this feature becomes useful when the selected item is unchecked, which I expect to do nothing and keep the item checked so there is always one checked item.
Most helpful comment
I need this feature also. But I somehow managed to implement this behaviour by creating custom
ButtonsforMaterialButtonToggleGroup.