Required single selection on MaterialButtonToggleGroup
I

4

15

Is there an option to make the MaterialButtonToggleGroup have a required selected button when using app:singleSelection="true"?

When clicking to a different button works fine (all other buttons are deselected), but when you click the same (already selected) button it deselects it itself and I want to remain selected.

My example:

 <com.google.android.material.button.MaterialButtonToggleGroup
      android:layout_width="wrap"
      android:layout_height="wrap_content"
      app:singleSelection="true">

      <com.google.android.material.button.MaterialButton
        android:id="@+id/filterA"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A"/>

      <com.google.android.material.button.MaterialButton
        android:id="@+id/filterB"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="B"/>

      <com.google.android.material.button.MaterialButton
        android:id="@+id/filterC"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="C"/>

    </com.google.android.material.button.MaterialButtonToggleGroup>
Inconsolable answered 16/5, 2019 at 8:21 Comment(2)
I already filled a feature request, because I couldn't find a decent solution issuetracker.google.com/issues/132823510Inconsolable
The app:singleSelection="true" doesnt work for me. All buttons are still selectable at the same timeUnit
I
27

You can define it in the layout using the app:selectionRequired attribute:

    <com.google.android.material.button.MaterialButtonToggleGroup
        app:selectionRequired="true"
        app:checkedButton="@id/..."
        app:singleSelection="true">

You can also use the method setSelectionRequired:

buttonGroup.setSelectionRequired(true);

Note: This requires a minimum of version 1.2.0-alpha03

Incondensable answered 20/12, 2019 at 15:8 Comment(1)
Yes you are right, google team added this feature -> issuetracker.google.com/issues/132823510Inconsolable
D
4

I got this working with the following:

toggle_group.addOnButtonCheckedListener { group, checkedId, isChecked ->
    if (group.checkedButtonId == -1) group.check(checkedId)
}

If you have singleSelection enabled, the conditional will only evaluate to true when the user has clicked on the button which is already checked, making it so no button is checked. When this happens, we just need to check the button they unchecked.

Dunk answered 1/11, 2019 at 14:8 Comment(2)
how to map child button when checked?Izolaiztaccihuatl
Thanks for this. It's the simplest solution around, with no dependency on the alpha build.Prosperus
B
2

I also came across this issue and I found this is working with the app:singleSelection="true"

    String selectedValue = "Male";
    genderBtnToggle.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
        @Override
        public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
            MaterialButton btn = genderBtnToggle.findViewById(checkedId);
            if (!isChecked && btn.getText().toString().equals(selectedValue)) {
                genderBtnToggle.check(checkedId);
            }

            if (isChecked) {
                selectedValue = btn.getText().toString();
            }
        }
    });
Bellerophon answered 19/9, 2019 at 19:4 Comment(0)
B
0

I also came across this issue and will be waiting for a permanent fix from google. In the meantime, I did the following to make sure that at least one button is checked.

    final MaterialButtonToggleGroup tGroup = view.findViewById(R.id.toggleGroup);
    final MaterialButton breast = tGroup.findViewById(R.id.breast);
    final MaterialButton bottle = tGroup.findViewById(R.id.bottle);
    final MaterialButton solids = tGroup.findViewById(R.id.solids);

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tGroup.check(v.getId());
        }
    };

    breast.setOnClickListener(onClickListener);
    bottle.setOnClickListener(onClickListener);
    solids.setOnClickListener(onClickListener);

Hope this helps.

Ballocks answered 9/9, 2019 at 5:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.