Android LinearLayout Example

Android LinearLayout deals with placing components one after another either in horizontal or vertical manner. It acts as a parent container, which can hold components like TextView, RadioButton, EditText, ImageView, Button, as well as other Layouts inside it as their child components. Android LinearLayout are of two kind :

  1. Horizontal LinearLayout : Every new component we  place, aligns towards right of the older component i.e. horizontally.
  2. Vertical LinearLayout : Addition of new component will be done below the older component i.e. vertically.

 


 

Download

Source code : LinearLayout Horizontal

 

Download

Source code : LinearLayout Vertical

 

 

 

 

 

 

 


Now Let us work on Project : (Refer – Configuring Existing AndroidStudioProject) LinearLayout which you might have downloaded from above or you can follow the scripts below by copying it to implement in your Project. To launch Downloaded project, Extract the zip, copy it into AndroidStudioProjects. Now from Android Studio click open Existing Android Studio Project and select the path to extracted file we just copied in AndroidStudioProjects directory. If Sync SDK path prompt comes just click OK and you are ready to work. If still you get error check for AndroidStudioProjects/LinearLayout/app/build.gradle , it will contain different SDK Version. Change it to SDK Version which you have.

 

 

1. Android LinearLayout Horizontal

  • We can make our LinearLayout  horizontal, simply by defining a property  android:orientation=”horizontal”.
  • Below you will see that we have started our Linear layout at Line-2 & it ends on Line-27. So everything between these two lines falls as child of  our Horizontal LinearLayout.
  • You will see that all 3 Buttons we made inside our LinearLayout will be placed in a single horizontal line.
  • Add 5-6 more buttons. You will see that when the screen area will be completely occupied, new buttons will not be visible since they will be out of frame. So we should keep this in mind while designing layouts.

 

activity_main.xml : Layout file

 

Output : Android LinearLayout – Horizontal

Android LinearLayout Horizontal

 

 

 

2. Android LinearLayout Vertical

  • We can make our LinearLayout  Vertical, simply by changing property to  android:orientation=”vertical”.
  • You will see that we have started our Linear layout at Line-2 & it ends on Line-27. So everything between these two lines falls as child of our Vertical LinearLayout.
  • You will see that all 3 Buttons we made inside our LinearLayout will be placed in a vertical manner.

 

activity_main.xml : Layout file

 

Output :  Android LinearLayout – Vertical

Android LinearLayout Vertical

 

 

MainActivity.java common for both horizontal & vertical layout

 

 

Try it yourself :

  • Try changing layout_height – width to fill_parent for all buttons & see that only 1st button will be visible.
  • Try making combined layout using both Vertical & Horizontal linearlayout to make a simple login screen. Add a vertical linearlayout inside which place 2 horizontal linearlayout, both of horizontal layout containing 1 textView & 1 EditText. At end of 2nd Horizontal layout add a Login button.

 

Report Errors + Bugs & Become Insider for Nestedif.com

We would like to hear you, if you find any error or misspelled phrase while reading our tutorials. By reporting mistakes through email to insider@nestedif.com you could help other peers.