I have a Material Button Toggle Group in a Fragment layout (with single selection and selection required), but on load of my app I want one of the button to already be selected. I thought adding this to my Main Activity onCreate() would have done the trick (the id of my button is paletteA):
paletteA.isPressed = true
but instead my app just crashes on loadtime and no errors are displayed in the Run window. Feels like something is not loading in time or is null, even though all of my Fragments have already been loaded in, etc, and this is the very last thing in my onCreate(). Any ideas?
The MaterialButtonToggleGroup widget has an XML attribute named app:checkedButton that can be used to load a button in the group as checked.
To set a particular button inside the MaterialButtonToggleGroup to load as checked by default, pass the id of the button to be checked as the value to the app:checkedButton attribute.
<com.google.android.material.button.MaterialButtonToggleGroup
...
app:singleSelection="true"
android:id="@+id/activity_main_togglebutton"
app:checkedButton="@id/activity_main_button2">
This can also be done programatically:
MaterialButtonToggleGroup group = findViewById(R.id.activity_main_togglebutton);
group.check(R.id.activity_main_button2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With