LinearLayout (Java)
LinearLayout stacks children vertically or horizontally. It’s ideal for simple rows/columns and works great inside a ScrollView. Mastering a few attributes—orientation, gravity, layout_gravity, and layout_weight—covers 90% of use-cases.
Core Properties (Cheat Sheet)
| Attribute | What it affects | Tips |
|---|---|---|
| android:orientation | Stack direction (vertical | horizontal) | Choose row vs column |
| android:gravity | How the parent positions all its children | e.g., center_horizontal |
| android:layout_gravity | How a child is placed inside its parent | e.g., right align one button |
| android:baselineAligned | Align baselines in a horizontal row | Use when you want text to line up |
| android:weightSum | Total weight of a row/column (optional) | Usually not needed; Android sums automatically |
| layout_weight | Proportional distribution in the orientation direction | Use with 0dp width/height to “stretch” |
Visual Intuition
Vertical
Children stacked top → bottom
Horizontal
Children laid out left → right
Weight
Divide remaining space proportionally
Example 1 — Weighted Buttons Row (1:2 ratio)
Cancel
Save
Result: first button ≈ 33%, second ≈ 67%
<!-- res/layout/row_actions.xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:text="Cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:text="Save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
</LinearLayout>
Why this works: In a horizontal LinearLayout, children with layout_width="0dp" and non-zero layout_weight split the remaining row width.
Example 2 — Profile Column (Centered vertical stack)
Avatar
Name
Bio
<!-- res/layout/view_profile.xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:padding="16dp">
<ImageView
android:src="@drawable/ic_avatar"
android:layout_width="120dp"
android:layout_height="120dp" />
<TextView
android:text="Sonu Kumar Pandit"
android:textStyle="bold"
android:textSize="18sp"
android:layout_marginTop="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="Android Developer • Educator"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Gravity vs layout_gravity: Here, parent android:gravity="center_horizontal" centers all children. If you wanted only the last TextView centered, you would put android:layout_gravity="center_horizontal" on that child.
Example 3 — Two-Column Form (Nested rows with baseline alignment)
We build a vertical column of horizontal rows. Each row uses baseline alignment so labels and fields line up perfectly.
<!-- res/layout/form_two_column.xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true">
<TextView
android:text="Email"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<EditText
android:inputType="textEmailAddress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:baselineAligned="true">
<TextView
android:text="Password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<EditText
android:inputType="textPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
</LinearLayout>
</LinearLayout>
Baseline magic: android:baselineAligned="true" makes text baselines line up even if controls have different intrinsic heights.
Gravity vs layout_gravity
<!-- Parent centers all children -->
<LinearLayout
android:gravity="center_horizontal" ... > ... </LinearLayout>
<!-- Only this child is pushed to the end -->
<Button android:layout_gravity="end" ... />
Performance & Best Practices
- Minimize nesting: A few nested LinearLayouts are okay, but avoid deep trees. Use ConstraintLayout for complex screens.
- Use weights sparingly: Weights are powerful but slightly more expensive to measure; prefer fixed or wrap sizes where possible.
- In ScrollView: The child LinearLayout should typically be wrap_content in height to avoid infinite height issues.
- RTL safety: When mixing with Relative/Constraint, prefer start/end over left/right.