1

I'm trying to create a group for buttons (not radio buttons!), and I've stumbled upon: MaterialButtonToggleGroup

the result I'm trying to achieve should look something like:

desired UI result

however while trying to implement it I've ran into this problem, which disallowed custom shapes in MaterialButtonGroups, which I managed to solve (It does not crash), but It still seems to simply not care about the custom drawable (the button stays rectangular)

small sample:

fragment:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FirstFragment">

    <com.google.android.material.button.MaterialButtonToggleGroup
        android:id="@+id/my_toggle_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:selectionRequired="false"
        app:singleSelection="true">

        <Button
            android:id="@+id/c_button"
            style="@style/roundSizeButton"
            android:layout_width="50dp"
            android:layout_height="50dp"
            app:backgroundTint="@color/white"
            android:background="@drawable/size_round_button_selected"
            android:layout_weight="1"
            android:text="C" />

        <Button
            android:id="@+id/d_button"
            style="?attr/materialIconButtonOutlinedStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="D" />

        <Button
            android:id="@+id/e_button"
            style="?attr/materialIconButtonOutlinedStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="E" />


    </com.google.android.material.button.MaterialButtonToggleGroup>
</androidx.constraintlayout.widget.ConstraintLayout>

size_round_button_selected:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="1000dp" />
    <solid android:color="@color/white" />
    <stroke
        android:width="1dp"
        android:color="@color/black" />
</shape>

Any ideas how can I make it work? I know implementing onClick for the buttons to handle it myself would be possible, but this seems cleaner (don't have to set 4 onClicks with the same logic)

Any tips / gists / hints will be appreciated, thanks in advance :)

Zain
  • 37,492
  • 7
  • 60
  • 84
JustSightseeing
  • 1,460
  • 3
  • 17
  • 37
  • 1
    *Do not use the android:background attribute. MaterialButton manages its own background drawable, and setting a new background means MaterialButton can no longer guarantee that the new attributes it introduces will function properly. If the default background is changed, MaterialButton cannot guarantee well-defined behavior.* [Documentation link](https://developer.android.com/reference/com/google/android/material/button/MaterialButton) – Zain May 07 '23 at 20:44
  • 1
    Huh, I guess reading the documentation would save me the time... Surprising, could you post this as an answer, so I may mark it as the correct solution? – JustSightseeing May 08 '23 at 08:58

1 Answers1

1

The documentation doesn't recommend using android:background attribute with MaterialButton because it already has a default background:

Do not use the android:background attribute. MaterialButton manages its own background drawable, and setting a new background means MaterialButton can no longer guarantee that the new attributes it introduces will function properly. If the default background is changed, MaterialButton cannot guarantee well-defined behavior.

Although you're using a normal Button, it's automatically converted to a MaterialButton when it comes to use material components.

There is a workaround to put app:backgroundTint="@null" along with android:background, that probably can work on a standalone MaterilaButton taking the previous documentation quote into consideration. But adding that MaterilaButton into a MaterialButtonToggleGroup will raise IllegalStateException at runtime:

java.lang.IllegalStateException: Attempted to get ShapeAppearanceModel from a MaterialButton which has an overwritten background.

And unfortunately the normal android.widget.Button doesn't comply with the MaterialButtonToggleGroup.

So, accordingly this need to performed with the traditional RadioGroup.

Zain
  • 37,492
  • 7
  • 60
  • 84