Android development
LinearLayout
UI design
button distribution
Android layout

Is it possible to evenly distribute buttons across the width of a LinearLayout

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Yes, a horizontal LinearLayout can distribute buttons evenly across its width. The standard approach is to give each button layout_width="0dp" and the same layout_weight, which tells Android to divide the available horizontal space proportionally.

Use Equal Weights in a Horizontal LinearLayout

The core XML pattern looks like this:

xml
1<LinearLayout
2    xmlns:android="http://schemas.android.com/apk/res/android"
3    android:layout_width="match_parent"
4    android:layout_height="wrap_content"
5    android:orientation="horizontal">
6
7    <Button
8        android:layout_width="0dp"
9        android:layout_height="wrap_content"
10        android:layout_weight="1"
11        android:text="One" />
12
13    <Button
14        android:layout_width="0dp"
15        android:layout_height="wrap_content"
16        android:layout_weight="1"
17        android:text="Two" />
18
19    <Button
20        android:layout_width="0dp"
21        android:layout_height="wrap_content"
22        android:layout_weight="1"
23        android:text="Three" />
24</LinearLayout>

Each button gets one share of the remaining width, so all three occupy equal horizontal space.

Why 0dp Matters

This part is easy to miss. If you keep layout_width="wrap_content", the button first measures itself based on its text and only then participates in weight distribution. That usually leads to uneven sizing.

Using 0dp tells LinearLayout to ignore the intrinsic width and allocate width from weights instead. For equal distribution, pair 0dp with identical weights.

Unequal Distribution Is Also Possible

If one button should take twice as much space as another, change the weights.

xml
1<Button
2    android:layout_width="0dp"
3    android:layout_height="wrap_content"
4    android:layout_weight="2"
5    android:text="Primary" />
6
7<Button
8    android:layout_width="0dp"
9    android:layout_height="wrap_content"
10    android:layout_weight="1"
11    android:text="Secondary" />

The first button receives two shares while the second receives one.

Adding Spacing Without Breaking the Layout

Margins are usually the cleanest way to create gaps between evenly sized buttons.

xml
1<Button
2    android:layout_width="0dp"
3    android:layout_height="wrap_content"
4    android:layout_weight="1"
5    android:layout_marginEnd="8dp"
6    android:text="Accept" />

Apply symmetric margins carefully. Large margins reduce the space available for the buttons themselves, so very small screens may require shorter labels or a different layout.

Programmatic Setup in Kotlin

The same idea works in code. The key is still width = 0 plus equal weights.

kotlin
1val layout = LinearLayout(this).apply {
2    orientation = LinearLayout.HORIZONTAL
3}
4
5repeat(3) { index ->
6    val button = Button(this).apply {
7        text = "Button ${index + 1}"
8    }
9
10    val params = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
11    layout.addView(button, params)
12}

That creates three evenly distributed buttons without XML. The same approach works when buttons are generated from a runtime list of actions rather than hardcoded in the layout.

When LinearLayout Is Enough and When It Is Not

For a simple row of actions, LinearLayout with weights is perfectly reasonable. If the layout becomes more complex, ConstraintLayout often provides better control and performance, especially when multiple rows, guidelines, or adaptive constraints are involved.

Still, for the specific problem of “make these buttons evenly share a row,” LinearLayout remains direct and easy to read.

Button Text Length Still Matters Visually

Equal width does not guarantee equal visual balance. If one label is much longer than the others, the row can still look awkward or the text may wrap.

Options include:

  • shortening labels
  • using icons with text carefully
  • reducing text size only if readability stays acceptable
  • switching to a stacked layout on narrow screens

Layout correctness and visual quality are related but not identical.

Common Pitfalls

The most common mistake is assigning weights while leaving layout_width as wrap_content. Another is forgetting to set the parent orientation to horizontal. Developers also sometimes add fixed widths, which defeats the purpose of weight-based distribution. Finally, equal widths can still produce poor results if button labels are too long for the available space.

Summary

  • Use a horizontal LinearLayout for simple evenly spaced button rows.
  • Set each button to layout_width="0dp".
  • Give each button the same layout_weight, usually 1.
  • Use margins for spacing rather than fixed widths.
  • Recheck label length so the evenly sized buttons still look good on small screens.

Course illustration
Course illustration

All Rights Reserved.