LinearLayout is the most basic layout in android studio, that aligns all the children sequentially either in a horizontal manner or a vertical manner by specifying the android:orientation attribute. If one applies android:orientation=”vertical” then elements will be arranged one after another in a vertical manner and If you apply android:orientation=”horizontal” then elements will be arranged one after another in a horizontal manner.
Sample Code of LinearLayout
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
tools:context = ".MainActivity" >
</ LinearLayout >
|
Some Important Attributes of LinearLayout
Attributes
|
Description
|
android:layout_weight |
It is defined individually to the child’s views to specify how LinearLayout
divides the remaining space amongst the views it contains
|
android:weightSum |
Defines the total weight sum |
android:orientation |
How the elements should be arranged in the layout. It can be horizontal or vertical. |
android:gravity |
It specifies how an object should position its content on its X and Y axes.
Possible values are – center_vertical, fill, center, bottom, end, etc.
|
android:layout_gravity |
Sets the gravity of the View or Layout relative to its parent.
Possible values are – center_vertical, fill, center, bottom, end, etc.
|
android:baselineAligned |
This must be a boolean value, either “true” or “false” and prevents the layout
from aligning its children’s baselines.
|
android:id |
This gives a unique id to the layout. |
Examples
1. How to arrange children views in a vertical manner
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
tools:context = ".MainActivity" >
< Button
android:layout_width = "match_parent"
android:layout_margin = "10dp"
android:layout_height = "wrap_content" />
< Button
android:layout_width = "match_parent"
android:layout_margin = "10dp"
android:layout_height = "wrap_content" />
< Button
android:layout_width = "match_parent"
android:layout_margin = "10dp"
android:layout_height = "wrap_content" />
</ LinearLayout >
|
Output UI:

2. How to arrange children views in a horizontal manner
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "horizontal"
tools:context = ".MainActivity" >
< Button
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_margin = "10dp" />
< Button
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_margin = "10dp" />
< Button
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_margin = "10dp" />
</ LinearLayout >
|
Output UI:

3. How to use layout_weight and weightSum
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "horizontal"
android:weightSum = "3"
tools:context = ".MainActivity" >
< Button
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_margin = "10dp"
android:layout_weight = "1" />
< Button
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_margin = "10dp"
android:layout_weight = "1" />
< Button
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_margin = "10dp"
android:layout_weight = "1" />
</ LinearLayout >
|
Output UI:

4. How to use gravity
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "horizontal"
android:weightSum = "3"
tools:context = ".MainActivity" >
< Button
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_margin = "10dp"
android:layout_weight = "1"
android:gravity = "bottom|center"
android:text = "GFG" />
< Button
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_margin = "10dp"
android:layout_weight = "1"
android:gravity = "center"
android:text = "GFG" />
< Button
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_margin = "10dp"
android:layout_weight = "1"
android:gravity = "center|top"
android:text = "GFG" />
</ LinearLayout >
|
Output UI:

5. How to use layout_gravity
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
tools:context = ".MainActivity" >
< Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity = "center"
android:layout_margin = "10dp" />
< Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity = "right"
android:layout_margin = "10dp" />
< Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity = "left"
android:layout_margin = "10dp" />
</ LinearLayout >
|
Output UI:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
14 Oct, 2020
Like Article
Save Article