Custom layouts and modifiers
Compose lays out the screen in a single pass, top to bottom:
- The parent gives each child constraints: “you can be between 0 and 360 dp wide, and up to 800 dp tall”.
- Each child measures itself within those constraints and reports its size.
- The parent places each child at an x and y.
The important rule: each child may be measured only once per layout. That’s what keeps Compose fast even with deep trees. If you need “all cards as tall as the tallest”, you can’t measure them all, find the tallest, and measure again.
Compose gives you tools for that:
Modifier.height(IntrinsicSize.Min)on aRowuses an intrinsic measurementIntrinsic measurement (Compose)Asking a composable how big it would need to be, before measuring it, so siblings can be sized to each other.Open in glossary →: it asks the children “how tall would you need to be?” before measuring them, then gives all of them that height.Layout { }lets you write your own arrangement whenRow,ColumnandBoxaren’t enough.FlowRowalready wraps chips onto several lines, so you don’t need to write that one yourself.
Layout is a single pass: Constraints flow down, sizes flow up, then placement. Measuring a Measurable twice in one pass throws, which guarantees linear-time layout. Layout(content) { measurables, constraints -> } measures children (usually with relaxed constraints, constraints.copy(minWidth = 0)) and returns layout(width, height) { placeable.place(x, y) }; Modifier.layout { measurable, constraints -> } does the same for a single element. Intrinsic measurements (minIntrinsicHeight and friends, via IntrinsicSize.Min/Max) answer “how big would you be” without a real measure, at the cost of an extra traversal. SubcomposeLayout (behind BoxWithConstraints, LazyColumn, Scaffold) composes content during layout, when its content depends on measured sizes; it can’t skip like normal composition, so it’s expensive inside list items. For reusable behavior, write Modifier.Node modifiers (ModifierNodeElement + LayoutModifierNode/DrawModifierNode/PointerInputModifierNode): they’re allocated once and updated, comparable for skipping, unlike composed {}. CompositionLocal is for implicit, tree-scoped values (theme, density, a tracking context), not for passing dependencies that should be explicit parameters.
// All cards as tall as the tallest one, without measuring twice
@Composable
fun FeatureRow(features: List<Feature>) {
Row(Modifier.height(IntrinsicSize.Min), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
features.forEach { feature ->
FeatureCard(feature, Modifier.weight(1f).fillMaxHeight())
}
}
}
// A custom layout: a label and a price, with the price aligned to the end on the label's last baseline
@Composable
fun LabelWithPrice(label: @Composable () -> Unit, price: @Composable () -> Unit, modifier: Modifier = Modifier) {
Layout(contents = listOf(label, price), modifier = modifier) { (labelMeasurables, priceMeasurables), constraints ->
val pricePlaceable = priceMeasurables.first().measure(constraints.copy(minWidth = 0))
val labelPlaceable = labelMeasurables.first().measure(
constraints.copy(minWidth = 0, maxWidth = constraints.maxWidth - pricePlaceable.width)
)
val height = maxOf(labelPlaceable.height, pricePlaceable.height)
layout(constraints.maxWidth, height) {
labelPlaceable.place(0, 0)
val baselineOffset = labelPlaceable[LastBaseline] - pricePlaceable[LastBaseline]
pricePlaceable.place(constraints.maxWidth - pricePlaceable.width, baselineOffset.coerceAtLeast(0))
}
}
}
// A reusable modifier as a Modifier.Node: draws a dashed outline
fun Modifier.dashedBorder(color: Color, width: Dp = 1.dp) = this then DashedBorderElement(color, width)
private data class DashedBorderElement(val color: Color, val width: Dp) : ModifierNodeElement<DashedBorderNode>() {
override fun create() = DashedBorderNode(color, width)
override fun update(node: DashedBorderNode) {
node.color = color
node.width = width
node.invalidateDraw()
}
}
private class DashedBorderNode(var color: Color, var width: Dp) : Modifier.Node(), DrawModifierNode {
override fun ContentDrawScope.draw() {
drawContent()
drawRect(color, style = Stroke(width.toPx(), pathEffect = PathEffect.dashPathEffect(floatArrayOf(12f, 8f))))
}
}IntrinsicSize.Minmakes theRowas tall as its tallest card needs to be;fillMaxHeight()makes every card use that height.Layout(contents = …)measures the price first, gives the label the remaining width, then places them on the same baseline. Each child is measured exactly once.DashedBorderElementis a small data class: Compose compares it between recompositions, and if nothing changed, it does nothing. TheNodedoes the drawing and lives as long as the modifier stays on the screen.
Tap the line with the bug.
@Composable
fun EqualColumns(content: @Composable () -> Unit) {
Layout(content) { measurables, constraints ->
val tallest = measurables.maxOf { it.measure(constraints).height }
val placeables = measurables.map {
it.measure(constraints.copy(minHeight = tallest, maxHeight = tallest))
}
layout(constraints.maxWidth, tallest) {
var x = 0
placeables.forEach { it.place(x, 0); x += it.width }
}
}
}Hint: How many times is each measurable measured?
Each child is measured twice: once to find the tallest, once at that height. Compose throws “measure() may not be called multiple times on the same Measurable”, because double measurement is how layouts become exponentially slow in deep trees.
Ask for the intrinsic height instead, which doesn’t count as measuring:
Layout(content) { measurables, constraints ->
val tallest = measurables.maxOf { it.maxIntrinsicHeight(constraints.maxWidth) }
val placeables = measurables.map { it.measure(constraints.copy(minHeight = tallest, maxHeight = tallest)) }
layout(constraints.maxWidth, tallest) {
var x = 0
placeables.forEach { it.place(x, 0); x += it.width }
}
}Or, simpler, use a Row with Modifier.height(IntrinsicSize.Min) as above.
Choosing the right layout tool
| Need | Tool | Cost |
|---|---|---|
| Standard arrangements, wrapping chips | Row, Column, Box, FlowRow, FlowColumn | Lowest |
| Children sized to each other (equal heights) | IntrinsicSize.Min/Max | One extra intrinsic pass |
| A custom arrangement | Layout, Modifier.layout | Same as built-ins |
| Content that depends on available size | BoxWithConstraints, SubcomposeLayout | Composition during layout, no skipping |
| Behavior reused across many composables | Modifier.Node | Allocated once, comparable |
BoxWithConstraints at the screen level (choose a two-pane layout) is fine; in every list item it adds subcomposition to each item’s measure, which is visible in scroll performance. Window size classes (currentWindowAdaptiveInfo()) answer most “which layout?” questions without it.
- Single-pass layout: constraints down, sizes up, place. Measuring a measurable twice throws.
Layout { measurables, constraints -> layout(w, h) { place } },Modifier.layout; relaxminWidthfor children.- Intrinsics (
IntrinsicSize.Min/Max,maxIntrinsicHeight) for size-to-siblings; extra traversal, no double measure. SubcomposeLayout/BoxWithConstraintscompose during layout and don’t skip: avoid in list items; prefer window size classes.Modifier.Node(ModifierNodeElement+ node interfaces) overcomposed {};CompositionLocalfor implicit tree values, not dependencies.
Check this part
How many times may a custom Layout measure each child?
How do you make all cards in a Row as tall as the tallest?
Why is BoxWithConstraints in every LazyColumn item a scroll performance risk?
Why prefer Modifier.Node over composed {} for custom modifiers?