Window insets in Compose

The Android platform is responsible for drawing the system UI, such as the status bar and navigation bar. This system UI is displayed regardless of which app the user is using. WindowInsets provides information about the system UI to ensure that your app draws in the correct area and your UI isn't obscured by the system UI.

Going edge-to-edge to draw behind the system bars
Figure 1. Going edge-to-edge to draw behind the system bars

By default, your app's UI is restricted to being laid out within the system UI, like the status bar and navigation bar. This ensures that your app's content isn't obscured by system UI elements.

However, we recommend apps to opt-in to displaying in these areas where system UI is also being displayed, which results in a more seamless user experience and allows your app to take full advantage of the window space available to it. This also allows apps to animate together with the system UI, especially when showing and hiding the software keyboard.

Opting in to displaying in these regions and displaying content behind the system UI is called going edge-to-edge. On this page, you learn about the different type of insets, how to opt-in to going edge-to-edge, and how to use the inset APIs to animate your UI and avoid obscuring parts of your app.

Inset fundamentals

When an app goes edge-to-edge, you need to ensure that important content and interactions are not obscured by the system UI. For example, if a button is placed behind the navigation bar, the user may not be able to click it.

The size of the system UI and information about where it is placed is specified via insets.

Each portion of the system UI has a corresponding type of inset that describes its size and where it is placed. For example, status bar insets provide the size and position of the status bar, whereas the navigation bar insets provide the size and position of the navigation bar. Each type of inset consists of four pixel dimensions: top, left, right, and bottom. These dimensions specify how far the system UI extends from the corresponding sides of the app's window. To avoid overlapping with that type of system UI, therefore, app UI must be inset by that amount.

These built-in Android inset types are available through WindowInsets:

WindowInsets.statusBars

The insets describing the status bars. These are the top system UI bars containing notification icons and other indicators.

WindowInsets.statusBarsIgnoringVisibility

The status bar insets for when they are visible. If the status bars are currently hidden (due to entering immersive full screen mode), then the main status bar insets will be empty, but these insets will be non-empty.

WindowInsets.navigationBars

The insets describing the navigation bars. These are the system UI bars on the left, right, or bottom side of the device, describing the taskbar or navigation icons. These can change at runtime based on the user's preferred navigation method and interacting with the taskbar.

WindowInsets.navigationBarsIgnoringVisibility

The navigation bar insets for when they are visible. If the navigation bars are currently hidden (due to entering immersive full screen mode), then the main navigation bar insets will be empty, but these insets will be non-empty.

WindowInsets.captionBar

The inset describing the system UI window decoration if in a freeform window, like top title bar.

WindowInsets.captionBarIgnoringVisibility

The caption bar insets for when they are visible. If the caption bars are currently hidden, then the main caption bar insets will be empty, but these insets will be non-empty.

WindowInsets.systemBars

The union of the system bar insets, which include the status bars, navigation bars, and caption bar.

WindowInsets.systemBarsIgnoringVisibility

The system bar insets for when they are visible. If the system bars are currently hidden (due to entering immersive full screen mode), then the main system bar insets will be empty, but these insets will be non-empty.

WindowInsets.ime

The insets describing the amount of space on the bottom that the software keyboard occupies.

WindowInsets.imeAnimationSource

The insets describing the amount of space that the software keyboard occupied before the current keyboard animation.

WindowInsets.imeAnimationTarget

The insets describing the amount of space that the software keyboard will occupy after the current keyboard animation.

WindowInsets.tappableElement

A type of insets describing more detailed information about the navigation UI, giving the amount of space where "taps" will be handled by the system, and not the app. For transparent navigation bars with gesture navigation, some app elements can be tappable through the system navigation UI.

WindowInsets.tappableElementIgnoringVisibility

The tappable element insets for when they are visible. If the tappable elements are currently hidden (due to entering immersive full screen mode), then the main tappable element insets will be empty, but these insets will be non-empty.

WindowInsets.systemGestures

The insets representing the amount of insets where the system will intercept gestures for navigation. Apps can manually specify handling a limited amount of these gestures via Modifier.systemGestureExclusion.

WindowInsets.mandatorySystemGestures

A subset of the system gestures that will always be handled by the system, and which can't be opted out via Modifier.systemGestureExclusion.

WindowInsets.displayCutout

The insets representing the amount of spacing needed to avoid overlapping with a display cutout (notch or pinhole).

WindowInsets.waterfall

The insets representing the curved areas of a waterfall display. A waterfall display has curved areas along the edges of the screen where the screen begins to wrap along the sides of the device.

These types are summarized by three "safe" inset types that ensure content isn't obscured:

These "safe" inset types protect content in different ways, based on the underlying platform insets:

Insets setup

To allow your app full control over where it draws content, follow these setup steps. Without these steps, your app may draw black or solid colors behind the system UI, or not animate synchronously with the software keyboard.

  1. Call enableEdgeToEdge() in Activity.onCreate. This call requests that your app display behind the system UI. Your app will then be in control of how those insets are used to adjust the UI.
  2. Set android:windowSoftInputMode="adjustResize" in your Activity's AndroidManifest.xml entry. This setting allows your app to receive the size of the software IME as insets, which you can use to pad and lay out content appropriately when the IME appears and disappears in your app.

    <!-- in your AndroidManifest.xml file: -->
    <activity
      android:name=".ui.MainActivity"
      android:label="@string/app_name"
      android:windowSoftInputMode="adjustResize"
      android:theme="@style/Theme.MyApplication"
      android:exported="true">
    

Compose APIs

Once your Activity has taken control of handling all insets, you can use Compose APIs to ensure that content isn't obscured and interactable elements don't overlap with the system UI. These APIs also synchronize your app's layout with inset changes.

For example, this is the most basic method of applying the insets to the content of your entire app:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    enableEdgeToEdge()

    setContent {
        Box(Modifier.safeDrawingPadding()) {
            // the rest of the app
        }
    }
}

This snippet applies the safeDrawing window insets as padding around the entire content of the app. While this ensures that interactable elements don't overlap with the system UI, it also means that none of the app will draw behind the system UI to achieve an edge-to-edge effect. To make full use of the entire window, you need to fine-tune where the insets are applied on a screen-by-screen or component-by-component basis.

All of these inset types are animated automatically with IME animations backported to API 21. By extension, all of your layouts using these insets are also automatically animated as the inset values change.

There are two primary ways to use these inset types to adjust your Composable layouts: padding modifiers and inset size modifiers.

Padding modifiers

Modifier.windowInsetsPadding(windowInsets: WindowInsets) applies the given window insets as padding, acting just like Modifier.padding would. For example, Modifier.windowInsetsPadding(WindowInsets.safeDrawing) applies the safe drawing insets as padding on all 4 sides.

There are also several built-in utility methods for the most common inset types. Modifier.safeDrawingPadding() is one such method, equivalent to Modifier.windowInsetsPadding(WindowInsets.safeDrawing). There are analogous modifiers for the other inset types.

Inset size modifiers

The following modifiers apply an amount of window insets by setting the size of the component to be the size of the insets:

Modifier.windowInsetsStartWidth(windowInsets: WindowInsets)

Applies the start side of windowInsets as the width (like Modifier.width)

Modifier.windowInsetsEndWidth(windowInsets: WindowInsets)

Applies the end side of windowInsets as the width (like Modifier.width)

Modifier.windowInsetsTopHeight(windowInsets: WindowInsets)

Applies the top side of windowInsets as the height (like Modifier.height)

Modifier.windowInsetsBottomHeight(windowInsets: WindowInsets)

Applies the bottom side of windowInsets as the height (like Modifier.height)

These modifiers are especially useful for sizing a Spacer that takes up the space of insets:

LazyColumn(
    Modifier.imePadding()
) {
    // Other content
    item {
        Spacer(
            Modifier.windowInsetsBottomHeight(
                WindowInsets.systemBars
            )
        )
    }
}

Inset consumption

The inset padding modifiers (windowInsetsPadding and helpers like safeDrawingPadding) automatically consume the portion of the insets that are applied as padding. While going deeper into the composition tree, nested inset padding modifiers and the inset size modifiers know that some portion of the insets have already been consumed by outer inset padding modifiers, and avoid using the same portion of the insets more than once which would result in too much extra space.

Inset size modifiers also avoid using the same portion of insets more than once if insets have already been consumed. However, since they are changing their size directly, they don't consume insets themselves.

As a result, nesting padding modifiers automatically change the amount of padding applied to each composable.

Looking at the same LazyColumn example as before, the LazyColumn is being resized by the imePadding modifier. Inside the LazyColumn, the last item is sized to be the height of the bottom of the system bars:

LazyColumn(
    Modifier.imePadding()
) {
    // Other content
    item {
        Spacer(
            Modifier.windowInsetsBottomHeight(
                WindowInsets.systemBars
            )
        )
    }
}

When the IME is closed, the imePadding() modifier applies no padding, since the IME has no height. Since the imePadding() modifier is applying no padding, no insets are being consumed, and the height of the Spacer will be the size of the bottom side of the system bars.

When the IME opens, the IME insets animate to match the size of the IME, and the imePadding() modifier begins applying bottom padding to resize the LazyColumn as the IME opens. As the imePadding() modifier begins applying bottom padding, it also starts consuming that amount of insets. Therefore, the height of the Spacer starts to decrease, as part of the spacing for the system bars has already been applied by the imePadding() modifier. Once the imePadding() modifier is applying an amount of bottom padding that is larger than the system bars, the height of the Spacer is zero.

When the IME closes, the changes happen in reverse: The Spacer starts to expand from a height of zero once the imePadding() is applying less than the bottom side of the system bars, until finally the Spacer matches the height of the bottom side of the system bars once the IME is completely animated out.

Figure 2. Edge-to-edge lazy column with TextField

This behavior is accomplished through communication between all windowInsetsPadding modifiers, and can be influenced in a couple of other ways.

Modifier.consumedWindowInsets(insets: WindowInsets) also consumes insets in the same way as Modifier.windowInsetsPadding, but it doesn't apply the consumed insets as padding. This is useful in combination with the inset size modifiers, to indicate to siblings that a certain amount of insets have already been consumed:

Column(Modifier.verticalScroll(rememberScrollState())) {
    Spacer(Modifier.windowInsetsTopHeight(WindowInsets.systemBars))

    Column(
        Modifier.consumeWindowInsets(
            WindowInsets.systemBars.only(WindowInsetsSides.Vertical)
        )
    ) {
        // content
        Spacer(Modifier.windowInsetsBottomHeight(WindowInsets.ime))
    }

    Spacer(Modifier.windowInsetsBottomHeight(WindowInsets.systemBars))
}

Modifier.consumedWindowInsets(paddingValues: PaddingValues) behaves very similarly to the version with a WindowInsets argument, but takes an arbitrary PaddingValues to consume. This is useful for informing children when padding or spacing is provided by some other mechanism than the inset padding modifiers, such as an ordinary Modifier.padding or fixed height spacers:

@OptIn(ExperimentalLayoutApi::class)
Column(Modifier.padding(16.dp).consumeWindowInsets(PaddingValues(16.dp))) {
    // content
    Spacer(Modifier.windowInsetsBottomHeight(WindowInsets.ime))
}

In cases where the raw window insets are needed without consumption, use the WindowInsets values directly, or use WindowInsets.asPaddingValues() to return a PaddingValues of the insets that are unaffected by consumption. However, due to the caveats below, prefer to use the window insets padding modifiers and window insets size modifiers wherever possible.

Insets and Jetpack Compose phases

Compose uses the underlying AndroidX core APIs to update and animate insets, which use the underlying platform APIs managing insets. Because of that platform behavior, insets have a special relationship with the phases of Jetpack Compose.

The value of insets are updated after the composition phase, but before the layout phase. This means that reading the value of insets in composition generally uses a value of the insets that is one frame late. The built-in modifiers described on this page are built to delay using the values of the insets until the layout phase, which ensures that the inset values are used on the same frame as they are updated.

Keyboard IME animations with WindowInsets

You can apply Modifier.imeNestedScroll() to a scrolling container to open and close the IME automatically when scrolling to the bottom of the container.

class WindowInsetsExampleActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        WindowCompat.setDecorFitsSystemWindows(window, false)

        setContent {
            MaterialTheme {
                MyScreen()
            }
        }
    }
}

@OptIn(ExperimentalLayoutApi::class)
@Composable
fun MyScreen() {
    Box {
        LazyColumn(
            modifier = Modifier
                .fillMaxSize() // fill the entire window
                .imePadding() // padding for the bottom for the IME
                .imeNestedScroll(), // scroll IME at the bottom
            content = { }
        )
        FloatingActionButton(
            modifier = Modifier
                .align(Alignment.BottomEnd)
                .padding(16.dp) // normal 16dp of padding for FABs
                .navigationBarsPadding() // padding for navigation bar
                .imePadding(), // padding for when IME appears
            onClick = { }
        ) {
            Icon(imageVector = Icons.Filled.Add, contentDescription = "Add")
        }
    }
}

Animation showing a UI element scrolling up and down to make way for a keyboard

Figure 1. IME animations

Inset support for Material 3 Components

For ease of use, many of the built-in Material 3 composables (androidx.compose.material3) handle insets themselves, based on how the composables are placed in your app according to the Material specifications.

Inset handling composables

Below is a list of the Material Components that automatically handle insets.

App bars

Content containers

Scaffold

By default, Scaffold provides insets as parameter paddingValues for you to consume and use. Scaffold does not apply the insets to content; this responsibility is yours. For example, to consume these insets with a LazyColumn inside a Scaffold:

Scaffold { innerPadding ->
    // innerPadding contains inset information for you to use and apply
    LazyColumn(
        // consume insets as scaffold doesn't do it by default
        modifier = Modifier.consumeWindowInsets(innerPadding),
        contentPadding = innerPadding
    ) {
        items(count = 100) {
            Box(
                Modifier
                    .fillMaxWidth()
                    .height(50.dp)
                    .background(colors[it % colors.size])
            )
        }
    }
}

Override default insets

You can change the windowInsets parameter passed to the composable to configure the composable's behavior. This parameter can be a different type of window inset to apply instead, or disabled by passing an empty instance: WindowInsets(0, 0, 0, 0).

For example, to disable the inset handling on LargeTopAppBar, set the windowInsets parameter to an empty instance:

LargeTopAppBar(
    windowInsets = WindowInsets(0, 0, 0, 0),
    title = {
        Text("Hi")
    }
)

Interop with the View system insets

You may need to override default insets when your screen has both Views and Compose code in the same hierarchy. In this case, you need to be explicit in which one should consume the insets, and which one should ignore them.

For example, if your outermost layout is an Android View layout, you should consume the insets in the View system and ignore them for Compose. Alternatively, if your outermost layout is a composable, you should consume the insets in Compose, and pad the AndroidView composables accordingly.

By default, each ComposeView consumes all insets at the WindowInsetsCompat level of consumption. To change this default behavior, set ComposeView.consumeWindowInsets to false.

Resources

  • Now in Android - a fully functional Android app built entirely with Kotlin and Jetpack Compose.