{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

WindowInsets

public interface WindowInsets

Known direct subclasses
MutableWindowInsets

A WindowInsets whose values can change without changing the instance.


A representation of window insets that tracks access to enable recomposition, relayout, and redrawing when values change. These values should not be read during composition to avoid doing composition for every frame of an animation. Use methods like Modifier.windowInsetsPadding, Modifier.systemBarsPadding, and Modifier.windowInsetsTopHeight for Modifiers that will not cause recomposition when values change.

Use the WindowInsets.Companion extensions to retrieve WindowInsets for the current window.

Summary

Nested types

public static class WindowInsets.Companion

Public methods

abstract int

The space, in pixels, at the bottom of the window that the inset represents.

abstract int
getLeft(@NonNull Density density, @NonNull LayoutDirection layoutDirection)

The space, in pixels, at the left of the window that the inset represents.

abstract int
getRight(
    @NonNull Density density,
    @NonNull LayoutDirection layoutDirection
)

The space, in pixels, at the right of the window that the inset represents.

abstract int

The space, in pixels, at the top of the window that the inset represents.

Extension functions

default final @NonNull WindowInsets
WindowInsetsKt.add(
    @NonNull WindowInsets receiver,
    @NonNull WindowInsets insets
)

Returns a WindowInsets that has values of this, added to the values of insets.

default final @NonNull PaddingValues

Convert a WindowInsets to a PaddingValues and uses LocalDensity for DP to pixel conversion.

default final @NonNull PaddingValues
WindowInsetsKt.asPaddingValues(
    @NonNull WindowInsets receiver,
    @NonNull Density density
)

Convert a WindowInsets to a PaddingValues and uses density for DP to pixel conversion.

default final @NonNull WindowInsets
WindowInsetsKt.exclude(
    @NonNull WindowInsets receiver,
    @NonNull WindowInsets insets
)

Returns the values in this WindowInsets that are not also in insets.

default final @NonNull WindowInsets
WindowInsetsKt.only(
    @NonNull WindowInsets receiver,
    @NonNull WindowInsetsSides sides
)

Returns a WindowInsets that eliminates all dimensions except the ones that are enabled.

default final @NonNull WindowInsets
WindowInsetsKt.union(
    @NonNull WindowInsets receiver,
    @NonNull WindowInsets insets
)

Returns a WindowInsets that has the maximum values of this WindowInsets and insets.

Public methods

getBottom

abstract int getBottom(@NonNull Density density)

The space, in pixels, at the bottom of the window that the inset represents.

getLeft

abstract int getLeft(@NonNull Density density, @NonNull LayoutDirection layoutDirection)

The space, in pixels, at the left of the window that the inset represents.

getRight

abstract int getRight(
    @NonNull Density density,
    @NonNull LayoutDirection layoutDirection
)

The space, in pixels, at the right of the window that the inset represents.

getTop

abstract int getTop(@NonNull Density density)

The space, in pixels, at the top of the window that the inset represents.

Extension functions

WindowInsetsKt.add

default final @NonNull WindowInsets WindowInsetsKt.add(
    @NonNull WindowInsets receiver,
    @NonNull WindowInsets insets
)

Returns a WindowInsets that has values of this, added to the values of insets. For example, if this has a top of 10 and insets has a top of 5, the returned WindowInsets will have a top of 15.

WindowInsetsKt.asPaddingValues

@Composable
default final @NonNull PaddingValues WindowInsetsKt.asPaddingValues(@NonNull WindowInsets receiver)

Convert a WindowInsets to a PaddingValues and uses LocalDensity for DP to pixel conversion. PaddingValues can be passed to some containers to pad internal content so that it doesn't overlap the insets when fully scrolled. Ensure that the insets are consumed after the padding is applied if insets are to be used further down the hierarchy.

import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.lazy.LazyColumn
import androidx.core.view.WindowCompat

class SampleActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        WindowCompat.setDecorFitsSystemWindows(window, false)
        super.onCreate(savedInstanceState)
        setContent {
            LazyColumn(
                contentPadding = WindowInsets.navigationBars.asPaddingValues()
            ) {
                // items
            }
        }
    }
}

WindowInsetsKt.asPaddingValues

default final @NonNull PaddingValues WindowInsetsKt.asPaddingValues(
    @NonNull WindowInsets receiver,
    @NonNull Density density
)

Convert a WindowInsets to a PaddingValues and uses density for DP to pixel conversion. PaddingValues can be passed to some containers to pad internal content so that it doesn't overlap the insets when fully scrolled. Ensure that the insets are consumed after the padding is applied if insets are to be used further down the hierarchy.

import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.lazy.LazyColumn
import androidx.core.view.WindowCompat

class SampleActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        WindowCompat.setDecorFitsSystemWindows(window, false)
        super.onCreate(savedInstanceState)
        setContent {
            LazyColumn(
                contentPadding = WindowInsets.navigationBars.asPaddingValues()
            ) {
                // items
            }
        }
    }
}

WindowInsetsKt.exclude

default final @NonNull WindowInsets WindowInsetsKt.exclude(
    @NonNull WindowInsets receiver,
    @NonNull WindowInsets insets
)

Returns the values in this WindowInsets that are not also in insets. For example, if this WindowInsets has a WindowInsets.getTop value of 10 and insets has a WindowInsets.getTop value of 8, the returned WindowInsets will have a WindowInsets.getTop value of 2.

Negative values are never returned. For example if insets has a WindowInsets.getTop of 10 and this has a WindowInsets.getTop of 0, the returned WindowInsets will have a WindowInsets.getTop value of 0.

WindowInsetsKt.only

default final @NonNull WindowInsets WindowInsetsKt.only(
    @NonNull WindowInsets receiver,
    @NonNull WindowInsetsSides sides
)

Returns a WindowInsets that eliminates all dimensions except the ones that are enabled. For example, to have a WindowInsets at the bottom of the screen, pass WindowInsetsSides.Bottom.

WindowInsetsKt.union

default final @NonNull WindowInsets WindowInsetsKt.union(
    @NonNull WindowInsets receiver,
    @NonNull WindowInsets insets
)

Returns a WindowInsets that has the maximum values of this WindowInsets and insets.