How to create rounded corners Android Button

To create an Android button rounded corners, it's better to to create separate xml file just for button and save it to drawable folder and link it back to your layout xml file. In order to to change the corners of all sides of the button, we will only need one attribute in the button XML file.

 

This will apply all sides to 50% rounded corners.

android:radius="50dp"

 

To change one or two or three corners, you can add individual attributes as follows:-

android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp

 

So, your final code in the button_bg_login.xml will be look like this code and we have added button backround to blue color and 50% rounded corners:-

<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="#0071e3"/>
  <corners android:radius="50dp"/>
</shape>

 

Once you have created button_bg_login.xml with your desire rounded corners attributes, please link it into your layout xml file as like below.

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">
        <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:background="@drawable/button_bg_login"
        android:text="@string/Lbl_Login"
        android:textSize="15sp"
        android:textColor="@color/btn_white"
        android:textAppearance="?android:attr/textAppearanceSmall"
        style="?android:attr/borderlessButtonStyle"/>
</RelativeLayout>

 

If you get any help on this info, please help to share with others.

 


Published on: 11/12/2021, 2:56 PM