Android RelativeLayout


Downlod RelativeLayout Sample

Android RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area

A RelativeLayout is a very powerful utility for designing a user interface because it can eliminate nested view groups and keep your layout hierarchy flat, which improves performance. If you find yourself using several nested LinearLayout groups, you may be able to replace them with a single RelativeLayout.

Frequently used Xml attributes of Android RelativeLayout

Xml attribute Description
android:layout_alignParentTop If "true", makes the top edge of this view match the top edge of the parent.
android:layout_centerVertical If "true", centers this child vertically within its parent
android:layout_below Positions the top edge of this view below the view specified with a resource ID.
android:layout_above Positions the bottom edge of this view above the given anchor view ID.
android:layout_toRightOf Positions the left edge of this view to the right of the view specified with a resource ID.
android:layout_toLeftOf Positions the right edge of this view to the left of the given anchor view ID.
android:layout_toStartOf Positions the end edge of this view to the start of the given anchor view ID.
android:layout_toEndOf Positions the start edge of this view to the end of the given anchor view ID.
android:layout_centerHorizontal If true, centers this child horizontally within its parent.
android:layout_centerInParent If true, centers this child horizontally and vertically within its parent.
android:layout_centerVertical If true, centers this child vertically within its parent.
android:layout_alignBottom Makes the bottom edge of this view match the bottom edge of the given anchor view ID.
android:layout_alignTop Makes the top edge of this view match the top edge of the given anchor view ID.
android:layout_alignEnd Makes the end edge of this view match the end edge of the given anchor view ID.
android:layout_alignLeft Makes the left edge of this view match the left edge of the given anchor view ID.
android:layout_alignRight Makes the right edge of this view match the right edge of the given anchor view ID.
android:layout_alignParentBottom If true, makes the bottom edge of this view match the bottom edge of the parent.
android:layout_alignParentEnd If true, makes the end edge of this view match the end edge of the parent.
android:layout_alignParentLeft If true, makes the left edge of this view match the left edge of the parent.
android:layout_alignParentRight If true, makes the right edge of this view match the right edge of the parent.
android:layout_alignParentStart If true, makes the start edge of this view match the start edge of the parent.
android:layout_alignParentTop If true, makes the top edge of this view match the top edge of the parent.

Layout file of above 2 example of RelativeLayout is given below:


Login screen using RelativeLayout

Now let's design a login screen using RelativeLayout. Create a Android Studio project with some name say LoginDemo under a package xyz.apnatutorial.relativelayout with a empty screen.

Here is the content of modified files.

MainActivity.java

activity_main.xml

Share & Like

Tutorial topics

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.