Sunday, 7 May 2023

Custom shaped button inside of MaterialButtonToggleGroup

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 :)



from Custom shaped button inside of MaterialButtonToggleGroup

No comments:

Post a Comment