Tuesday, February 21, 2012

Creating a simple border with rounded corners for a layout or view in Android?



Hi all, just thought I'd post this simple snippet that I've just found.
This allows you to simply add a border ( even with rounded corners!) to a layout or view.
All you need to do is create a new xml file somewhere in /res/drawable called 'the_border.xml'
with this or similar as the contents:

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <stroke android:width="1dp" android:color="#FFFFFF" /> 
    <padding android:left="7dp" android:top="13dp" 
            android:right="7dp" android:bottom="17dp" /> 
    <corners android:radius="4dp" /> 
</shape> 
You can then reference this as the background of an item in your layout like this (in xml)
android:background="@drawable/the_border"
or like this in code:
llErrorMessage.setBackgroundResource(R.drawable.the_border);
Nice eh?


1 comment:

  1. my dialog's background color bleeds out at the corner. its just drawing the border white color in round corner and the dialog box is still solid square.

    ReplyDelete