Linear Layout Android | Android Development | Android Tutorial


So here is your first step.

Just take deep breath and take a step toward android learning. Yes, It will be like magic when you will get your first app in your hand. We promise that your happiness will be on peak. It will be the result of you hard work and focused work. So make this dream true. Start Now !!!



What is linear layout ?

It is the first thing which your mind adopts that, what is it ? What is linear layout ? Is it some kind of design or structure. So hopefully somehow you are correct. Linear layout is used for designing purpose and it contains it's particular structure to build. It is like a stack or a drawer in your cupboard but remember Linear layout keeps thing like ladder (only one by one). It doesn't allow views to overlap each other. It is a view group that aligns it's all children in a single direction, it can be vertically or horizontally. You can specify the layout direction with the android:orientation attribute.



Step 1 : Open Android Studio and right click on layout folder under project tab at left side. Click New >> Layout resource file.



Mayadi - A Knowledge Source
Create Layout



Step 2 : After clicking on layout resource file, A dialog will appear. Name the file and select linear layout in root element.


Mayadi - A Knowledge Source
Name layout file


Step 3 : Some xml tags will be there in linear layout file.





Mayadi - A Knowledge Source
Set linear height width



Step 4 : layout_width and layout_height are used to set linear for full screen of mobile phone.

Mayadi - A Knowledge Source
layout_width and layout_height


Step 4 : Now set orientation using android:orientation attribute.

Mayadi - A Knowledge Source
linear layout orientation

Step 5 : Set background using android:backgroud attribute.

Mayadi - A Knowledge Source
Background

Step 6 : Now the term gravity comes into play. There are two attributes related to gravity.

1. android:layout_gravity
2. android:gravity



Main difference between android:layout_gravity and android:gravity is that "android:layout_gravity" related to parent and "android:gravity" is related to child. Yes, I know it's little bit difficult to understand but don't worry we explain to you in a very layman way.

See this below image carefully.
Mayadi - A Knowledge Source
Understand gravity

Have you got any idea about gravity after seeing above image ? Exactly, you picked it up right !! Here you can see if we use "android:layout_gravity" then it effects parent view. same as if you use "android:gravity"  then it effects child view. For example : if you want to move text than "android:gravity" will work or if you want to move whole child view than "android:layout_gravity" comes into the play.

Step 7 : "android:layout_weight" ,as name shows of this attribute that it is related to somehow weight of view. Yes, you are correct it defines the weight of views.

For example : A parent view contains two child views. now if both child gets android:layout_weight=1 than both child will take equal space in mobile screen. we often use this to equalize the views.

  

Comments