Android
LinearLayout
UI Design
Layout Spacing
Android Development

How to make space between LinearLayout children?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Android, spacing between LinearLayout children usually comes from margins, divider drawables, or a dedicated spacer view. The best choice depends on whether every child should have the same gap or whether only certain items need extra room. The important distinction is that margin adds space outside a child, while padding adds space inside it.

Use Margins for Simple Child Spacing

The most direct approach is to put a top or start margin on the later children.

xml
1<LinearLayout
2    android:layout_width="match_parent"
3    android:layout_height="wrap_content"
4    android:orientation="vertical"
5    android:padding="16dp">
6
7    <Button
8        android:layout_width="match_parent"
9        android:layout_height="wrap_content"
10        android:text="Button 1" />
11
12    <Button
13        android:layout_width="match_parent"
14        android:layout_height="wrap_content"
15        android:layout_marginTop="8dp"
16        android:text="Button 2" />
17
18    <Button
19        android:layout_width="match_parent"
20        android:layout_height="wrap_content"
21        android:layout_marginTop="8dp"
22        android:text="Button 3" />
23</LinearLayout>

For a horizontal LinearLayout, the same idea becomes android:layout_marginStart or android:layout_marginEnd.

Use Dividers for Uniform Gaps

If every child should have the same spacing, LinearLayout can apply a divider drawable between children.

xml
1<LinearLayout
2    android:layout_width="match_parent"
3    android:layout_height="wrap_content"
4    android:orientation="vertical"
5    android:divider="@drawable/spacer"
6    android:showDividers="middle">
7
8    <TextView
9        android:layout_width="wrap_content"
10        android:layout_height="wrap_content"
11        android:text="First" />
12
13    <TextView
14        android:layout_width="wrap_content"
15        android:layout_height="wrap_content"
16        android:text="Second" />
17</LinearLayout>

And the spacer drawable can be transparent:

xml
1<shape xmlns:android="http://schemas.android.com/apk/res/android">
2    <size android:height="12dp" />
3    <solid android:color="@android:color/transparent" />
4</shape>

This is a good option when you want consistent spacing without adding margin rules to every child.

Use Space for One-Off Gaps

If only one gap is special, a Space view is fine.

xml
1<LinearLayout
2    android:layout_width="match_parent"
3    android:layout_height="wrap_content"
4    android:orientation="vertical">
5
6    <TextView
7        android:layout_width="wrap_content"
8        android:layout_height="wrap_content"
9        android:text="Title" />
10
11    <Space
12        android:layout_width="match_parent"
13        android:layout_height="16dp" />
14
15    <TextView
16        android:layout_width="wrap_content"
17        android:layout_height="wrap_content"
18        android:text="Subtitle" />
19</LinearLayout>

This is clear, but it adds another view to the hierarchy, so it is better for occasional gaps than for uniform spacing across many children.

Set Spacing Programmatically When Needed

If spacing depends on runtime conditions, set margins in code.

java
1LinearLayout container = findViewById(R.id.container);
2
3for (int i = 0; i < 3; i++) {
4    Button button = new Button(this);
5    button.setText("Item " + i);
6
7    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
8        LinearLayout.LayoutParams.MATCH_PARENT,
9        LinearLayout.LayoutParams.WRAP_CONTENT
10    );
11
12    if (i > 0) {
13        float density = getResources().getDisplayMetrics().density;
14        params.topMargin = (int) (8 * density);
15    }
16
17    container.addView(button, params);
18}

The key detail is converting dp to pixels before assigning the margin.

Horizontal Layouts Follow the Same Rule

For horizontal button rows or toolbars, the exact same patterns apply, just on the horizontal axis. Use layout_marginStart between items or a vertical divider drawable if you want consistent spacing across the row.

That keeps vertical and horizontal spacing strategies consistent across the app.

Prefer Start and End Margins for RTL Support

On modern Android layouts, layout_marginStart and layout_marginEnd are safer than layout_marginLeft and layout_marginRight. Start and end automatically adapt when the device uses a right-to-left language, so the visual gap stays between neighboring views instead of drifting to the wrong side.

xml
1<LinearLayout
2    android:layout_width="match_parent"
3    android:layout_height="wrap_content"
4    android:orientation="horizontal"
5    android:padding="16dp">
6
7    <Button
8        android:layout_width="0dp"
9        android:layout_height="wrap_content"
10        android:layout_weight="1"
11        android:text="Cancel" />
12
13    <Button
14        android:layout_width="0dp"
15        android:layout_height="wrap_content"
16        android:layout_weight="1"
17        android:layout_marginStart="12dp"
18        android:text="Save" />
19</LinearLayout>

This also works well with weighted children. The weight decides how much width each child receives, and the start margin defines the gap between them.

Choose Outer Spacing Separately

Another useful layout rule is to separate child-to-child spacing from parent-to-content spacing. Parent padding controls the outer breathing room around the group, while child margin or dividers control the gaps between children. When those two concerns are mixed together, layouts become harder to tune and reuse.

Common Pitfalls

  • Using padding when you actually want space between children.
  • Adding margin to the first child when parent padding would express the outer spacing more cleanly.
  • Hardcoding pixel values instead of density-independent dp values.
  • Overusing Space views when a divider or margin pattern would be simpler.
  • Mixing several spacing techniques in one small layout and making it harder to maintain.

Summary

  • Use child margins for simple spacing.
  • Use a divider drawable when every child should have the same gap.
  • Use Space for occasional one-off gaps.
  • Convert dp to pixels when setting margins in code.
  • Prefer one consistent spacing strategy per layout for maintainability.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.