Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

LinearLayout and its Important Attributes with

Examples in Android
Read Courses Jobs

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
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
</LinearLayout>

Some Important Attributes of LinearLayout

Attributes Description

It is defined individually to the child’s views to specify how


LinearLayout
android:layout_weight
divides the remaining space amongst the views it contains

android:weightSum Defines the total weight sum


Attributes Description

How the elements should be arranged in the layout. It can be horizontal


android:orientation
or vertical.

It specifies how an object should position its content on its X and Y


axes.
android:gravity
Possible values are – center_vertical, fill, center, bottom, end, etc.

Sets the gravity of the View or Layout relative to its parent.


android:layout_gravity
Possible values are – center_vertical, fill, center, bottom, end, etc.

This must be a boolean value, either “true” or “false” and prevents the
layout
android:baselineAligned
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
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- Add vertical in the android:orientation-->

<!-- Add Button-->


<Button
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="wrap_content"/>

<!-- Add Button-->


<Button
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="wrap_content"/>

<!-- Add Button-->


<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
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<!-- Add horizontal in the android:orientation-->

<!-- Add Button-->


<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="10dp" />

<!-- Add Button-->


<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="10dp" />

<!-- Add Button-->


<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
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
tools:context=".MainActivity">
<!-- Add value in the android:weightSum-->
<!-- Add horizontal in the android:orientation-->

<!-- Add Button-->


<!-- Add value in the android:layout_weight-->
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1" />

<!-- Add Button-->


<!-- Add value in the android:layout_weight-->
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1" />

<!-- Add Button-->


<!-- Add value in the android:layout_weight-->
<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
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
tools:context=".MainActivity">
<!-- Add value in the android:weightSum-->
<!-- Add horizontal in the android:orientation-->

<!-- Add Button-->


<!-- Add value in the android:gravity -->
<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" />

<!-- Add Button-->


<!-- Add value in the android:gravity -->
<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" />

<!-- Add Button-->


<!-- Add value in the android:gravity -->
<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
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- Add vertical in the android:orientation-->

<!-- Add Button-->


<!-- Add value in the layout_gravity -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp" />

<!-- Add Button-->


<!-- Add value in the layout_gravity -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="10dp" />

<!-- Add Button-->


<!-- Add value in the layout_gravity -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="10dp" />

</LinearLayout>

Output UI:

You might also like