Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MaterialButtonToggleGroup's child with same width

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
like image 629
Sid Avatar asked Jun 01 '19 15:06

Sid


1 Answers

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>
like image 194
Michael Voropaiev Avatar answered Sep 19 '22 13:09

Michael Voropaiev