Material Button Toggle Group single selection
UPDATE :
app:selectionRequired="true"
attribute is available as of version 1.2.0
Override the toggle()
method of the MaterialButton
class and use it instead of MaterialButton
import android.content.Context
import android.util.AttributeSet
import com.google.android.material.button.MaterialButton
class CustomMaterialToggleButton : MaterialButton {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
override fun toggle() {
if (!isChecked) {
super.toggle()
}
}
}
This will make sure that already checked button is not unchecked on single selection.
As of 1.2.0-alpha03 you can simply use the selectionRequired
option:
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggle_button_group"
app:singleSelection="true"
app:selectionRequired="true">
</com.google.android.material.button.MaterialButtonToggleGroup>