How to set border around Relative layout using custom xml layout file.
Use of border into Relative layout is very easy using external custom xml layout file with stroke attribute. With the use of border into layout it will make the whole application layout separate from phone screen by simply showing border between android mobile phone and activity. So here is the complete step by step tutorial for Add border to relativelayout in android using XML.
How to Add border to relativelayout in android using XML.
Create border_layout.xml file inside layout file.
Code for MainActivity.java file.
package com.relativelayoutborder_android_examples.com; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Code for activity_main.xml layout file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.relativelayoutborder_android_examples.com.MainActivity" android:background="@layout/border_layout" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Add border to relativelayout in android using XML" android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center" /> </RelativeLayout>
Code for border_layout.xml file.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="#fbfdfd"/> <stroke android:width="4dp" android:color="#0f03fe" ></stroke> </shape> </item> </selector>
Screenshots: