Create rounded background as border radius in android layout

Like Html and CSS you can create rounded corner background for layout and button in android. The way of creating a rounded corner in Android is a bit different than Html and CSS. To create a rounded corner in android you just need to create an XML file in the drawable directory.

You can also set a different style for different states in that XML file. Let take an example to elaborate on this.

First, create file button.xml in drawable folder. (app/src/main/res/drawable/button.xml)

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false" >
        <shape>
            <solid
                android:color="#6bcdfd" />
            <stroke
                android:width="1px"
                android:color="#45a9da" />
            <corners android:radius="20dp" />
        </shape>
    </item>
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#ba78e7" />
            <stroke
                android:width="1px"
                android:color="#ad60e1" />
            <corners android:radius="20dp" />
        </shape>
    </item>
</selector>

You can also set a gradient color in the background. Add the gradient tag in place of solid.

<gradient android:startColor="#f6fafd"
   android:endColor="#ebf0fd"
   android:angle="270" />

Now use this XML file as a background of your layout or button.

<Button
     android:layout_width="140dp"
     android:layout_height="40dp"
     android:background="@drawable/button_sample"
     android:textColor="#ffffff"
     android:text="BUTTON"
/>

All is done, Run your app.

Keywords: