Think of LinearLayout as a container that arranges UI elements in a straight line — either vertically (top → bottom) or horizontally (left → right).
For example:
Buttons in a row
Form fields stacked one below another
It is one of the most basic layout types used when building apps in Android Studio.
1. Definition
LinearLayout is a ViewGroup in Android that arranges its child views in a single direction (horizontal or vertical).
In simple words:
➡ It places UI components one after another in a line.
2. Purpose
The main purpose of LinearLayout is to:
Organize UI components in a simple line structure
Create forms, menus, toolbars
Control space distribution using weights
Keep UI clean and structured
Example uses:
Login screen
Navigation bar
Button row
3. Syntax
Basic XML syntax:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
Explanation:
layout_width→ width of layoutlayout_height→ height of layoutorientation→ direction of items
4. Types of Orientation
Vertical Layout
Elements appear top to bottom
android:orientation="vertical"
Example order:
TextView
EditText
Button
Horizontal Layout
Elements appear left to right
android:orientation="horizontal"
Example:
Button Button Button
5. Important Properties
1. orientation
Defines layout direction.
vertical
horizontal
Example:
android:orientation="vertical"
2. layout_weight
Controls how much space a view occupies.
Example:
<Button
android:layout_weight="1"/>
If 3 buttons all have weight 1 → space divided equally.
3. gravity
Aligns children inside layout.
Example:
android:gravity="center"
Options:
center
left
right
top
bottom
4. layout_gravity
Controls position of a child view inside parent.
Example:
android:layout_gravity="center"
5. padding
Adds space inside the layout.
Example:
android:padding="16dp"
6. Important Methods
These methods are usually used in Java / Kotlin code.
1. setOrientation()
Sets layout direction.
linearLayout.setOrientation(LinearLayout.VERTICAL);
2. setGravity()
Aligns children.
linearLayout.setGravity(Gravity.CENTER);
3. addView()
Adds a view dynamically.
linearLayout.addView(button);
4. removeView()
Removes a view.
linearLayout.removeView(textView);
7. Real-Time Examples
Example 1: Login Form
<LinearLayout
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:hint="Email"/>
<EditText
android:hint="Password"/>
<Button
android:text="Login"/>
</LinearLayout>
Layout result:
Email Field
Password Field
Login Button
Example 2: Button Row
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:text="Yes"/>
<Button android:text="No"/>
<Button android:text="Cancel"/>
</LinearLayout>
Result:
Yes No Cancel
Example 3: Equal Space Buttons (Weight)
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="A"
android:layout_weight="1"
android:layout_width="0dp"/>
<Button
android:text="B"
android:layout_weight="1"
android:layout_width="0dp"/>
</LinearLayout>
Both buttons take equal width.
8. Advantages
✔ Simple to use
✔ Good for small layouts
✔ Easy alignment
✔ Supports weight-based sizing
9. Disadvantages
❌ Too many nested LinearLayouts can slow UI
❌ Less flexible compared to ConstraintLayout
10. Simple One-Line Summary
LinearLayout = A container that arranges UI elements in a straight line (horizontal or vertical).