androidinsets

How to avoid views resizing in immersive mode?


I'm using immersive mode when i click on "play" and show system bars again when pressing "stop". I'm using these methods:

fun hideSystemUI(container: View) {
        WindowCompat.setDecorFitsSystemWindows(window, false)
        WindowInsetsControllerCompat(window, container).let { controller ->
            controller.hide(WindowInsetsCompat.Type.systemBars())
            controller.systemBarsBehavior =
                WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        }
    }

    fun showSystemUI(container: View) {
        WindowCompat.setDecorFitsSystemWindows(window, true)
        WindowInsetsControllerCompat(window, container)
            .show(WindowInsetsCompat.Type.systemBars())
    }

But when i'm going in/out immersive mode my Views are resizing. It looks like my constraints resizing because of parent system bars is no longer on the screen. I tried to use insets:

fun View.applyPaddingByInsets(
    applyLeft: Boolean = false,
    applyTop: Boolean = false,
    applyRight: Boolean = false,
    applyBottom: Boolean = false
) {
    ViewCompat.setOnApplyWindowInsetsListener(this) { view, windowInsets ->
        val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
        view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
            val leftPadding = if (applyLeft) insets.left else 0
            val topPadding = if (applyTop) insets.top else 0
            val rightPadding = if (applyRight) insets.right else 0
            val bottomPadding = if (applyBottom) insets.bottom else 0

            view.updatePadding(leftPadding, topPadding, rightPadding, bottomPadding)
        }
        windowInsets
    }
}

I'm applying it on my first top element in Constraint which is parent for the rest views (they connect to bottom of it) and apply it on my last (bottom) view so my middle views connect to the top of it. But the result is the same. Views still resizing. Can you help me to solve this issue? I want my views to stay still in/out of immersive mode


Solution

  • I found solution. We need to set window.attributes.layoutInDisplayCutoutMode in LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER or LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES due to documentation's cutout best practices: https://developer.android.com/guide/topics/display-cutout#best_practices_for_display_cutout_support. Also get insets by windowInsets.getInsetsIgnoringVisibility(WindowInsetsCompat.Type.systemBars())