Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window insets misbehaving across android versions #781

Closed
dilraj-singh1997 opened this issue Oct 9, 2021 · 3 comments · Fixed by #784
Closed

Window insets misbehaving across android versions #781

dilraj-singh1997 opened this issue Oct 9, 2021 · 3 comments · Fixed by #784

Comments

@dilraj-singh1997
Copy link

dilraj-singh1997 commented Oct 9, 2021

Bug description

Window insets are behaving differently across android versions.
Use case- We want to show app content in full screen, so we hide the navigation bar and status bar using the following snippet.

WindowCompat.setDecorFitsSystemWindows(window, false)
requestWindowFeature(Window.FEATURE_NO_TITLE)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    window.attributes.layoutInDisplayCutoutMode =
        WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
}
WindowInsetsControllerCompat(window, findViewById(R.id.parent)).apply {
    hide(WindowInsetsCompat.Type.systemBars())
    systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}

After this, to reposition some of the UI elements below the status bar, we are using Modifier.statusBarsPadding().
This is working fine in Android SDK version 28 (Android 9).

But this is not working in Android SDK version 30 (Android 11).

If we log the LocalWindowInsets.current.statusBars.top, then it starts from its original value, and linearly decreases to 0. This can be seen in the screen-shots below.

Android API 30 insets

Android API 28 insets png

Original result

As you can see in the above image, the emulator having Android SDK version 28 is working fine, but the emulator on the right having Android SDK version 30 is displaying both the texts at the same place. Modifier.statusBarsPadding() is not working in Android SDK version 30.

Code snippet for the Texts:

Text(modifier = Modifier, text = "Hello 1 $name!")
Text(modifier = Modifier.statusBarsPadding(), text = "Hello 2 $name!")

To solve this, we are storing the max value of LocalWindowInsets.current.statusBars.top, and keeping its max value in a local variable. See the following snippet,

val topBarPadding: Dp
    @Composable
    get() {
        var _topBarPadding by remember {
            mutableStateOf(0)
        }
        _topBarPadding = max(_topBarPadding, LocalWindowInsets.current.statusBars.top)
        return with(LocalDensity.current) { _topBarPadding.toDp() }
    }

This fixes the issue, now we can see the UI elements which have been given the padding of topBarPadding below the status bar.

Modifier.padding(top = topBarPadding)

Modifier result

Now both the emulators are having the same behaviour. Code snippet for both the emulators:

Text(modifier = Modifier, text = "Hello 1 $name!")
Text(modifier = Modifier.padding(top = topBarPadding), text = "Hello 2 $name!")

Please check this issue, keeping this hack in production is not an option.

Please find the activity code attached here.

Thanks.

Expected behaviour

On all Android versions, we should be able to set the status bar padding using Modifier.statusBarsPadding()

@bentrengrove
Copy link
Collaborator

bentrengrove commented Oct 12, 2021

I was looking at something similar recently. Because you are animating out the status bar, I believe this is working as intended. I think you will need Modifier.cutoutPadding() which was only recently added in #696

It was released in 0.19.0

@dilraj-singh1997
Copy link
Author

We really appreciate your efforts, this is fantastic. Probably we can add it to the official docs as well here.

@bentrengrove
Copy link
Collaborator

Good shout, thanks for pointing that out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants