Development Tutorial on Working with Layouts in Android

Written by:  • Edited by: Simon Hill
Updated May 4, 2010
• Related Guides: Android | Google Android Programming | Google

When we are working with Android we inevitably have to deal with layouts and widgets. Let's have a look at 2 layouts: AbsoluteLayout and RelativeLayout. We will examine their differences and also how we can construct a solid graphical interface with these two elements.

 

Slide 1 of 4

Lately I have been working hard with the two layouts I will cover in this section, and I wanted to share my experience and knowledge about these two important and essential containers.

As I explained in another article: Create a user interface using XML: Layouts, we can work with different containers to hold our widgets. Depending on the container (layout) we use, the elements inside of it will be displayed in different ways. In this article we are going to focus on the Relative and the Absolute Layouts.

Both of them are very useful containers, however we have to know which to use in each case. With AbsoluteLayouts, the position of the elements is absolute, which means that the widget location is set according to an X,Y position on the screen, taking into account that the (0,0) position is the top-left corner of the screen.

On the other hand, widgets in RelativeLayout are positioned according to the parent or the position of other widget. In a colloquial way, it will be something like “Put this button below this image”.

Let's examine both layouts in a little bit more depth so we can see the attributes we can use for each one.

AbsoluteLayout

Slide 2 of 4

As I said before, the AbsoluteLayout main feature is that you can position the elements inside of it, with absolute coordinates. Absolute layouts are not very flexible and they are very hard to maintain: Imagine that you are working with a device (in my case I always work with my HTC Magic or HTC MyTouch in the USA), you create a screen with buttons, images and text and you position them using AbsoluteLayouts. You then create your application and you put it on the market. With time other Android mobiles appear, some with a screen larger than yours, some smaller. What will happen to your application on these phones? They will have problems displaying your screens because they will be dislocated. So take this into account, use them in the right way and in the right place, and combined with other Layout-elements.

To place an element inside an AbsoluteLayout, you have to put the following lines in it:

android:layout_x=”Xpx”

android:layout_y=”Ypx”

Example

<Button android:id="@+id/button"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:layout_x="115px"

android:layout_y="128px">

</Button>

It really is that easy.

RelativeLayout

Slide 3 of 4

RelativeLayouts are more complex to work with from my point of view, but they are well worth the effort. First we need to have a very clear idea of how the elements of the screen are going to be displayed.

Let's have a look at some of the most important attributes we can use in a widget to place it in a RelativeLayout.

android:layout_alignLeft = “@+id/widget”

android:layout_alignTop = “@+id/widget”

android:layout_alignBottom = “@+id/widget”

android:layout_alignRight = “@+id/widget”

Using the layout_align attribute, we are making the left/top/bottom/right edge of this view match the left/top/bottom/right edge of the given anchor view ID.

android:layout_alignParentLeft=”true|false”

android:layout_alignParentTop=”true|false”

android:layout_alignParentBottom=”true|false”

android:layout_alignParentRight=”true|false”

Using the layout_alignParent attributes, with the true or false value, we place the view at the left/top/bottom/right of its parent.

android:layout_below=”@+id/widget”

android:layout_above=”@+id/widget”

With this parameter we can place a view above or below another we specify in the value.

Hint!-> Views relationship are evaluated "in one pass", so, to assure your screen works how you want, if View A depends on view B, put B before A in the layout.

Conclusion

Slide 4 of 4

As we have seen, the RelativeLayout attributes are more complex, but they assure us the best way to display elements in the screen, without thinking of screen sizes. But AbsoluteLayouts are useful too! Don't stop using both of them.

In real applications, do we use just RelativeLayouts or AbsoluteLayouts? Of course not, they are always mixed and they live with LinearLayouts, TableLayouts etc...

Hopefully you now know a bit more about user interface design. Good luck and feel free to ask me questions.

Hint!-> Very important!! Some days ago, Google declared the AbsoluteLayout element is deprecated. It means that in future versions of the SDK it will not be available!!!! Better not to use it!


Comments

Showing all 13 comments
 
Bharath Mar 1, 2011 3:52 PM
Alternative to RelativeLayout
Thanks for the article.
I've already finished doing an application in AbsoluteLayout. Now google has announced that AbsoluteLayout has been deprecated. Which is the best alternative for AbsoluteLayout.

Also I need the application to work on all resolutions (320x240 to 1280x800).

And is there a way to make my application look differently on different resolution. I mean, If for example, the 320x240 resolution layout has just a button, the 1280x800 resolution layout has a button + one imageview. Is that possible?
Vivek Tamrakar Feb 2, 2011 1:32 AM
Text Selection in WebView
Hi,
Iam facing one problem while working ..
Actually am able to select text from WebView (using Target 2.3). But when am delpoying to Samsung Tab 2.2 am not able to select text from webview.
How can I solve this issue
Vivek Tamrakar Jan 27, 2011 5:34 AM
Highlighting text in webview(Dynamically)
Am proceeding to create ebook reader in android.In this app, I need to give the functionality of highlighting text inside webview.
How can I make approach for this ???(Device is Android 2.3)
Jbeerdev Jan 9, 2011 4:01 PM
RE: Development Tutorial on Working with Layouts in Android
Hi vivek tamrakar.

You can create dinamic layouts using "java" code. For example

LinearLayout mLayout = new LinearLayout()
Button mButton = new Button();

mLayout.add(mButton);

This "pseudo-code", but just to get the idea.

LinearLayout, RelativeLayout, TextView... etc etc, are Java classes too.
vivek tamrakar Jan 6, 2011 4:51 AM
How can we use dynamic programming in Android using xml files
It's fantastic working with xml files but my problem is how can i start my programming when I suppose to code for generic code or dynamic programming...
Please send me solution
arjun Nov 25, 2010 8:22 AM
Thanks
yours articles are very useful who enter in android world,
parag Sep 21, 2010 8:21 AM
Thanks
U r article really help full for me!
Jbeerdev May 6, 2010 2:27 AM
RE: Development Tutorial on Working with Layouts in Android
Hi Jessica.
Sorry for that, I have to clarify some concepts. "align" is the short of alignment. You can have a layout or a view with a center alignment for example
Narendar May 6, 2010 12:59 AM
Layout
Very Nice Guidelines for Layouts.
Jessica Apr 28, 2010 4:01 PM
thank you
Thank you for this helpful article! There were a couple typos that momentarily confused me (eg: figuring out what aling) means, that could be helpful to go back and correct, but overall, great information! :)
Jbeerdev Sep 14, 2009 10:40 AM
Jbeer
Here it is:

http://www.brighthub.com/mobile/google-android/articles/48845.aspx

An example and some explanation of how creating layouts using Java code.
Jbeerdev Sep 14, 2009 2:23 AM
how can i show the UI based on relative Layout
Hi Arun Gupta!

Do you want to know creating layouts using Java? Ok, that will be the title of my new article. I have to warn you, that its easier to work with XML than with java code, but sometimes, in some situations, we need to create the UI in java.

So, stay close, in few hours you will have the article!
Arun Gupta Sep 8, 2009 6:13 AM
how can i show the UI based on relative Layout
how can i show the UI based on relative Layout
using java code not through an xml file.
 
blog comments powered by Disqus
Email to a friend