Guides

Adaptive layouts

An adaptive app changes its layout with the window size: a bar at the bottom on a phone, a rail at the side on a tablet, two panes on a desktop. On desktop, the user can resize the window at any time, so this matters there too.

Example

@Composable
fun App() {
    val sizeClass = currentWindowAdaptiveInfoV2().windowSizeClass
    if (sizeClass.isWidthAtLeastBreakpoint(WindowSizeClass.WIDTH_DP_MEDIUM_LOWER_BOUND)) {
        RailLayout()
    } else {
        BarLayout()
    }
}

Setup

The adaptive APIs are in separate libraries. Add them in build.gradle.kts:

commonMain.dependencies {
    implementation("org.jetbrains.compose.material3.adaptive:adaptive:1.3.0")            // size classes
    implementation("org.jetbrains.compose.material3.adaptive:adaptive-layout:1.3.0")     // pane layouts
    implementation("org.jetbrains.compose.material3.adaptive:adaptive-navigation:1.3.0") // navigators
}

Most of it is experimental. Add @OptIn(ExperimentalMaterial3AdaptiveApi::class) where you use it.

Window size classes

Don't use exact pixel or dp widths in your layout code. Use a window size class: a small number of size groups that all apps share.

Width class Window width Typical device
Compact under 600 dp Phone held upright
Medium 600 โ€“ 839 dp Tablet held upright, foldable
Expanded 840 โ€“ 1199 dp Tablet held sideways, small desktop window
Large 1200 โ€“ 1599 dp Desktop
Extra-large 1600 dp and more Large desktop

Heights have classes too: compact under 480 dp, medium up to 899 dp, expanded from 900 dp.

Get the current class with currentWindowAdaptiveInfoV2. It updates when the window changes size.

val sizeClass = currentWindowAdaptiveInfoV2().windowSizeClass

Compare it with a breakpoint. "At least" means: this size or bigger.

sizeClass.isWidthAtLeastBreakpoint(WindowSizeClass.WIDTH_DP_MEDIUM_LOWER_BOUND)    // 600 dp or wider
sizeClass.isWidthAtLeastBreakpoint(WindowSizeClass.WIDTH_DP_EXPANDED_LOWER_BOUND)  // 840 dp or wider
sizeClass.isHeightAtLeastBreakpoint(WindowSizeClass.HEIGHT_DP_MEDIUM_LOWER_BOUND)  // 480 dp or taller

Use currentWindowAdaptiveInfoV2, not currentWindowAdaptiveInfo. The old one has no large and extra-large classes, and is deprecated.

Bar or rail

The most common change: a NavigationBar at the bottom on compact windows, a NavigationRail at the side on wider ones.

Read the size class once, at the top of your app, and pass it down. Then your layouts are easy to preview and test with any size.

A narrow window: navigation bar at the bottom
A narrow window: navigation bar at the bottom
// In an app: val sizeClass = currentWindowAdaptiveInfoV2().windowSizeClass
val sizeClass = WindowSizeClass.BREAKPOINTS_V2.computeWindowSizeClass(widthDp = 360, heightDp = 640)
Box(Modifier.size(360.dp, 400.dp)) { AppLayout(sizeClass) }

/** Bar at the bottom on narrow windows, rail at the side on wider ones. */
@Composable
fun AppLayout(sizeClass: WindowSizeClass) {
    var selected by remember { mutableIntStateOf(0) }
    val items = listOf("Home" to Icons.Filled.Home, "Favorites" to Icons.Filled.Favorite, "Settings" to Icons.Filled.Settings)
    val content = @Composable { modifier: Modifier ->
        Box(modifier.fillMaxSize().background(MaterialTheme.colorScheme.surfaceContainerLow), contentAlignment = Alignment.Center) {
            Text(items[selected].first)
        }
    }
    if (sizeClass.isWidthAtLeastBreakpoint(WindowSizeClass.WIDTH_DP_MEDIUM_LOWER_BOUND)) {
        Row {
            NavigationRail {
                items.forEachIndexed { i, (label, icon) ->
                    NavigationRailItem(selected = selected == i, onClick = { selected = i }, icon = { Icon(icon, null) }, label = { Text(label) })
                }
            }
            content(Modifier.weight(1f))
        }
    } else {
        Column {
            content(Modifier.weight(1f))
            NavigationBar {
                items.forEachIndexed { i, (label, icon) ->
                    NavigationBarItem(selected = selected == i, onClick = { selected = i }, icon = { Icon(icon, null) }, label = { Text(label) })
                }
            }
        }
    }
}
A wider window: navigation rail at the side
A wider window: navigation rail at the side
// In an app: val sizeClass = currentWindowAdaptiveInfoV2().windowSizeClass
val sizeClass = WindowSizeClass.BREAKPOINTS_V2.computeWindowSizeClass(widthDp = 600, heightDp = 480)
Box(Modifier.size(600.dp, 320.dp)) { AppLayout(sizeClass) }

/** Bar at the bottom on narrow windows, rail at the side on wider ones. */
@Composable
fun AppLayout(sizeClass: WindowSizeClass) {
    var selected by remember { mutableIntStateOf(0) }
    val items = listOf("Home" to Icons.Filled.Home, "Favorites" to Icons.Filled.Favorite, "Settings" to Icons.Filled.Settings)
    val content = @Composable { modifier: Modifier ->
        Box(modifier.fillMaxSize().background(MaterialTheme.colorScheme.surfaceContainerLow), contentAlignment = Alignment.Center) {
            Text(items[selected].first)
        }
    }
    if (sizeClass.isWidthAtLeastBreakpoint(WindowSizeClass.WIDTH_DP_MEDIUM_LOWER_BOUND)) {
        Row {
            NavigationRail {
                items.forEachIndexed { i, (label, icon) ->
                    NavigationRailItem(selected = selected == i, onClick = { selected = i }, icon = { Icon(icon, null) }, label = { Text(label) })
                }
            }
            content(Modifier.weight(1f))
        }
    } else {
        Column {
            content(Modifier.weight(1f))
            NavigationBar {
                items.forEachIndexed { i, (label, icon) ->
                    NavigationBarItem(selected = selected == i, onClick = { selected = i }, icon = { Icon(icon, null) }, label = { Text(label) })
                }
            }
        }
    }
}

The pictures make the size class by hand with WindowSizeClass.BREAKPOINTS_V2.computeWindowSizeClass(widthDp, heightDp). That is also how you test a layout at a given size. Don't use the WindowSizeClass(minWidthDp, minHeightDp) constructor for this: it doesn't round to a breakpoint, so the pane layouts below treat 360 dp as a huge window.

There is also a ready-made NavigationSuiteScaffold that switches between bar, rail and drawer for you. It is in another library (material3-adaptive-navigation-suite), and not covered here yet.

Pane layouts

On wide windows, show more than one thing at once. Two layouts do this for you:

On a wide window: list and detail side by side
On a wide window: list and detail side by side
val messages = listOf("Lunch on Friday?", "Your order has shipped", "Photos from the trip", "Meeting notes")

Box(Modifier.size(840.dp, 300.dp)) { Inbox(windowWidthDp = 840) }

/** A list of messages and the selected one. Works with one pane or two. */
@Composable
fun Inbox(windowWidthDp: Int) {
    val scope = rememberCoroutineScope()
    // The navigator decides which panes show. In an app, call it without arguments:
    // it reads the window size itself.
    val navigator = rememberListDetailPaneScaffoldNavigator<Int>(
        scaffoldDirective = calculatePaneScaffoldDirective(
            WindowAdaptiveInfo(WindowSizeClass.BREAKPOINTS_V2.computeWindowSizeClass(widthDp = windowWidthDp, heightDp = 480), Posture()),
        ),
    )
    val selected = navigator.currentDestination?.contentKey

    ListDetailPaneScaffold(
        directive = navigator.scaffoldDirective,
        scaffoldState = navigator.scaffoldState,
        listPane = {
            AnimatedPane {
                LazyColumn {
                    itemsIndexed(messages) { index, message ->
                        ListItem(
                            headlineContent = { Text(message) },
                            colors = ListItemDefaults.colors(
                                containerColor = if (index == selected) MaterialTheme.colorScheme.secondaryContainer
                                else MaterialTheme.colorScheme.surface,
                            ),
                            modifier = Modifier.clickable {
                                scope.launch { navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, index) }
                            },
                        )
                    }
                }
            }
        },
        detailPane = {
            AnimatedPane {
                Column(Modifier.fillMaxSize().background(MaterialTheme.colorScheme.surfaceContainerLow).padding(16.dp)) {
                    // Only needed when the list is hidden (one pane).
                    if (navigator.canNavigateBack()) {
                        IconButton(onClick = { scope.launch { navigator.navigateBack() } }) {
                            Icon(Icons.AutoMirrored.Filled.ArrowBack, "Back")
                        }
                    }
                    Text(selected?.let { messages[it] } ?: "Pick a message", style = MaterialTheme.typography.titleLarge)
                }
            }
        },
    )
}

On a phone, the same code shows one pane at a time:

On a phone: only the list shows
On a phone: only the list shows
val messages = listOf("Lunch on Friday?", "Your order has shipped", "Photos from the trip", "Meeting notes")

Box(Modifier.size(360.dp, 300.dp)) { Inbox(windowWidthDp = 360) }

/** A list of messages and the selected one. Works with one pane or two. */
@Composable
fun Inbox(windowWidthDp: Int) {
    val scope = rememberCoroutineScope()
    // The navigator decides which panes show. In an app, call it without arguments:
    // it reads the window size itself.
    val navigator = rememberListDetailPaneScaffoldNavigator<Int>(
        scaffoldDirective = calculatePaneScaffoldDirective(
            WindowAdaptiveInfo(WindowSizeClass.BREAKPOINTS_V2.computeWindowSizeClass(widthDp = windowWidthDp, heightDp = 480), Posture()),
        ),
    )
    val selected = navigator.currentDestination?.contentKey

    ListDetailPaneScaffold(
        directive = navigator.scaffoldDirective,
        scaffoldState = navigator.scaffoldState,
        listPane = {
            AnimatedPane {
                LazyColumn {
                    itemsIndexed(messages) { index, message ->
                        ListItem(
                            headlineContent = { Text(message) },
                            colors = ListItemDefaults.colors(
                                containerColor = if (index == selected) MaterialTheme.colorScheme.secondaryContainer
                                else MaterialTheme.colorScheme.surface,
                            ),
                            modifier = Modifier.clickable {
                                scope.launch { navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, index) }
                            },
                        )
                    }
                }
            }
        },
        detailPane = {
            AnimatedPane {
                Column(Modifier.fillMaxSize().background(MaterialTheme.colorScheme.surfaceContainerLow).padding(16.dp)) {
                    // Only needed when the list is hidden (one pane).
                    if (navigator.canNavigateBack()) {
                        IconButton(onClick = { scope.launch { navigator.navigateBack() } }) {
                            Icon(Icons.AutoMirrored.Filled.ArrowBack, "Back")
                        }
                    }
                    Text(selected?.let { messages[it] } ?: "Pick a message", style = MaterialTheme.typography.titleLarge)
                }
            }
        },
    )
}

Both work with a navigator, a ThreePaneScaffoldNavigator. It reads the window size, keeps track of what is open and how to go back, and tells the scaffold which panes to show. You never write "if wide" yourself.

val navigator = rememberListDetailPaneScaffoldNavigator<Int>()
val scope = rememberCoroutineScope()

// When the user picks an item:
scope.launch { navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, itemId) }

// The open item:
val openId = navigator.currentDestination?.contentKey

Each pane's content goes inside an AnimatedPane, so it animates when panes appear and disappear.

Back

On a phone, the detail pane covers the list. The user needs a way back:

For the system back, use NavigationBackHandler from org.jetbrains.androidx.navigationevent:navigationevent-compose:

NavigationBackHandler(
    state = rememberNavigationEventState(NavigationEventInfo.None),
    isBackEnabled = navigator.canNavigateBack(),
    onBackCompleted = { scope.launch { navigator.navigateBack() } },
)

The older BackHandler from androidx.compose.ui.backhandler still works, but is deprecated.

Foldables

WindowAdaptiveInfo also has a windowPosture: whether a foldable is half open like a laptop (isTabletop), and where its hinge is. The pane layouts already keep panes off the hinge. You only need it for your own layouts.

Summary

You want Write
The size class currentWindowAdaptiveInfoV2().windowSizeClass
Check "tablet or bigger" sizeClass.isWidthAtLeastBreakpoint(WindowSizeClass.WIDTH_DP_MEDIUM_LOWER_BOUND)
Bar or rail if (...) NavigationRail { } else NavigationBar { }
List and detail ListDetailPaneScaffold + rememberListDetailPaneScaffoldNavigator()
Content with a side pane SupportingPaneScaffold + rememberSupportingPaneScaffoldNavigator()
A size class for a test or preview WindowSizeClass.BREAKPOINTS_V2.computeWindowSizeClass(360, 640)