Monday 29 September 2014

How to add Rounded Corner borders to Android Layout


 How to add Rounded Corner borders to Android Layout


Create a xml file under your drawable folder with following code. (The name of the file I created is rounded_corner.xml)

rounded_corner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
    android:shape="rectangle">
    <!-- view background color -->
    <solid
        android:color="#a9c5ac" >
    </solid>
    <!-- view border color and width -->
    <stroke
        android:width="3dp"
        android:color="#1c1b20" >
    </stroke>
    <!-- If you want to add some padding -->
    <padding
        android:left="4dp"
        android:top="4dp"
        android:right="4dp"
        android:bottom="4dp"    >
    </padding>
    <!-- Here is the corner radius -->
    <corners
        android:radius="10dp"   >
    </corners>
</shape>

And keep this drawable as background for the view to which you want to keep rounded corner border. Let’s keep it for a LinearLayout

<LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_corner"
        android:layout_centerInParent="true">
         
        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Hi, This layout has rounded corner borders ..."
            android:gravity="center"
            android:padding="5dp"/>
         
    </LinearLayout>

No comments:

Donate