Skip to content

Jetpack Compose Custom Layout — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Jetpack Compose Custom Layout. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

Your custom Layout composable measures children at the wrong size, places them off-screen, or doesn't respect incoming constraints.

Wrong Approach ❌

@Composable
fun BadLayout(modifier: Modifier = Modifier, content: @Composable () -> Unit) {
    Layout(content = content, modifier = modifier) { measurables, constraints ->
        // Ignores constraints — uses hardcoded sizes
        val placeables = measurables.map { measurable ->
            measurable.measure(Constraints(0, 100, 0, 100))
        }
        layout(width = 100, height = 100) {
            placeables.forEach { it.place(0, 0) } // All stacked — no arrangement
        }
    }
}

Output: Items forced to wrong size, stacked on top of each other.

Right Approach ✅

@Composable
fun FlowRowLayout(
    modifier: Modifier = Modifier,
    horizontalGap: Dp = 8.dp,
    verticalGap: Dp = 8.dp,
    content: @Composable () -> Unit
) {
    val hGapPx = with(LocalDensity.current) { horizontalGap.toPx() }
    val vGapPx = with(LocalDensity.current) { verticalGap.toPx() }

    Layout(content = content, modifier = modifier) { measurables, constraints ->
        val placeables = measurables.map { it.measure(constraints.copy(minWidth = 0)) }
        var x = 0
        var y = 0
        var rowHeight = 0
        val totalWidth = constraints.maxWidth

        layout(width = totalWidth, height = y + placeables.maxOfOrNull { it.height } ?: 0) {
            placeables.forEach { placeable ->
                if (x + placeable.width > totalWidth) {
                    x = 0; y += rowHeight + vGapPx.toInt(); rowHeight = 0
                }
                placeable.place(x, y)
                x += placeable.width + hGapPx.toInt()
                rowHeight = maxOf(rowHeight, placeable.height)
            }
        }
    }
}

Output: Properly measured and placed items with wrapping flow behavior.

Prevention

  • Always respect incoming constraints — clamp sizes to constraints.minWidth/maxWidth.
  • Use Layout composable for custom arrangement, SubcomposeLayout for async children.
  • Convert Dp to pixels using LocalDensity.current.
  • Call place() in the correct coordinate space (relative to parent layout).

Common Mistakes with compose custom layout

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between Layout and SubcomposeLayout?

Layout measures all children with the same constraints. SubcomposeLayout lets you subcompose children based on previous measurements (e.g., FlowRow collapsing overflow items).

### How do I match one child's size to another in custom layout?

Measure the first child, then use its size to create constraints for the second child. This is a two-pass measurement pattern.

### Can I reuse standard Layout implementations?

Yes. Compose provides Row, Column, Box, FlowRow, ConstraintLayout out of the box. Only write a custom Layout when these don't meet your needs.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro