1

I have created selector buttons using MaterialButtonToggleGroup.

I want to add padding between stroke and drawable.

This is what I’m getting: enter image description here

This is what I want:

enter image description here

As you can see in the above image there is padding between stroke and selector drawable.

Here is the XML code:

<com.google.android.material.button.MaterialButtonToggleGroup
        android:id="@+id/toggleContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"
        app:checkedButton="@id/btnOutline"
        android:paddingVertical="4dp"
        app:selectionRequired="true"
        app:singleSelection="true">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btnOutline"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="@drawable/button_selector"
            android:text="@string/outline"
            android:textSize="15sp"
            android:drawablePadding="2dp"
            android:textAllCaps="false"
            android:textColor="@drawable/selector_color"
            app:strokeColor="@color/selector_btn_toggle" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btnMain"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="@drawable/button_selector"
            android:text="@string/main"
            android:textAllCaps="false"
            android:drawablePadding="2dp"
            android:textSize="15sp"
            android:textColor="@drawable/selector_color"
            app:strokeColor="@color/selector_btn_toggle" />

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

As you can see in the code I'm using android:drawablePadding="2dp", but this is not working. I'm using a backgroundTint to set the selector drawable.

Drawable button_selector code:

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/selector_btn_toggle" android:state_checked="true"/>
</selector>
Parag Rane
  • 179
  • 4
  • 15

1 Answers1

0

Create a layer-list, to add item padding, drawablepPadding works for compound drawable not for background, create below

selected_background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="#FFFFFF"/>
        </shape>
    </item>
    <item android:top="3dp" android:bottom="3dp" android:right="3dp" android:left="3dp">
        <shape>
            <solid android:color="@color/selector_btn_toggle"/>
        </shape>
    </item>
</layer-list>

then set it your button_selectpr

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/selected_background" android:state_checked="true"/>
</selector>
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37