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:
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.
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.
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.
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
LinearLayoutfor simple evenly spaced button rows. - Set each button to
layout_width="0dp". - Give each button the same
layout_weight, usually1. - Use margins for spacing rather than fixed widths.
- Recheck label length so the evenly sized buttons still look good on small screens.

