Search code examples
androidmaterial-designmaterialbutton

How to add stroke to just bottom side of Material button?


I want to achieve this

I want to achieve this button.

I Tried to add layer-list drawable background.

  • Doesn't work in case of MaterialButton.
  • In case of normal button it also doesn't work because My AppTheme is Material Theme. So normal Button also behaves like Material Button.

Solution

  • You can't use MaterialButton for a custom drawable Background like this, said in it's documentation:

    Do not use the android:background attribute. MaterialButton manages its own background drawable, and setting a new background means MaterialButton can no longer guarantee that the new attributes it introduces will function properly. If the default background is changed, MaterialButton cannot guarantee well-defined behavior.


    Anyways, I designed the custom Background drawable for a normal Androidx Button for you.

    In your activity_main.xml you define the Button with the attribute android:background="@drawable/custom_shape" you will insert the drawable we define later:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:layout_margin="10dp">
    
        <androidx.appcompat.widget.AppCompatButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Collect"
            android:background="@drawable/custom_shape"
            android:textColor="#ffffff"/>
    
    </RelativeLayout>
    

    Here is the mentioned drawable called custom_shape.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item>
            <shape android:shape="rectangle">
                <solid android:color="#ffc9a1"/>
                <corners android:radius="10dp"/>
            </shape>
        </item>
    
        <item android:bottom="5dp">
            <shape>
                <solid android:color="#f7791e" />
                <corners android:radius="10dp"/>
            </shape>
        </item>
    </layer-list>
    

    The final result look pretty close to your wished Button:

    Button with custom Background