androidmaterial-designandroid-buttonmaterial-components-androidandroid-togglebutton

MaterialButtonToggleGroup child's MaterialButton layout_marginStart, layout_marginEnd not working


If i am trying to add margin with layout_marginStart or layout_marginEnd but there is no effect on UI. I am not sure why layout_marginStart, layout_marginEnd not working with MaterialButton when i add them as the child of MaterialButtonToggleGroup

enter image description here

 <com.google.android.material.button.MaterialButtonToggleGroup
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="@dimen/ten_dp"
                            app:singleSelection="true">

                            <com.google.android.material.button.MaterialButton
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginStart="@dimen/twentY"
                                app:icon="@drawable/ic_directions_walk_black_24dp" />

                            <com.google.android.material.button.MaterialButton
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginStart="@dimen/ten_dp"

                                app:icon="@drawable/ic_directions_car_black_24dp" />

                            <com.google.android.material.button.MaterialButton
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"

                                android:layout_marginStart="@dimen/ten_dp"
                                app:icon="@drawable/ic_directions_bus_black_24dp" />
                        </com.google.android.material.button.MaterialButtonToggleGroup>

Solution

  • After hours straggling, I finally found clean solution for this. we can easily use android:insetRight or android:insetLeft to add spacing to the Material buttons.

    Here is a code sample of 10dp (5dp + 5dp) space between each button:

    <com.google.android.material.button.MaterialButtonToggleGroup
      android:id="@+id/toggleGroup"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
    
      <com.google.android.material.button.MaterialButton
        android:id="@+id/button1"
        android:insetRight="5dp"
        ... />
    
      <com.google.android.material.button.MaterialButton
        android:id="@+id/button2"
        android:insetRight="5dp"
        android:insetLeft="5dp"
        ... />
    
      <com.google.android.material.button.MaterialButton
        android:id="@+id/button3"
        android:insetLeft="5dp"
        ... />
    
    </com.google.android.material.button.MaterialButtonToggleGroup>
    

    Hope this helps you too :)