In Jetpack Compose, how do Row's weight, Arrangement and Alignment share out space, and how does Box let a badge overlap an avatar?
answer
- scoped modifiers talk to the parent
- unweighted children are measured first
- main axis arranges, cross axis aligns
- offset moves without resizing
basics
~20 sRow measures unweighted children first, then splits the leftover width by weight; Arrangement spaces children along the main axis and Alignment places them across it. Box stacks children, and BoxScope.align with offset puts a badge over the avatar's corner.
solid answer
~40 sA `Row` measures children without a weight at their preferred width, then divides the remaining width among weighted children in proportion to `weight`; with the default `fill = true` each takes exactly its share. `horizontalArrangement` (default `Arrangement.Start`, or `spacedBy`, `SpaceBetween`) spaces children along the row, and `verticalAlignment` (default `Alignment.Top`) places them across it; `Column` mirrors this. A `Box` stacks children in composition order and sizes itself to the largest one, so a badge declared after the avatar draws on top; `Modifier.align(Alignment.TopEnd)` pins it to the corner and `offset` pushes it out without changing the measured size. For chips that wrap, `FlowRow` breaks onto new lines.
code
kotlin · 44 lines@Composable
fun ProfileHeader(name: String, unread: Int, interests: List<String>, avatar: Painter) {
Column(Modifier.fillMaxWidth().padding(16.dp)) {
Row(verticalAlignment = Alignment.CenterVertically) {
Box {
Image(
painter = avatar,
contentDescription = "Profile photo",
modifier = Modifier.size(64.dp).clip(CircleShape),
)
Text(
text = unread.toString(),
color = Color.White,
modifier = Modifier
.align(Alignment.TopEnd)
.offset(x = 6.dp, y = (-6).dp)
.background(Color.Red, CircleShape)
.padding(horizontal = 6.dp),
)
}
Spacer(Modifier.width(16.dp))
Text(
text = name,
modifier = Modifier.weight(1f),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
}
FlowRow(
modifier = Modifier.padding(top = 12.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
interests.forEach { tag ->
Text(
text = tag,
modifier = Modifier
.border(1.dp, Color.Gray, RoundedCornerShape(50))
.padding(horizontal = 12.dp, vertical = 6.dp),
)
}
}
}
}go deeper
Know which parent places children in which direction, that Box stacks them in order, and that weight, align and matchParentSize only exist inside their parent's scope.
Explain the weight algorithm with numbers, the default arrangement and alignment, what fill = false changes, and why offset lets a badge overhang without resizing the Box.
Diagnose layouts that misbehave: weights ignored inside a horizontal scroll, a badge clipped or covering a neighbour, fillMaxSize inflating a Box, a FlowRow asked to hold an unbounded list.
Set the default toolkit for the team: plain Row, Column and Box first, ConstraintLayout only where relationships demand it, and shared header components instead of ad hoc offsets.
## Three layouts, three placement rules `Column` places its children top to bottom, `Row` places them start to end, and `Box` stacks them on top of each other in composition order, so a later child draws over an earlier one. By default each of them wraps its content: it is as small as its children allow, within the constraints its parent passes in. Each child can tell its parent how it wants to be treated through **scoped modifiers**: - `RowScope.weight` and `ColumnScope.weight` share out leftover space. - `RowScope.align`, `ColumnScope.align` and `BoxScope.align` override the parent's alignment for one child. - `BoxScope.matchParentSize` sizes a child to its `Box`. They exist only inside that parent's content lambda, so the compiler rejects `weight` on a child of a `Box`. The View system's layout params had no such check: a misplaced attribute was silently ignored at run time. Scoped modifiers are also called parent data modifiers, because they carry information from the child up to the parent's measure policy rather than changing the child itself. ## How a Row shares out width `Row` measures each child once, in an order that makes weights work: 1. Children **without** a weight are measured first, at the width they prefer. 2. The width left over is divided among weighted children in proportion to their `weight`, which must be positive. 3. With `fill = true`, the default, a weighted child is forced to exactly its share. With `fill = false` the share is only a maximum; the child may be narrower, and the unused width is not handed to its siblings. Worked example: a 348 dp `Row` holds a 48 dp icon and two texts with `weight(1f)` and `weight(2f)`. 300 dp remain, so the texts get 100 dp and 200 dp. Two consequences come up in interviews. A `Row` with at least one weighted child already fills the available width, so `fillMaxWidth()` on it is redundant. And inside a horizontally scrollable container the remaining space is infinite, so weights are disregarded. `Column` behaves the same way on the vertical axis. ## Arrangement and alignment | Parent | Main axis parameter (default) | Cross axis parameter (default) | Per-child override | |---|---|---|---| | `Row` | `horizontalArrangement` (`Arrangement.Start`) | `verticalAlignment` (`Alignment.Top`) | `Modifier.align(Alignment.CenterVertically)` | | `Column` | `verticalArrangement` (`Arrangement.Top`) | `horizontalAlignment` (`Alignment.Start`) | `Modifier.align(Alignment.End)` | | `Box` | none | `contentAlignment` (`Alignment.TopStart`) | `Modifier.align(Alignment.TopEnd)` | `Arrangement.spacedBy(8.dp)` puts a fixed gap between children. `SpaceBetween`, `SpaceAround` and `SpaceEvenly` distribute leftover space, so they change nothing when the parent wraps its content tightly. ## Box for overlap: the avatar badge A notification badge that overlaps the avatar's corner is the classic `Box` job: - The `Box` takes its size from its largest child, here the 64 dp avatar. - `Modifier.align(Alignment.TopEnd)` on the badge pins it to the avatar's top-end corner. - `Modifier.offset(x = 6.dp, y = (-6).dp)` pushes it outward. `offset` moves content without changing its measured size, so the `Box` stays 64 dp and the badge overhangs its bounds. Leave room beside it, or the next element in the row may sit under the overhang. - `matchParentSize()` sizes a child to the `Box` after the other children have set its size. `fillMaxSize()` would instead take all the space the `Box` is allowed and make the `Box` grow. - `propagateMinConstraints` is `false` by default, so children are measured without the `Box`'s minimum size. ## FlowRow for chips that wrap `FlowRow`, in foundation-layout, fills a line and moves to the next one when it runs out of space, which suits a profile's interest chips: - `horizontalArrangement` spaces the items within a line. - `verticalArrangement` spaces the lines themselves. - `itemVerticalAlignment` aligns items of different heights within a line. - `maxItemsInEachRow` and `maxLines` cap the shape. It is not lazy: every chip is composed. That is right for a handful of tags; a long, scrolling collection belongs in a lazy grid, which is a lists subject rather than a layout one. ## Where ConstraintLayout fits ConstraintLayout for Compose ships as its own artifact, `androidx.constraintlayout:constraintlayout-compose`, versioned apart from Compose. In the View system it was the tool for flattening deep hierarchies; Compose handles deep nesting of `Row`, `Column` and `Box` efficiently, so here it earns its place only when relative constraints, barriers or chains express a layout more clearly than nesting.
- In Compose, what changes if a Row child uses Modifier.weight(1f, fill = false) instead of weight(1f)?Its share of the leftover width becomes a maximum rather than a fixed size. The child may come out narrower than its share, so a short text stays short, and the width it does not use is not redistributed to its siblings; the `Row` simply ends up narrower. With the default `fill = true` the child is forced to exactly its share.
- When would you choose ConstraintLayout for Compose over nested Row, Column and Box?When the layout is really about relationships: an element aligned to another's edge across groups, a barrier after the widest of several labels, a chain spreading items. It ships as the separate `constraintlayout-compose` artifact with its own versions. Flattening for speed is not a reason, because Compose handles deep nesting efficiently, unlike the View system where ConstraintLayout was the performance answer.
- In a Compose Box, why use matchParentSize() rather than fillMaxSize() for a background layer behind the avatar?`matchParentSize()` gives the layer the `Box`'s size after the other children have decided it, so the `Box` still wraps the avatar. `fillMaxSize()` asks for all the space the `Box`'s parent allows, which makes the `Box` itself grow to that space and moves everything aligned inside it.
- Weight in a Compose Row divides the whole width, ignoring children that have no weight.
- A Row needs fillMaxWidth() before any weight takes effect.
- Box places children side by side unless you add offsets.
- offset() grows the Box so the overhanging badge stays inside its bounds.
- FlowRow is lazy, so it is the right choice for hundreds of tags.