Relative Layout in Android

There is a many way to arrange our UI component of application on screen in android. We have learn about Linear Layout in last tutorial. Now we learn about most useful after linear layout is Relative Layout.

As per name  this layout is group of child view which is arranged with positions relation of each other. The positions of each child view is specified as relative to other view or directly to relative layout. If you not specify any position of view, its take default as top-left of parent relative layout. A relative layout is very useful in making of amazing UI in application. If you use nested of linear layout to manage UI, then you can able to use Relative Layout instead of that.

In Relative layout we can use right, left, top, bottom, above and below for arrange view to  positions relative each other. See all attribute of Relative layout is here.

See simple explanation of relative layout below.

Let me explain simple example, we have two buttons button1 and button2 and we have to set position of button1 in center parent, where button2 is below to button1. So to position center we use
android:layout_centerInParent="true" is set button1 to center in parent. Now to set button2  below button1 we set android:layout_below="@+id/button1" and its set position relative to below button1. There is some usefull attributes of Relative Layout below.

  • layout_alignParentTop : If “true” then set the view on top of relative layout.
  • layout_alignParentBottom : If “true” then set the view on bottom of relative layout.
  • layout_alignParentLeft : If “true” then set the view on left of relative layout.
  • layout_alignParentRight : If “true” then set the view on right of relative layout.
  • layout_centerInParent : If “true” then set the view on center of relative layout.
  • layout_centerHorizontal : If “true” then set the view on center in horizontally of relative layout.
  • layout_above : set position of view on the top of give view’s resource ID.
  • layout_below : set position of view on the bottom of give view’s resource ID.
  • layout_toLeftOf : set position of view to left of give view’s resource ID.
  • layout_toRightOf : set position of view to right of give view’s resource ID.

Now its time to write code. Open you xml file and write below code.

Boom…you will see output like…


See more information of Relative Layout from developer website.

Now see next tutorial Frame Layout in Android.

(Visited 1,149 times, 1 visits today)