LinearLayout In Android-Tutorial And Example

We will talk about Linearlayout in android tutorial today.

If you are beginner and has not any basic idea about LinearLayout in android, then this tutorial perfectly suits you.

This example will teach you android layout design from scratch.

LinearLayout in Android is used to create Layout Design of every Page. Basically there are two types of Layout :

1.) LinearLayout and
2.) RelativeLayout

LinearLayout is used to design various elements like Button, EditText, TextView etc. in separate blockwise method.

Blobkwise method means these elements can not cover each other as they can cover each other in Relative Layout.

In short, LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally in Android App.

Orientation of LinearLayout In Android

LinearLayout has attribute named Orientation which is used to set children of LinearLayout in vertical or horizontal manner.

Let us take Two Buttons and set them with Linear Layout in Android Layout.

1.) Set Horizontally

Look below source code

Output of Above code will generate layout like below image.

LinearLayout orientation horizontal

As you can see that two button are set horizontally. In LinearLayout orientation is set as horizontal and two buttons are it’s children.

2.) Set Vertically

Look below source code

Output of Above code will generate layout like below image.

linearlayout vertical orientation
As you can see that two button are set vertically. In LinearLayout orientation is set as vertical and two buttons are it’s children.

You can set all layouts like Linearlayout, Relativelayout, Framelayout, TableLayout, GridLayout etc. as child of each other.

Important Attributes of LinearLayout

Different attributes are there to control LinearLayout as below.

1.) layout_width and layout_height

It will control the width and height of the linearlayout.

If you want to give full width and height then set layout_width and layout_height as  match_parent.

Set layout_width and layout_height as wrap_content if you want to have width of linearlayout as per requirements of children.

You can set layout_width in units also. For example 50dp, 40dp etc. The higher the dp number the more width is there.

2.) layout_weight

layput_weight attribute can be used in the children of linearlayout.

It will set height or width of children as per given weight.

If orientation of linearlayout is horizontal, then weight will decide width of children and if orientation is vertical then weight will set height of children.

Weight for width

Look at below code

Output of above code

linearlayout weight horizontal

Here orientation is horizontal so weight will decide button’s width.

weight is given value 2 for Button 1 and value 1 for button 2. So easily you can see that Button 1 has double width than Button 2.

Similarly, you can play with weight by giving different values to different children.

In this case we have to set both button’s layout_width as “0dp”

 Weight for height

Look at below code

Output of above code

linearlayout weight

Here orientation is vertical so height will decide button’s width.

weight is given value 2 for Button 1 and value 1 for button 2. So easily you can see that Button 1 has double height than Button 2.

Similarly, you can play with weight by giving different values to different children.

In this case we have to set both button’s layout_height as “0dp”

3.) gravity

gravity value (center, bottom, top, left, right) of linearlayout will set linearlayout’s child accordingly.

Consider below code

This is output

linearlayout gravity center
As you can see that value of gravity in linearlayout is center, so both buttons (Children of linearlayout) are at exact center in the whole linearlayout.

Now if you give value of gravity as “right”,

output will be like this

linearlayout gravity right
Both buttons are at the right side of the screen.

Notice one thing that buttons are at right but not in the center.

You can give multiple values to gravity like below

Output will be like below

linearlayout gravity center right
Now you can play around with various gravity values for android linearlayout for it’s children.

If you want to master in other Android basic concepts, then visit following tutorials

Android Basic Concepts to master Android App Development

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *