Home Tutorials Hints & Tips

Getting Started in Android Development

Part 15 - The Relative Layout

The relative layout is far more useful than the linear layout and likely to be the most used. I tend to use it far more than the linear layout simply because it gives the developer the highest degree of control and flexibility over the positioning of the views on the screen. As the name implies, the relative layout displays views in relative positions. The positions being relative to a view’s parent or siblings.

In the example for this tutorial all views appear in the top left hand corner of the parent until XML attributes are applied to the layout.

The relative layout used for this tutorial is shown below.

The TextView with “New Text 1” was given the XML attribute:


This literally positions the TextView centrally in the screen.

The TextView with “New Text 2” is given two attributes:


android:layout_above="@+id/textView" positions the TextView above the TextView with ID “@+id/textView” and android:layout_centerHorizontal="true" then aligns the TextView centrally so that is directly above TextView with ID “@+id/textView”. Otherwise, it would appear above “@+id/textView” and on the left hand side of the screen.

You should get the sense of how XML attributes are used to control the position of the view. It is worth taking some time to familiarize yourself with all of these attributes. They are described in the Android developer’s documentation here.

After this tutorial your RelativeActivity.java file should look similar to the one below:

Your ListviewActvity.java file should look similar to the one below:

Your Android Manifest file should look similar to the one below:

Download Download tutorial set 1

Privacy and Cookies Disclaimer Copyright
© 2015 - 2018 North Border Tech Training All rights reserved