|
| 1 | +--- |
| 2 | +name: adaptive |
| 3 | +description: Instructions to make or update an app's UI so that it adapts to different |
| 4 | + Android devices including phones, tablets, foldables, laptops, desktop, TV, Auto |
| 5 | + and XR. It includes how to handle different window sizes, pointing devices (such |
| 6 | + as mouse) and text entry devices (such as keyboard) using the Compose MediaQuery |
| 7 | + API. It also covers multi-pane layouts using Navigation3 Scenes, adaptive UI components |
| 8 | + (such as buttons) with varying target sizes, and adaptive layouts (including navigation |
| 9 | + areas - nav rails and nav bars) using the Compose Grid and FlexBox APIs. |
| 10 | +license: Complete terms in LICENSE.txt |
| 11 | +metadata: |
| 12 | + author: Google LLC |
| 13 | + last-updated: '2026-05-20' |
| 14 | + keywords: |
| 15 | + - android |
| 16 | + - ui |
| 17 | + - adaptive |
| 18 | + - Grid |
| 19 | + - FlexBox |
| 20 | + - MediaQuery |
| 21 | + - navigation |
| 22 | +--- |
| 23 | + |
| 24 | +## Prerequisites |
| 25 | + |
| 26 | +The app must: |
| 27 | + |
| 28 | +- Use Compose for all screens. If it's still using Fragments or Views, suggest using the XML to Compose skill to migrate those screens. |
| 29 | +- Use Jetpack Navigation 3. If it doesn't, suggest the Navigation 3 skill to migrate the app. |
| 30 | + |
| 31 | +## Workflow to make an app adaptive |
| 32 | + |
| 33 | +To make an app adaptive, follow these steps or a subset of them adapting to the |
| 34 | +task. |
| 35 | + |
| 36 | +- Step 1: Verify current UI |
| 37 | +- Step 2: Make the navigation bar adaptive |
| 38 | +- Step 3: Add multi-pane layouts |
| 39 | +- Step 4: Make vertical lists adaptive by changing the number of columns |
| 40 | +- Step 5: Hide app bars when scrolling |
| 41 | + |
| 42 | +## Step 1. Verify current UI |
| 43 | + |
| 44 | +Ensure that screenshot tests exist to verify the current UI on different form |
| 45 | +factors. If they don't exist, add the [Compose Preview Screenshot Testing |
| 46 | +tool](references/android/develop/ui/compose/tooling/debug.md). Use the following annotation to create previews for all the major form |
| 47 | +factors. For example: |
| 48 | + |
| 49 | + |
| 50 | +```kotlin |
| 51 | +@Preview(name = "Phone", device = Devices.PHONE, showBackground = true) |
| 52 | +@Preview(name = "Foldable", device = Devices.FOLDABLE, showBackground = true) |
| 53 | +@Preview(name = "Tablet", device = Devices.TABLET, showBackground = true) |
| 54 | +@Preview(name = "Desktop", device = Devices.DESKTOP, showBackground = true) |
| 55 | +annotation class FormFactorPreviews |
| 56 | + |
| 57 | +@PreviewTest |
| 58 | +@FormFactorPreviews |
| 59 | +@Composable |
| 60 | +fun FeedScreenPreview() { |
| 61 | + SnippetsTheme { |
| 62 | + Box { |
| 63 | + Text("My Screen") |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +<br /> |
| 70 | + |
| 71 | +## Step 2. Make the navigation bar adaptive |
| 72 | + |
| 73 | +Bottom navigation bars are optimized for touch input when the user is holding a |
| 74 | +phone in portrait mode. On larger screen hand-held devices, like tablets and |
| 75 | +unfolded foldables, the navigation area must be accessible from the edge of the |
| 76 | +screen (navigation rail). |
| 77 | + |
| 78 | +If you need to provide more screen real state for the content, hide the |
| 79 | +navigation area. Examples of this include: |
| 80 | + |
| 81 | +- Hiding the navigation bar when the user scrolls down and showing it again when the user scrolls up. The assumption is that when the user is scrolling down, they are consuming content but when scrolling up they are trying to navigate away from that content. |
| 82 | +- Hiding the navigation area when its content is distracting. For example, in camera previews or when the content is best displayed in full screen (such as a single photo screen). |
| 83 | + |
| 84 | +When the detail screen is displayed full-screen on mobile, full-screen mode must |
| 85 | +be deactivated on larger screens. |
| 86 | + |
| 87 | +Steps to migrate: |
| 88 | + |
| 89 | +- Locate the existing navigation bar. |
| 90 | +- Convert each item to a `NavigationSuiteItem`. |
| 91 | +- Identify whether the navigation bar's visibility changes. For example, if it is wrapped with an `AnimatedContent` or `AnimatedVisibility` composable. If so, follow the guidance in the "Control navigation area visibility". |
| 92 | +- Replace the container that held the navigation bar (often a `Scaffold`) with `NavigationSuiteScaffold` from the Material 3 adaptive layouts library. |
| 93 | +- Supply the navigation items using the `navigationItems` parameter of `NavigationSuiteScaffold`. |
| 94 | + |
| 95 | +### Step 2.1. Control navigation area visibility |
| 96 | + |
| 97 | +If the navigation bar's visibility changes - it is hidden under certain |
| 98 | +scenarios or on certain screens - this behavior must be maintained with the |
| 99 | +adaptive navigation area. This is done using `NavigationSuiteScaffold`'s `state` |
| 100 | +parameter. |
| 101 | + |
| 102 | +Steps to migrate: |
| 103 | + |
| 104 | +- Identify the scenarios under which the navigation bar is hidden. This is usually done with a boolean variable for the visibility. It could be named something like `isNavBarVisible` or `shouldShowNavBar`. |
| 105 | +- Create an instance of `NavigationSuiteScaffoldState` using `rememberNavigationSuiteScaffoldState()` and pass it to `NavigationSuiteScaffold`. |
| 106 | +- When the navigation area visibility changes, use a `LaunchedEffect` to call `show` or `hide` on the `NavigationSuiteScaffoldState`. |
| 107 | + |
| 108 | +For example: |
| 109 | + |
| 110 | + |
| 111 | +```kotlin |
| 112 | +// Pass this variable to any composable that needs to control the navigation area visibility |
| 113 | +var isNavBarVisible by remember { mutableStateOf(true) } |
| 114 | +val scaffoldVisibilityState = rememberNavigationSuiteScaffoldState() |
| 115 | + |
| 116 | +NavigationSuiteScaffold( |
| 117 | + navigationSuiteItems = navItems, |
| 118 | + state = scaffoldVisibilityState |
| 119 | +) { |
| 120 | + // Main content |
| 121 | +} |
| 122 | + |
| 123 | +LaunchedEffect(isNavBarVisible){ |
| 124 | + if (isNavBarVisible) { |
| 125 | + scaffoldVisibilityState.show() |
| 126 | + } else { |
| 127 | + scaffoldVisibilityState.hide() |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +<br /> |
| 133 | + |
| 134 | +## Step 3. Add multi-pane layouts using Navigation 3 Scenes |
| 135 | + |
| 136 | +Analyze the codebase looking for related screens - tapping on something in one |
| 137 | +screen opens another screen that shows information related to the first. There |
| 138 | +are two canonical screen relationships: list-detail and supporting pane. |
| 139 | + |
| 140 | +IMPORTANT: You must use the Navigation 3 `SceneStrategy` approach to implement |
| 141 | +multi-pane layouts. Do not use `ListDetailPaneScaffold` or |
| 142 | +`SupportingPaneScaffold`. |
| 143 | + |
| 144 | +### Step 3.1. List-detail |
| 145 | + |
| 146 | +#### Identify the list and detail screens |
| 147 | + |
| 148 | +List-detail layouts display a list of items (this is the list screen) and |
| 149 | +clicking on an item opens a new screen that shows more details about that item |
| 150 | +(the detail screen). |
| 151 | + |
| 152 | +Typical usage includes productivity apps like email, notes, and messaging. |
| 153 | + |
| 154 | +Unless requested explicitly, avoid this pattern when the detail content requires |
| 155 | +substantial screen space (e.g., images or media that benefits from a full-screen |
| 156 | +presentation). |
| 157 | + |
| 158 | +#### Add a Material list-detail SceneStrategy |
| 159 | + |
| 160 | +- Add the `androidx.compose.material3.adaptive:adaptive-navigation3` library |
| 161 | +- Create an `androidx.compose.material3.adaptive.navigation3.ListDetailSceneStrategy` using `rememberListDetailSceneStrategy` |
| 162 | +- Pass the `ListDetailSceneStrategy` to `NavDisplay` using its `sceneStrategies` parameter |
| 163 | + |
| 164 | +#### Use metadata to identify the list and detail screens |
| 165 | + |
| 166 | +- Add metadata using `entry(metadata = ...)` or `NavEntry(metadata = ...)` to the list entry using `ListDetailSceneStrategy.listPane(detailPlaceholder = { |
| 167 | + <placeholder composable> })`. |
| 168 | +- Use the `detailPlaceholder` parameter to add a placeholder on the detail screen when no list items are selected. |
| 169 | +- Add metadata to the detail entry using `ListDetailSceneStrategy.detailPane()`. |
| 170 | + |
| 171 | +#### Important considerations |
| 172 | + |
| 173 | +- When a detail screen displays its content full-screen on mobile (content fills the entire screen, bars or rails are hidden), full-screen mode must be deactivated if it's part of a list-detail layout. |
| 174 | +- Detail screens must not show a back arrow when on a list-detail layout. |
| 175 | + |
| 176 | +For a reference implementation, check the [Nav3 **Material** List Detail |
| 177 | +recipe](references/android/guide/navigation/navigation-3/recipes/material-listdetail.md). |
| 178 | + |
| 179 | +### Step 3.2. Supporting pane |
| 180 | + |
| 181 | +Identify supporting pane screens where a main screen displays a single item, and |
| 182 | +selecting it opens a "supporting screen" with more details. The supporting |
| 183 | +screen complements the main screen and is shown in a supporting pane. |
| 184 | + |
| 185 | +#### Add a Material supporting pane `SceneStrategy` |
| 186 | + |
| 187 | +- If you haven't already, add the `androidx.compose.material3.adaptive:adaptive-navigation3` library |
| 188 | +- Create an `androidx.compose.material3.adaptive.navigation3.SupportingPaneSceneStrategy` using `rememberSupportingPaneSceneStrategy` |
| 189 | +- Pass the `SupportingPaneSceneStrategy` to `NavDisplay` using its `sceneStrategies` parameter |
| 190 | + |
| 191 | +#### Use metadata to identify the main and supporting screens |
| 192 | + |
| 193 | +- Add metadata using `entry(metadata = ...)` or `NavEntry(metadata = ...)` to the main entry using `SupportingPaneSceneStrategy.mainPane()` |
| 194 | +- Add metadata to the supporting entry using `SupportingPaneSceneStrategy.supportingPane()` |
| 195 | + |
| 196 | +### Step 3.3. Run screenshot tests |
| 197 | + |
| 198 | +If you have made changes, record new reference files. Ask the user to visually |
| 199 | +verify that the new layouts are correct. |
| 200 | + |
| 201 | +## Step 4. Make vertical lists adaptive by changing the number of columns |
| 202 | + |
| 203 | +### Step 4.1. Make lazy lists adaptive |
| 204 | + |
| 205 | +Look for the following vertical list composables: `LazyColumn`, |
| 206 | +`LazyVerticalGrid`, `LazyVerticalStaggeredGrid`. |
| 207 | + |
| 208 | +Steps to migrate: |
| 209 | + |
| 210 | +- Choose a suitable minimum width in dp for the column. It should be large enough so that item is clearly visible to the user. |
| 211 | +- For `LazyColumn`: change to a `LazyVerticalGrid` and follow the instruction below |
| 212 | +- For `LazyVerticalGrid`: change the `columns` parameter to use `GridCells.Adaptive(<width>.dp)` |
| 213 | +- For `LazyVerticalStaggeredGrid`: change the `columns` parameter to use `StaggeredGridCells.Adaptive(<width>.dp)` |
| 214 | + |
| 215 | +### Step 4.2. Migrate non-lazy lists to Grid |
| 216 | + |
| 217 | +WARNING: Grid is an experimental API available from Compose 1.11.0-beta01. |
| 218 | +Confirm with the user that they are happy to use an experimental API in their |
| 219 | +codebase. |
| 220 | + |
| 221 | +Look for any `Column` that contains multiple items of the same type and replace |
| 222 | +it with `Grid`. Do not replace it with `LazyVerticalGrid` or any other lazy |
| 223 | +layout. Do not place `Grid` inside the existing `Column`. Completely replace it. |
| 224 | + |
| 225 | +`Grid` is configured by supplying a lambda (an extension function on |
| 226 | +`GridConfigurationScope`) to its `config` parameter. Inside the lambda, |
| 227 | +`constraints` provides the minimum and maximum dimensions of the grid container |
| 228 | +and can be used to change the number of rows and columns based on the available |
| 229 | +size. For example, the following code configures `Grid` such that when the |
| 230 | +available width is: |
| 231 | + |
| 232 | +- less than 800dp, a 2x4 grid is used |
| 233 | +- 800dp or more, a 4x2 grid is used |
| 234 | + |
| 235 | + |
| 236 | +```kotlin |
| 237 | +Grid( |
| 238 | + config = { |
| 239 | + val maxWidthDp = constraints.maxWidth.toDp() |
| 240 | + val (cols, rows) = if (maxWidthDp < 800.dp){ |
| 241 | + 2 to 4 |
| 242 | + } else{ |
| 243 | + 4 to 2 |
| 244 | + } |
| 245 | + |
| 246 | + val gapSizeDp = 8.dp |
| 247 | + val cellSize = ((maxWidthDp - (gapSizeDp * (cols - 1))) / cols).coerceAtLeast(0.dp) |
| 248 | + repeat(cols) { column(cellSize) } |
| 249 | + repeat(rows) { row(cellSize) } |
| 250 | + gap(gapSizeDp) |
| 251 | + } |
| 252 | +) { /** items **/ } |
| 253 | +``` |
| 254 | + |
| 255 | +<br /> |
| 256 | + |
| 257 | +`Grid` is an experimental API so add the `@OptIn(ExperimentalGridApi::class)` |
| 258 | +annotation to any function that uses it. |
| 259 | + |
| 260 | +## Step 5: Hide App Bars when scrolling |
| 261 | + |
| 262 | +In an app with multiple top-level destinations, each screen must manage its own |
| 263 | +app bar state independently. There are two main scroll behaviors: |
| 264 | + |
| 265 | +- `exitUntilCollapsedScrollBehavior`: Hides on scroll down, stays hidden while you scroll up until you reach the very top (0 offset). |
| 266 | +- `enterAlwaysScrollBehavior`: Hides on scroll down, shows immediately on scroll up. |
| 267 | + |
| 268 | +## Final step: Build and test |
| 269 | + |
| 270 | +Build the app and run the local tests. If the project has screenshot tests, run |
| 271 | +them but DO NOT update the reference images. Prompt the user to do this after |
| 272 | +they have viewed the screenshot diffs. |
| 273 | + |
| 274 | +## Additional documentation for experimental adaptive APIs |
| 275 | + |
| 276 | +The following APIs are available from Compose 1.11.0-beta01. |
| 277 | + |
| 278 | +### FlexBox |
| 279 | + |
| 280 | +Check the FlexBox documentation: |
| 281 | + |
| 282 | +- [Overview](references/android/develop/ui/compose/layouts/adaptive/flexbox/index.md) |
| 283 | +- [Get started - setup](references/android/develop/ui/compose/layouts/adaptive/flexbox/get-started.md) |
| 284 | +- [Set container behavior](references/android/develop/ui/compose/layouts/adaptive/flexbox/container-behavior.md) |
| 285 | +- [Set item behavior](references/android/develop/ui/compose/layouts/adaptive/flexbox/item-behavior.md) |
| 286 | + |
| 287 | +## MediaQuery |
| 288 | + |
| 289 | +Check the [MediaQuery documentation](references/android/develop/ui/compose/layouts/adaptive/mediaquery/index.md) when you need to query the device's |
| 290 | +screen size, pointer precision, keyboard type, whether it has cameras or |
| 291 | +microphones, and other device capabilities. |
| 292 | + |
| 293 | +## Grid |
| 294 | + |
| 295 | +Check the Grid documentation when you need to display a fixed number of items in |
| 296 | +a grid layout: |
| 297 | + |
| 298 | +- [Overview](references/android/develop/ui/compose/layouts/adaptive/grid/index.md) |
| 299 | +- [Get started - setup](references/android/develop/ui/compose/layouts/adaptive/grid/get-started.md) |
| 300 | +- [Set container properties](references/android/develop/ui/compose/layouts/adaptive/grid/container-properties.md) |
| 301 | +- [Set item properties](references/android/develop/ui/compose/layouts/adaptive/grid/item-properties.md) |
0 commit comments