Layouts: Row, Column, Box
Why it matters
- Every screen in a Compose app is built from these three containers; getting arrangement, alignment and weight wrong produces misaligned UIs.
- A missing
weighton a flexible child is a classic production bug: a long label pushes a trailing switch or icon off the screen. - Interviewers ask how Row measures weighted children, how arrangement differs from alignment, and when you need a custom
Layout. - Compose measures each child once per layout pass, so nesting Rows, Columns and Boxes is cheap. Knowing that stops you from 'optimising' the wrong thing.
The simple explanation
- A Row is a shelf: items sit side by side, start to end.
- Arrangement is how you space items along the shelf (packed at the start, at both ends, with fixed gaps).
- Alignment is where each item sits across the shelf (top, centre, bottom).
- A Column is a stack: items sit one above the other.
- A Box is a picture frame: you layer things on top of each other.
- Weight is saying "this item gets a share of the space that is left" instead of a fixed size.
Row, Column and Box are the basic layout composables in Compose. They cover what LinearLayout and FrameLayout did in XML.
Row places children horizontally (start to end, so right to left in RTL languages). Column places them top to bottom. Box stacks them on top of each other.
Each linear container has two layout concepts:
| Concept | What it does | Where you set it |
|---|---|---|
| Arrangement | Distributes children along the main axis (horizontal for Row, vertical for Column) | horizontalArrangement (Row) / verticalArrangement (Column) |
| Alignment | Positions children on the cross axis | verticalAlignment (Row), horizontalAlignment (Column), contentAlignment (Box) |
Defaults: a Row uses Arrangement.Start and Alignment.Top; a Column uses Arrangement.Top and Alignment.Start; a Box uses Alignment.TopStart.
Arrangement.spacedBy(8.dp) adds 8 dp between adjacent children only, not at the edges. Arrangement.SpaceBetween puts the first child at the start and the last at the end, with equal gaps between children. Arrangement.SpaceEvenly puts equal space before, between and after. Arrangement.SpaceAround puts half a gap at each edge and a full gap between children.
Modifier.weight(1f) exists only inside Row and Column (it is defined on RowScope and ColumnScope). The parent first measures the children without weight, then splits the space that is left between weighted children in proportion to their weights. The weight must be greater than zero. By default (fill = true) a weighted child is forced to exactly its share.
weight is parent data, not a size modifier, so its position in the chain does not matter: Modifier.weight(1f).padding(8.dp) and Modifier.padding(8.dp).weight(1f) give the same layout. In both, the padding sits inside the child's weighted share.
A Box sizes itself to fit its largest child (unless you give the Box its own size). It does not resize the smaller children. To make a child cover the whole Box, such as a scrim over an image, use Modifier.matchParentSize() on that child. Use Box for overlays (badges, spinners, scrims), not for linear sequences.
Notes
- Row arranges children horizontally, Column vertically, Box stacks them. Together they cover
LinearLayoutandFrameLayout. - Arrangement controls main-axis placement (one parameter on the container). Alignment controls cross-axis position.
Arrangement.spacedBy(n.dp)adds n dp between adjacent children only. It does not add space at the start or end edges.Arrangement.SpaceBetweenputs the first child at the start edge and the last at the end edge. With two children, that pushes them to opposite ends.Modifier.weight(1f)is only available inside Row and Column. In Row it sets width; in Column it sets height. Weighted children share the space left after unweighted children are measured.- The position of
weightin a modifier chain does not matter. Padding is inside the weighted share either way. - A Row's default
verticalAlignmentisAlignment.Top, and Box's defaultcontentAlignmentisAlignment.TopStart. - A Box is as big as its largest child. Use
Modifier.matchParentSize()for a child that should cover the Box without making it bigger. - Compose measures each child once per layout pass, so nesting layouts is cheap compared with the View system. Intrinsic measurements and subcomposition (
BoxWithConstraints) do add work. - A custom
Layoutis for arrangements the built-in containers cannot express, not a speed fix for normal nesting. CheckFlowRowandFlowColumnbefore writing one.
Code example
A user card that combines Column, Row and Box: spacing, SpaceBetween, weight and a centred Box.
@Composable
fun UserCard(name: String, role: String, modifier: Modifier = Modifier) {
// Column is the outer container; spacedBy adds 8 dp between children
Column(
modifier = modifier
.fillMaxWidth()
.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
// Row with SpaceBetween: name at the start, role at the end
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically,
) {
Text(name, style = MaterialTheme.typography.titleMedium)
Text(
role,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
// Box: content centred in a fixed-height area
Box(
modifier = Modifier
.fillMaxWidth()
.height(48.dp),
contentAlignment = Alignment.Center,
) {
Text("Tap to expand", style = MaterialTheme.typography.bodySmall)
}
// Two equal-weight children split the row width 50/50
Row(modifier = Modifier.fillMaxWidth()) {
Text("Online", modifier = Modifier.weight(1f))
Text(
"3m ago",
modifier = Modifier.weight(1f),
textAlign = TextAlign.End,
)
}
}
}Arrangement.spacedBy(8.dp)on the Column adds 8 dp between children but not at the top or bottom; thepaddinggives the edge space.Arrangement.SpaceBetweenin the first Row pushes the name to the start and the role to the end without needing weight. With a very long name the two texts can still collide; giving the nameModifier.weight(1f)would make it wrap instead.- The two
Modifier.weight(1f)children in the last Row split the width equally; changing one toweight(2f)would give it two-thirds.
Run the idea in plain Kotlin
This computes where three 40 px children start in a 360 px Row for each arrangement, and how weights share the space that is left.
fun positions(total: Int, sizes: List<Int>, mode: String): List<Int> {
val free = total - sizes.sum()
val n = sizes.size
val (edge, gap) = when (mode) {
"SpaceBetween" -> 0 to free / (n - 1)
"SpaceEvenly" -> free / (n + 1) to free / (n + 1)
"SpaceAround" -> free / (2 * n) to free / n
else -> 0 to 0 // Start
}
var x = edge
return sizes.map { size ->
val start = x
x += size + gap
start
}
}
fun weights(total: Int, fixed: Int, weights: List<Float>): List<Int> {
val remaining = total - fixed
val sum = weights.sum()
return weights.map { (remaining * it / sum).toInt() }
}
fun main() {
val sizes = listOf(40, 40, 40)
for (mode in listOf("Start", "SpaceBetween", "SpaceEvenly", "SpaceAround")) {
println(mode + ": children start at " + positions(360, sizes, mode))
}
// A 60 px icon plus two weighted texts in a 360 px Row
println("weights 1f and 2f share 300 px: " + weights(360, 60, listOf(1f, 2f)))
}Expected output:
Start: children start at [0, 40, 80]
SpaceBetween: children start at [0, 160, 320]
SpaceEvenly: children start at [60, 160, 260]
SpaceAround: children start at [40, 160, 280]
weights 1f and 2f share 300 px: [100, 200]
In production
- A settings row showed a label and a
Switchin aRow. In English it looked fine. - After translation, users with German and with large font sizes reported that the switch was missing on some rows.
- Cause: the label had no weight. Row measures unweighted children in order, so the long label took all the width and the
Switchgot zero width.
- Gave the label
Modifier.weight(1f)so it takes only the space left after theSwitch, and wraps if it is too long. - Added previews and screenshot tests with a long pseudo-locale string and a large font scale.
- Made a team rule: in a Row with one flexible child and fixed trailing content, the flexible child gets the weight.
- A hero image card at the top of a home screen showed a dark scrim over the image so the title stayed readable. After the change, the card grew to fill the whole screen height.
- The scrim was a
Box(Modifier.fillMaxSize().background(scrimColor))inside the card'sBox. - Cause: a Box sizes itself to its largest child.
fillMaxSize()asked for all the space the parent allowed, so the card grew to match.
- Replaced
fillMaxSize()on the scrim withModifier.matchParentSize(), which sizes the child to the Box after the other children are measured, without changing the Box size. - An alternative is to draw the scrim on the image itself with
Modifier.drawWithContentand skip the extra layout node. - Checked the card bounds in Layout Inspector and added a screenshot test of the list.
Common mistakes
- Forgetting weight on the flexible child. A long first child in a Row can push later children out of view. Give the flexible child
Modifier.weight(1f)so fixed items keep their size. - Using Box for a linear sequence. Box stacks children at the same position, so they overlap. Use Row or Column for sequences; keep Box for overlays.
- Calling weight outside Row or Column.
Modifier.weightis defined onRowScopeandColumnScope, so it does not compile on a child of a Box or outside a layout scope. In a Box, usefillMaxWidth(0.5f)or similar for a fraction of the size. - Using fillMaxSize for an overlay inside a wrap-content Box. The Box grows to fill all available space. Use
Modifier.matchParentSize()instead. - Confusing arrangement with alignment.
horizontalArrangement = Arrangement.Centerin a Row centres the group horizontally. It does nothing for vertical position; that isverticalAlignment. - Forgetting the defaults are Top and TopStart. A Row's children align to the top, and a Box's children sit in the top-start corner, unless you set
verticalAlignmentorcontentAlignment.
Interview questions
Answer out loud first. Then reveal the answer and compare it at each depth.
“What is the difference between arrangement and alignment in Row and Column?”
“How does Modifier.weight work in Row versus Column?”
“When would you use Box instead of Row or Column, and when would you reach for a custom Layout?”
“Does the position of Modifier.weight in a modifier chain matter?”
Quiz
What does Arrangement.SpaceBetween do with three children in a Row?
Where can you call Modifier.weight(1f)?
What is the default contentAlignment of Box?
In a Row, what does the verticalAlignment parameter control?
0 of 4 answered
Mini task
Build a product card with a Box overlay
- Create a composable
fun ProductCard(title: String, price: String, isNew: Boolean, modifier: Modifier = Modifier). - Inside, use a
ColumnwithverticalArrangement = Arrangement.spacedBy(8.dp)andModifier.fillMaxWidth().padding(12.dp). - Add a
RowwithhorizontalArrangement = Arrangement.SpaceBetweenandverticalAlignment = Alignment.CenterVertically. Placetitleat the start andpriceat the end. - Add a second
Rowwith two children:Text("In stock")withModifier.weight(1f)andText("Free shipping")withModifier.weight(1f)andtextAlign = TextAlign.End. - Wrap the Column in a
Box(modifier). Inside the Box, after the Column, addText("NEW")withModifier.align(Alignment.TopEnd).background(Color.Red).padding(4.dp), shown only whenisNewis true. - Preview the card with
isNew = true,isNew = false, and a very long title. Then give the titleModifier.weight(1f)and compare.
Done when: the title and price sit on the same line at opposite edges, the two bottom texts split the width 50/50, the "NEW" badge appears in the top-end corner only when isNew is true, and you can explain what the weight on the long title changed.