MaterialButtonToggleGroup's child with same width
Asked Answered
D

3

10

I want to use MaterialButtonToggleGroup with 2 MaterialButton. I want to display them with same width inside MaterialButtonToggleGroup

If I use match_parent then only one button will be displayed and if I use wrap_content they displayed in the middle.

Below is the current output

MaterialButtonToggleGroup

Below is my code :-

<com.google.android.material.button.MaterialButtonToggleGroup
        android:id="@+id/toggle_button_group"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:gravity="center"
        app:checkedButton="@id/btn_login"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:singleSelection="true">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btn_login"
            style="?attr/materialButtonOutlinedStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Login" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btn_register"
            style="?attr/materialButtonOutlinedStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Register" />

    </com.google.android.material.button.MaterialButtonToggleGroup>```

Please help. Thanks
Diggs answered 1/6, 2019 at 15:16 Comment(0)
B
18

Just faced an issue myself, starting version 1.1.0-beta01, MaterialButtonToggleGroup is extending LinearLayout so equal width for buttons can be set like this (assuming default android:orientation="horizontal"):

<com.google.android.material.button.MaterialButtonToggleGroup
    android:id="@+id/toggleButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parrent"
            android:text="Button 1"
            style="?attr/materialButtonOutlinedStyle"
            />
        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parrent"
            android:text="Button 2"
            style="?attr/materialButtonOutlinedStyle"
            />
</com.google.android.material.button.MaterialButtonToggleGroup>
Boley answered 22/5, 2020 at 7:57 Comment(0)
W
1

I couldn't find any option for auto width for MaterialButtonToggleGroup button group. You can use the custom value.

<com.google.android.material.button.MaterialButtonToggl
    android:layout_margin="8dp"
    android:id="@+id/ToggleGroupBrother"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:singleSelection="true"
    app:checkedButton="@+id/ToggleButton0">
    <com.google.android.material.button.MaterialButton
        android:id="@+id/ToggleButton0"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="0"
        app:backgroundTint="@color/white"
        style="@style/MaterialButtonStyle"/>
    <com.google.android.material.button.MaterialButton
        android:id="@+id/ToggleButton1"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="1"
        app:backgroundTint="@color/white"
        style="@style/MaterialButtonStyle"/>
    <com.google.android.material.button.MaterialButton
        android:id="@+id/ToggleButton2"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="2"
        app:backgroundTint="@color/white"
        style="@style/MaterialButtonStyle"/>
Whitley answered 18/9, 2019 at 12:13 Comment(0)
P
0

You can set the width of both Buttons to the maximum drawn width like this :

        materialButton2.post(new Runnable() {
                    @Override
                    public void run() {
                        int maxWidth = Math.max(materialButton1.getWidth(), materialButton2.getWidth());
                        materialButton1.setWidth(maxWidth);
                        materialButton2.setWidth(maxWidth);
                    }
                });
Perspiratory answered 16/7, 2020 at 20:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.