Add border to RelativeLayout(or any view) in Android

Screen Shot 2016-08-18 at 3.56.16 PM

relativelayout with border

 

To add border to your RelativeLayout, first you need to create a xml resource file in your drawable folder.

Then add the following code to that xml file.

<?xml version="1.0" encoding="utf-8"?>
<shape
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">

  <stroke
    android:width="5dp"
    android:color="#757575” /> 
</shape> 

The above code will create black border with 5dp thickness. You can change thickness using ‘width’ parameter and color using ‘color’ parameter in above code.

Sample code to apply above border to a RelativeLayout. Here we assume that the name of above file is border.xml in drawable folder.

<RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_margin="25dp"
  android:background="@drawable/border">
</RelativeLayout>