Table of contents
1.
Introduction
2.
Important Attributes
3.
Types Of Linear Layout Orientation
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Android Linear Layout

Author Rahul Sain
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A linear layout is where other views are arranged horizontally in a single column or vertically in a single row.

In this article, we will learn about Linear layouts and the types of linear layouts. But before jumping into this, we would strongly recommend learning about Android UI Layouts.

The upcoming section will discuss various attributes provided by Linear Layout with the help of some examples.

Important Attributes

Let's look at some properties of the Linear Layout that we will use while developing the UI for an Android app.

  1. baselineAligned: When set to false, the layout is prevented from matching the baselines of its offspring.
  2. baselineAlignedChildIndexWhen this layout is part of a baseline aligned layout, it can define which of its children to baseline align to (that is, which child Button).
  3. dividerDrawn as a vertical divider between buttons.
  4. gravitySpecifies how an object's content should be positioned within its boundaries on the X and Y axes.
  5. measureWithLargestChildWhen this attribute is set to true, all children with weight will be the smallest child.
  6. orientationIs the layout a column or a row? For a row, use horizontal, and for a column, use vertical.
  7. weightSum: The maximum weight sum is defined here.

Types Of Linear Layout Orientation

As the name implies, these two orientations arrange their children in a line, either vertically or horizontally. Let us go over these in greater depth.

Let's discuss both with a single example, we will write the snippet below in our activity_main.xml.

Code:

<?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"
    tools:context=".MainActivity"
    android:weightSum="2"
    android:baselineAligned="false">

        <LinearLayout
            android:id="@+id/ll_grey"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#CEC7D6"
            android:padding="20dp"
            android:orientation="horizontal"
            android:layout_weight="1">

                <androidx.appcompat.widget.AppCompatTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/app_name"/>

                <androidx.appcompat.widget.AppCompatTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/app_name"/>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_green"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#6FBA54"
            android:padding="20dp"
            android:orientation="vertical"
            android:layout_weight="1">

                <androidx.appcompat.widget.AppCompatTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/app_name"/>

                <androidx.appcompat.widget.AppCompatTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/app_name"/>

        </LinearLayout>

</LinearLayout>

Output:

In the above example, we can see that we have created two layouts, one is horizontal, and the other is vertical. We have added AppCompatTextView, which is similar to normal TextView.

The orientation is self-explanatory. The default orientation in the Linear layout is vertical. Let us move into another attribute like weightSum, where we supply a total number of child layouts where we will use layout_weight property defining how to divide the size of the layout within a total number of layout children.

The layout_weight attribute specifies the relative importance of each child control within the parent linear layout. weightSum attribute is necessary if the child's weight property is defined.

The gravity attribute is an optional attribute that is used to regulate the layout's alignment, such as start, end, center, top, bottom, and so on. Let’s make changes in our activity_main.xml.

Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="start"
    android:orientation="horizontal">

    <Button
        android:id="@+id/buttonOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="#C36969"
        android:text="Button One" />

    <Button
        android:id="@+id/buttonTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="#5ED15D"
        android:text="Button Two" />

</LinearLayout>

Output:

For linear layout, we have specified gravity start. As a result, the buttons in Horizontal orientation are aligned from the left side.

FAQs

  1. What is the difference between padding and margin android layout?
    Ans: This is the distinction between android margin and padding. Padding is the gap inside the border between the border and the content of the actual view. The spaces outside, between the border and the other elements adjacent to this perspective, are referred to as margins.
     
  2. What does setOnClickListener do in Android?
    Ans: The setOnClickListener function in Android is useful since it allows us to associate a listener with specific attributes. A callback function will be executed when this method is invoked. You can also build a class for more than one listener, leading to code reuse.
     
  3. What is custom Toast in Android?
    Ans: Toast is a small popup notification in Android that displays information about the operation we completed in our app. The Toast will display the message for a short time before disappearing automatically after a timeout.

Key Takeaways

In this article, we have extensively discussed various types of linear layouts in Android and their attributes.

You can head over to our Android Development Course on the Coding Ninjas Website to dive deep into Android Development and build future applications.

We hope that this blog has helped you enhance your knowledge regarding linear layout attributes and if you would like to learn more, check out our articles on Relative Layout. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass