LinearLayout
LinearLayout organizes elements along a single line. You specify whether that line is verticle or horizontal using android:orientation. Here is a sample Layout XML using LinearLayout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/backbutton" android:text="Back" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="First Name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:width="100px" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:text="Last Name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:width="100px" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
Here is a screenshot of the result of the above XML.

Here is a screenshot of the same XML except that the android:orientation has been changed to horizontal.

You might note that the EditText field at the end of the line has had its width reduced in order to fit. Android will try to make adjustments when necessary to fit items on screen. The last page of this tutorial will cover one method to help deal with this.
I mentioned on the first page that Layouts can be nested. LinearLayout is frequently nested, with horizontal and vertical layouts mixed. Here is an example of this.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/backbutton" android:text="Back" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:text="First Name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:width="100px" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:text="Last Name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:width="100px" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
As you can see we have a Vertical LinearLayout whose children are a button, and two horizontal LinearLayouts. Each horizontal LinearLayout has two child controls. You should note that in the child LinearLayout elements I used android:layout_height=”wrap_content” instead of fill_parent. If I had used fill_parent the first name TextView and EditView would have taken all of the available space on the screen, and the Last Name would not have been displayed. Here is what this XML does display.

Nested Layouts do not have to be of one type. I could, for example, have a LinearLayout as one of the children in a FrameLayout.
Next: RelativeLayout
{ 49 } Comments
You have a very nice set of tutorials here! I am curious as to handling rotation of the physical device, such as the user effectively switching from portrait to landscape viewing of an application. Are there callbacks that are given, or queries that can be performed by us, as the developer, or is this automatic?
Hi,
This was a great tutorial and an easy read. I understand alot more on how google’s xml display/gui works.
Thanks a bundle!
Small typo here — the original code sample says ‘android:orientation=”horizontal”‘, but should say ‘android:orientation=”vertical”‘ instead.
Helped me alot……as I am beginner as a professional….
I really liked your tutorial, and how easy it was to understand everything by explanation, graphic content and code. This helps tremendously. thanks.
Nice one..
Nice One
I am learning Android and I hope this tutorial helps me.
This tutorial is really helpful and easy to understand.
can you send me downloadable pdf file of this tutorial.
Thanks! RelativeLayout very very good.
Hi,
Your goal is awesome. I read some articles and I’m sure you and your goal are the best.
I’ll be happy if I can help you, although I’m beginner.
Warm Regards
Hesam
very nice& very useful to the begginers….. thanks lot….
Great tutorial man. Thanks for sharing this information with us, i’m so glad becouse i found this.
Your post is very good tutorial.
so, I would like to ask you some question.
I have to tell you before . I don’t know that is correct or not.
Could I translate your post in thai language ?
of cause , I will tell people that post where it come from ( credit ).
if no, it’s ok for me. I still follow your blog : )
thank you
Excellent article! Thanks a lot!
simple
easy to understand for first tutorial
thanx!
there is much good information i like it
Very nice!
Its always good to understand things with examples.
Hi,
Nice tutorial.
I want one label, one edittext and one button horizontally.
My requirement is on changing the screen size, my label and button size should be fix but my edittext should be flexible.
I mean that if we increase window size then only the edittext box size should change to adjust the screen size.
please help.
Thanks.
thanks… its helpful to me in my android appp
good explanation so thanks dear and try to provide the pdf or ppt
I wonder why the button is located in the first column and the text view of the “first name” and “last name” starts on the second column. It would be nice to explain a solution for that.
Thanks for the tutorial.
Sorry, I thought my comment is associated to the TableLayout page only. This is in reference to the TableLayout.
”I wonder why the button is located in the first column and the text view of the “first name” and “last name” starts on the second column. It would be nice to explain a solution for that.”
Also,there was another article that said that embedded LinearLayout is more cpu extensive than a single RelativeLayout. Can you comment on the efficiency of different layouts. It looks like TableLayout is more straight forward than RelativeLayout but does it cost more than LinearLayout?
thnx……….
good e,ample
Dite mois si sa marche
thanks buddy. it’s nice article about android. layout layout n layout
if you need any ebook about android. please visit my blog http://free-ebook-collections.co.nr/ thanks
Very good.i like it
hi, can we use this absolutelayout same as HTML CSS like just using x and y. can we use right/left and bottom/top.
thanks.
Great! You resolve my problem. Thanks.
Thanks for the lucid explanation! It is very helpful for a beginner like me .
Thanks in advance for this knowledge
Thanks!!
im new to android and i made a layout but it doesnt show the imagebuttons and text could you tell me why? Here it is:
Thanks for very clear explanation.
Muito bom ^.^
seria legal ter o código para fazer em java também
Hello – I can’t understand why when I do it in emulator – every thing is on the top of the screen???? Please tell me – why on EARTH(!) my two buttons (“add” and “print”) appears always on the top of the screen overlapping gallery view?? Why this has to be so painful???? I just set the gravity to “TOP” for gallery and to “BOTTOM” for those two buttons in tablelayout, as you can see in my xml code. what the heck is that???? Below is my XML:
nice
Great stuff it help you with a good starting point UI layouts…thanks much!!
Great tutorial! Thanks for sharing your knowledge!
Cheers
riper
I want to know how to increase font size in Eclipse IDE
hello,
thanks for giving such a great infromation..
but whant to know which layour would bebeat for mt application taht it supports all my device sizes..
Thanks,
Ayrina
I found this site very helpful…. very concise yet informative
Thanks man.. this helps a lot
Thank you very much,
this makes android layouting much easier,
very nice
Absolutely, feel free to translate it as long as you provide proper credit.
Very nice tutorial i realy learn more from it
Hey Hi!!! You have given really nice exaple and so nicely explained everything.Just wanted to add that in linear layout output of horizontal orientation is given as output of vertical one’s. And thanks its was really helping to understand layouts..
This one is great Keep going on!!
{ 2 } Trackbacks
[...] learn Android you can find also a great resources for how to use Android AbsoluteLayout, FrameLayout, [...]
[...] AbsoluteLayout FrameLayout LinearLayout RelativeLayout TableLayout [...]
Post a Comment