Android LinearLayout Explained – Complete Beginner to Advanced Guide (Kotlin & Java)

Learn Android LinearLayout with examples. Understand orientation, layout_weight, and real UI design using Kotlin and Java.

Android LinearLayout Explained – Complete Beginner to Advanced Guide (Kotlin & Java)

Introduction

When building Android user interfaces, layouts play a crucial role. Layouts define how UI components like buttons, text fields, and images are arranged on the screen.

One of the simplest and most commonly used layouts in Android is LinearLayout.

LinearLayout arranges UI elements in a single direction — either vertically or horizontally.

This makes it very easy to build simple UI structures such as login forms, product lists, and dashboard sections.

Let’s understand how LinearLayout works.


What is LinearLayout?

LinearLayout is a ViewGroup that arranges its child views in a single line.

That line can be:

  • Vertical

  • Horizontal

Example structure:

LinearLayout
├── TextView
├── EditText
└── Button

Each view appears one after another.


LinearLayout Orientation

Orientation determines the direction of layout.

There are two types:

  1. Vertical

  2. Horizontal


Vertical LinearLayout

Views are arranged from top to bottom.

Example:

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:text="Username"/>

<EditText
android:hint="Enter username"/>

<Button
android:text="Login"/>

</LinearLayout>

Result:

Username
[Enter username]
[Login Button]

This layout is commonly used for forms.


Horizontal LinearLayout

Views are arranged left to right.

Example:

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"/>

<TextView
android:text="Product Name"/>

</LinearLayout>

Result:

[Image] Product Name

This layout is often used in list items.


layout_weight Explained

One powerful feature of LinearLayout is layout_weight.

It allows views to share available space.

Example:

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
android:layout_weight="1"
android:layout_width="0dp"
android:text="Cancel"/>

<Button
android:layout_weight="1"
android:layout_width="0dp"
android:text="Submit"/>

</LinearLayout>

Result:

[Cancel] [Submit]

Both buttons take equal width.


Why layout_width Must Be 0dp

When using weight:

layout_width = 0dp

Because weight controls size.

If width is wrap_content, weight may not work correctly.


Example – Equal Columns Layout

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:text="Price"/>

<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:text="Qty"/>

<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:text="Total"/>

</LinearLayout>

Perfect for billing screens in POS apps.


Nested LinearLayouts

You can place LinearLayouts inside each other.

Example:

LinearLayout (vertical)
├── LinearLayout (horizontal)
├── LinearLayout (horizontal)
└── LinearLayout (horizontal)

Example UI:

Product Name Price
Quantity Total
[Checkout Button]

But too many nested layouts can slow UI.


Performance Consideration

LinearLayout is simple but can become inefficient when heavily nested.

Example bad structure:

LinearLayout
└── LinearLayout
└── LinearLayout
└── LinearLayout

Too many nested layers increase rendering time.

Modern apps often prefer ConstraintLayout.


Real Production Example – Login Screen

Example login screen layout:

<LinearLayout
android:orientation="vertical"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:hint="Email"/>

<EditText
android:hint="Password"/>

<Button
android:text="Login"/>

</LinearLayout>

Simple and clean.


Real Production Example – POS Billing Row

Billing item row:

Product Name | Qty | Price | Total

Using LinearLayout horizontal with weights.

This is common in POS apps.


LinearLayout Attributes

Important attributes:

Attribute Purpose
orientation Direction of layout
layout_weight Space distribution
gravity Align children
layout_gravity Position child
padding Inner spacing

Gravity vs Layout Gravity

Example:

gravity → alignment of child views inside layout
layout_gravity → position of view inside parent

Example:

android:gravity="center"

Centers children.


Common Beginner Mistakes

❌ Too many nested LinearLayouts
❌ Incorrect weight usage
❌ Forgetting orientation
❌ Mixing match_parent with weight incorrectly

Always design layout carefully.


Interview-Level Answer

If interviewer asks:

“What is LinearLayout?”

Professional answer:

LinearLayout is a ViewGroup that arranges its child views in a single direction, either vertically or horizontally. It also supports weight-based space distribution.


Simple Summary

LinearLayout arranges UI elements:

Vertical → Top to Bottom
Horizontal → Left to Right

Key feature:

layout_weight → divide space

Conclusion

LinearLayout is one of the simplest layouts in Android, making it perfect for beginners. It allows developers to quickly arrange UI components in vertical or horizontal structures.

Although modern apps often use ConstraintLayout for complex designs, LinearLayout is still widely used for simple layouts like forms, rows, and sections.

Understanding LinearLayout is an important step in mastering Android UI design.

Share

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Angry Angry 0
Sad Sad 0
Wow Wow 0