androidandroid-layoutandroid-linearlayout

How can I create a border around an Android LinearLayout?


I have one big layout, and one smaller layout inside of it.

How do I create a line border around the small layout?


Solution

  • Sure. You can add a border to any layout you want. Basically, you need to create a custom drawable and add it as a background to your layout. example:

    Create a file called customborder.xml in your drawable folder:

    <?xml version="1.0" encoding="UTF-8"?>
     <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
       <corners android:radius="20dp"/> 
       <padding android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp"/>
       <stroke android:width="1dp" android:color="#CCCCCC"/>
     </shape>
    

    Now apply it as a background to your smaller layout:

    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/customborder">
    

    That should do the trick.

    Also see: