Skip to content
Related Articles

Related Articles

Improve Article

LinearLayout and its Important Attributes with Examples in Android

  • Difficulty Level : Hard
  • Last Updated : 14 Oct, 2020
Geek Week

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: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



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:weightSumDefines the total weight sum
android:orientationHow 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:idThis 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: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:

The output of the above code

2. How to arrange children views in a horizontal manner

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    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:

The output of the above code

3. How to use layout_weight and weightSum

XML






<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    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:

The output of the above code

4. How to use gravity 

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    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:

The output of the above code

5. How to use layout_gravity 

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    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:

The output of the above code

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!



My Personal Notes arrow_drop_up
Recommended Articles
Page :