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

MutableWindowInsets

@ExperimentalLayoutApi
public final class MutableWindowInsets implements WindowInsets


A WindowInsets whose values can change without changing the instance. This is useful to avoid recomposition when WindowInsets can change.

import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.MutableWindowInsets
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.exclude
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.onConsumedWindowInsetsChanged
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeContent
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.core.view.WindowCompat

class SampleActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        WindowCompat.setDecorFitsSystemWindows(window, false)
        super.onCreate(savedInstanceState)
        setContent {
            val remainingInsets = remember { MutableWindowInsets() }
            val safeContent = WindowInsets.safeContent
            Box(
                Modifier
                    .navigationBarsPadding()
                    .onConsumedWindowInsetsChanged { consumedWindowInsets ->
                        remainingInsets.insets = safeContent.exclude(consumedWindowInsets)
                    }) {
                // padding can be used without recomposition when insets change.
                val padding = remainingInsets.asPaddingValues()
                Box(Modifier.padding(padding))
            }
        }
    }
}

Summary

Public constructors

Public methods

int

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

final @NonNull WindowInsets

The WindowInsets that are used for left, top, right, and bottom values.

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

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

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

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

int

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

final void

The WindowInsets that are used for left, top, right, and bottom values.

Public constructors

MutableWindowInsets

public MutableWindowInsets(@NonNull WindowInsets initialInsets)

Public methods

getBottom

public int getBottom(@NonNull Density density)

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

getInsets

public final @NonNull WindowInsets getInsets()

The WindowInsets that are used for left, top, right, and bottom values.

getLeft

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

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

getRight

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

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

getTop

public int getTop(@NonNull Density density)

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

setInsets

public final void setInsets(@NonNull WindowInsets insets)

The WindowInsets that are used for left, top, right, and bottom values.