Android :: What is relative layout and how to create one?

In our previous article which was Android :: How to add views inside the Frame Layout? we discussed how we can add views inside the frame layout. Today’s topic details what a relative layout is and how we can create one?

In our previous article we explained that there are many views available in android and same can be used by you for your application. The same can be used to align the views in the user’s screen. Relative Layout is one of the views available in the android system which helps in making user interfaces.

What is Relative Layout?

Relative layout is the most used layout in android because in it you can align and allocate space on screen based on your UI design. Relative Layout is like using div in html. With relative layout you can add in views which are displayed in relative positions same like in div. Like you want a view on top and you want 2 views below that, the same can be achieved only through relative layout. In the relative layout you can position the views Below, Left, Right, Top of the other views or parent. Which means you can tell the android system that you want a particular view placed at the bottom of the parent view or layout.

How to create a relative layout?

In order to create a Frame Layout you have to follow the following steps:

Create or open an existing project and while in the project click on:

File->New-AndroidXML File option in the eclipse menu. You will see a window like below:

Relative Layout

Relative Layout

In the dialog choose Relative Layout and add in File Name to the same and click finish. After doing the same you will see the xml file see the xml file listed under res->layouts folder in eclipse with the following code:

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent” >
</RelativeLayout>

 

In the above code the <RelativeLayout> and </RelativeLayout> defines that the layout being used is a relative layout. The  xmlns:android=”http://schemas.android.com/apk/res/android” passed the name space of xml provided by android defined for Relative Layout.     android:layout_width=”match_parent” is used to tell what will the width be for the layout match_parent means width is equal to the width in which this layout is being used. Same way android:layout_height=”match_parent” defines what the height for the layout will be.

You can now add views inside the <RelativeLayout> and </RelativeLayout> tags according to needs. We will detail adding new views in the relative layout in our next article.

We will keep updating our Android Programming Tips section every 4 days, please subscribe to our feed and also share our articles on Facebook and Google Plus so other’s can benefit from our insight. Please feel free to send us your comments below, on how we can improve.

No comments yet.

Leave a Reply