Adaptive

android/skills/jetpack-compose/adaptive

作者 android42dc2270e960Complete terms in LICENSE.txt7.7K 个星标收录于 2026年10月8日更新于 2026年10月8日仓库13天前更新

Instructions to make or update an app's UI so that it adapts to different Android devices including phones, tablets, foldables, laptops, desktop, TV, Auto and XR. It includes how to handle different window sizes, pointing devices (such as mouse) and text entry devices (such as keyboard) using the Compose MediaQuery API. It also covers multi-pane layouts using Navigation3 Scenes, adaptive UI components (such as buttons) with varying target sizes, and adaptive layouts (including navigation areas - nav rails and nav bars) using the Compose Grid and FlexBox APIs.

AI 生成的概览

指导如何让 Android Compose 应用的界面自适应手机、平板、折叠屏、桌面、电视、车载和 XR 等设备。

功能
该技能提供一套分步工作流,使用 Jetpack Compose 让 Android 应用的界面适配不同设备形态。内容包括用截图测试验证界面、把导航栏改为自适应导航区域、用 Navigation 3 Scenes 添加列表-详情和辅助窗格的多窗格布局、让列表和网格自适应,以及滚动时隐藏应用栏。它还通过随附的参考文件说明 Grid、FlexBox 和 MediaQuery 等实验性 Compose API。
适用场景
当使用 Compose 和 Jetpack Navigation 3 构建的 Android 应用需要适配手机、平板、折叠屏、笔记本、桌面、电视、车载或 XR 等设备时使用。适合把底部导航栏迁移为导航栏侧边栏、引入多窗格布局,或让列表和网格按可用宽度调整等任务。
运行要求
应用必须所有界面都使用 Compose,并使用 Jetpack Navigation 3,否则该技能会先建议使用迁移类技能。它依赖 Material 3 自适应布局库、Compose Preview 截图测试工具,以及 Compose 1.11.0-beta01 的实验性 API(Grid、FlexBox、MediaQuery)。该技能不包含脚本,只有参考文档文件。

Prerequisites

The app must:

  • Use Compose for all screens. If it's still using Fragments or Views, suggest using the XML to Compose skill to migrate those screens.
  • Use Jetpack Navigation 3. If it doesn't, suggest the Navigation 3 skill to migrate the app.

Workflow to make an app adaptive

To make an app adaptive, follow these steps or a subset of them adapting to the task.

  • Step 1: Verify current UI
  • Step 2: Make the navigation bar adaptive
  • Step 3: Add multi-pane layouts
  • Step 4: Make vertical lists adaptive by changing the number of columns
  • Step 5: Hide app bars when scrolling

Step 1. Verify current UI

Ensure that screenshot tests exist to verify the current UI on different form factors. If they don't exist, add the Compose Preview Screenshot Testing tool [blocked]. Use the following annotation to create previews for all the major form factors. For example:

kotlin
@Preview(name = "Phone", device = Devices.PHONE, showBackground = true)@Preview(name = "Foldable", device = Devices.FOLDABLE, showBackground = true)@Preview(name = "Tablet", device = Devices.TABLET, showBackground = true)@Preview(name = "Desktop", device = Devices.DESKTOP, showBackground = true)annotation class FormFactorPreviews
@PreviewTest@FormFactorPreviews@Composablefun FeedScreenPreview() {    SnippetsTheme {        Box {            Text("My Screen")        }    }}
<br />

Step 2. Make the navigation bar adaptive

Bottom navigation bars are optimized for touch input when the user is holding a phone in portrait mode. On larger screen hand-held devices, like tablets and unfolded foldables, the navigation area must be accessible from the edge of the screen (navigation rail).

If you need to provide more screen space for the content, hide the navigation area. Examples of this include:

  • 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.
  • Hiding the navigation area when its content is distracting. For example, in camera previews or when displaying a full-screen photo.

When the detail screen is displayed full-screen on mobile, full-screen mode must be deactivated on larger screens.

Steps to migrate:

  • Locate the existing navigation bar.
  • Convert each item to a NavigationSuiteItem.
  • 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".
  • Replace the container that held the navigation bar (often a Scaffold) with NavigationSuiteScaffold from the Material 3 adaptive layouts library.
  • Supply the navigation items using the navigationItems parameter of NavigationSuiteScaffold.

Step 2.1. Control navigation area visibility

If the navigation bar's visibility changes - it is hidden under certain scenarios or on certain screens - this behavior must be maintained with the adaptive navigation area. This is done using NavigationSuiteScaffold's state parameter.

Steps to migrate:

  • Identify the scenarios under which the navigation bar is hidden. This is usually done with a boolean variable for the visibility. Use isNavBarVisible or shouldShowNavBar as the variable name.
  • Create an instance of NavigationSuiteScaffoldState using rememberNavigationSuiteScaffoldState() and pass it to NavigationSuiteScaffold.
  • When the navigation area visibility changes, use a LaunchedEffect to call show or hide on the NavigationSuiteScaffoldState.

For example:

kotlin
// Pass this variable to any composable that needs to control the navigation area visibilityvar isNavBarVisible by remember { mutableStateOf(true) }val scaffoldVisibilityState = rememberNavigationSuiteScaffoldState()
NavigationSuiteScaffold(    navigationSuiteItems = navItems,    state = scaffoldVisibilityState) {    // Main content}
LaunchedEffect(isNavBarVisible){    if (isNavBarVisible) {        scaffoldVisibilityState.show()    } else {        scaffoldVisibilityState.hide()    }}
<br />

Step 3. Add multi-pane layouts using Navigation 3 Scenes

Analyze the codebase looking for related screens - tapping on something in one screen opens another screen that shows information related to the first. There are two canonical screen relationships: list-detail and supporting pane.

IMPORTANT: You must use the Navigation 3 SceneStrategy approach to implement multi-pane layouts. Do not use ListDetailPaneScaffold or SupportingPaneScaffold.

Step 3.1. List-detail

Identify the list and detail screens

List-detail layouts display a list of items (this is the list screen) and clicking on an item opens a new screen that shows more details about that item (the detail screen).

Typical usage includes productivity apps like email, notes, and messaging.

Unless requested explicitly, avoid this pattern when the detail content requires substantial screen space (e.g., images or media that benefits from a full-screen presentation).

Add a Material list-detail SceneStrategy
  • Add the androidx.compose.material3.adaptive:adaptive-navigation3 library
  • Create an androidx.compose.material3.adaptive.navigation3.ListDetailSceneStrategy using rememberListDetailSceneStrategy
  • Pass the ListDetailSceneStrategy to NavDisplay using its sceneStrategies parameter
Use metadata to identify the list and detail screens
  • Add metadata using entry(metadata = ...) or NavEntry(metadata = ...) to the list entry using ListDetailSceneStrategy.listPane(detailPlaceholder = { <placeholder composable> }).
  • Use the detailPlaceholder parameter to add a placeholder on the detail screen when no list items are selected.
  • Add metadata to the detail entry using ListDetailSceneStrategy.detailPane().
Important considerations
  • 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.
  • Detail screens must not show a back arrow when on a list-detail layout.

For a reference implementation, check the Nav3 Material List Detail recipe [blocked].

Step 3.2. Supporting pane

Identify supporting pane screens where a main screen displays a single item, and selecting it opens a "supporting screen" with more details. The supporting screen complements the main screen and is shown in a supporting pane.

Add a Material supporting pane SceneStrategy
  • If you haven't already, add the androidx.compose.material3.adaptive:adaptive-navigation3 library
  • Create an androidx.compose.material3.adaptive.navigation3.SupportingPaneSceneStrategy using rememberSupportingPaneSceneStrategy
  • Pass the SupportingPaneSceneStrategy to NavDisplay using its sceneStrategies parameter
Use metadata to identify the main and supporting screens
  • Add metadata using entry(metadata = ...) or NavEntry(metadata = ...) to the main entry using SupportingPaneSceneStrategy.mainPane()
  • Add metadata to the supporting entry using SupportingPaneSceneStrategy.supportingPane()

Step 3.3. Run screenshot tests

If you have made changes, record new reference files. Ask the user to visually verify that the new layouts are correct.

Step 4. Make vertical lists adaptive by changing the number of columns

Step 4.1. Make lazy lists adaptive

Look for the following vertical list composables: LazyColumn, LazyVerticalGrid, LazyVerticalStaggeredGrid.

Steps to migrate:

  • Choose a suitable minimum width in dp for the column. The item must be clearly visible to the user at this width.
  • For LazyColumn: change to a LazyVerticalGrid and follow the instruction later
  • For LazyVerticalGrid: change the columns parameter to use GridCells.Adaptive(<width>.dp)
  • For LazyVerticalStaggeredGrid: change the columns parameter to use StaggeredGridCells.Adaptive(<width>.dp)

Step 4.2. Migrate non-lazy lists to Grid

WARNING: Grid is an experimental API available from Compose 1.11.0-beta01. Confirm with the user that they are happy to use an experimental API in their codebase.

Look for any Column that contains multiple items of the same type and replace it with Grid. Do not replace it with LazyVerticalGrid or any other lazy layout. Do not place Grid inside the existing Column. Completely replace it.

Grid is configured by supplying a lambda (an extension function on GridConfigurationScope) to its config parameter. Inside the lambda, constraints provides the minimum and maximum dimensions of the grid container and can be used to change the number of rows and columns based on the available size. For example, the following code configures Grid such that when the available width is:

  • less than 800dp, a 2x4 grid is used
  • 800dp or more, a 4x2 grid is used
kotlin
Grid(    config = {        val maxWidthDp = constraints.maxWidth.toDp()        val (cols, rows) = if (maxWidthDp < 800.dp){            2 to 4        } else{            4 to 2        }
        val gapSizeDp = 8.dp        val cellSize = ((maxWidthDp - (gapSizeDp * (cols - 1))) / cols).coerceAtLeast(0.dp)        repeat(cols) { column(cellSize) }        repeat(rows) { row(cellSize) }        gap(gapSizeDp)    }) { /** items **/ }
<br />

Grid is an experimental API so add the @OptIn(ExperimentalGridApi::class) annotation to any function that uses it.

Step 5: Hide App Bars when scrolling

In an app with multiple top-level destinations, each screen must manage its own app bar state independently. There are two main scroll behaviors:

  • exitUntilCollapsedScrollBehavior: Hides on scroll down, stays hidden while you scroll up until you reach the very top (0 offset).
  • enterAlwaysScrollBehavior: Hides on scroll down, shows immediately on scroll up.

Final step: Build and test

Build the app and run the local tests. If the project has screenshot tests, run them but DO NOT update the reference images. Prompt the user to do this after they have viewed the screenshot diffs.

Additional documentation for experimental adaptive APIs

The following APIs are available from Compose 1.11.0-beta01.

FlexBox

Check the FlexBox documentation:

  • Overview [blocked]
  • Get started - setup [blocked]
  • Set container behavior [blocked]
  • Set item behavior [blocked]

MediaQuery

Check the MediaQuery documentation [blocked] when you need to query the device's screen size, pointer precision, keyboard type, whether it has cameras or microphones, and other device capabilities.

Grid

Check the Grid documentation when you need to display a fixed number of items in a grid layout:

  • Overview [blocked]
  • Get started - setup [blocked]
  • Set container properties [blocked]
  • Set item properties [blocked]

来源与署名

来源:android/skills位于jetpack-compose/adaptive提交42dc227

许可证: Complete terms in LICENSE.txt

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架

更多来自 android/skills 的技能

Wear Compose M3

android

指导构建、更新和迁移 Wear OS Compose Material3 应用,并提供规范组件示例与版本规则。

Software Development7.7K13天前更新

Display Glasses With Jetpack Compose Glimmer

android

Provides guidelines for developing projected Android XR apps for display glasses using the Jetpack Compose Glimmer UI toolkit. This skill covers foundational Glimmer design principles, workflows for implementing Jetpack Compose Glimmer, and interaction models for the glasses form factor. Use this skill to build an Android XR Augmented Experience app with Jetpack Compose Glimmer that adheres to the Glimmer design system for optimized glasses styling.

待分类7.7K13天前更新

Play Billing Library Version Upgrade

android

指导将 Android 项目从旧版 Google Play 结算库升级到最新稳定版本。

Software Development7.7K13天前更新

Leanback To Compose Tv Migration

android

Provides instructions and architectural patterns for migrating Android TV applications from legacy Leanback UI Toolkit, Android Views, or Support Fragments to Jetpack Compose for TV (androidx.tv). Use this skill for Leanback to Compose migrations, including browse screen, settings screen, authentication screen, login screen, or video playback screen migrations, or when replacing BrowseSupportFragment, LeanbackSettingsFragment, PreferenceFragment, BaseLeanbackPreferenceFragmentCompat, VideoSupportFragment, GuidedStepSupportFragment, SearchSupportFragment, VerticalGridSupportFragment, Presenter, ArrayObjectAdapter, or CursorMapper with modern Compose equivalents, implementing immersive carousels with focus memory, Media3 video playback with PlayerSurface, or custom 10-foot hero layouts.

待分类7.7K13天前更新

Testing Setup

android

分析 Android 项目的测试配置,并安装测试框架、测试装置以及单元、UI、截图和端到端测试。

Software Development7.7K13天前更新

Android Profiler

android

指导 Android 性能分析:录制系统跟踪、堆转储和方法记录,并使用 Perfetto SQL 进行分析。

Software Development7.7K13天前更新