Linear Layout in Android

Android need layout to contain all view in proper format. Linear layout is one of them. As per name Linear layout is a view group that aligns all children in a single direction, vertically or horizontally. We have to specify layout direction “horizontal” or “vertical ” using attribute android:orientation . Look at the below image.


In vertical linear layout, all child views are arranged in vertical list. So a vertical list will only have one child per row, no matter how wide they are. In horizontal linear layout, all child views are arranged in one row horizontally and a horizontal list will only be one row high (the height of the tallest child, plus padding). A Linear Layout respects margins between children and the gravity (right, center, or left alignment) of each child.

Let’s see simple code for vertical orientation in linear layout.


and code for horizontal orientation linear layout.


Layout Weight in Linear Layout

Linear layout have many attribute, but here is one most useful is  android:layout_weight  for arrange equally or in required order. We will able to assign weight to individual children using this attribute. This attribute assigns an “importance” value to a child view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.

For example we have one layout and two child view. We give android:weightSum="1"  its mean our layout have 100% space in screen. Now in our first child button we give its   android:layout_weight="0.7"  and in second child button   android:layout_weight="0.3"  so its mean that first button take 70% of screen space in layout and second button take remaining space screen. See code and output below.



There is a many attribute of Linearlayout. See full description from developer site.

Now see next tutorial Relative Layout in Android.

(Visited 1,508 times, 1 visits today)