3

How can one toggle between immersive to non-immersive mode without layout being re-calculated and thus experience a bounce effect? Here's the relevant code that I'm using to toggle between the states:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        immersive_btn.setOnClickListener {
            toggleImmersive()
        }
    }
    val isInFullScreenImmersiveMode: Boolean
        get() = window!!.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_IMMERSIVE == View.SYSTEM_UI_FLAG_IMMERSIVE

    private fun toggleImmersive() {
        if (isInFullScreenImmersiveMode) {
            showSystemBar()
        } else {
            setFullScreenImmersiveMode()
        }
    }

    fun showSystemBar() {
        window!!.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
    }

    fun setFullScreenImmersiveMode() {
        window!!.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN
                or View.SYSTEM_UI_FLAG_IMMERSIVE)
    }

A video showing the bounce: enter image description here

Only solution I came up with is using window?.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN) in OnCreate() but that would result in another problem: The status bar won't be shown, even when not in full screen immersive mode.

Edit: layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/immersive_btn"
        android:layout_centerInParent="true"
        android:layout_centerHorizontal="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Toggle Immersive" />

</RelativeLayout>
idish
  • 3,190
  • 12
  • 53
  • 85
  • seems like it's either an emulator or layout specific issue. add the layout to reproduce the issue. – Pavneet_Singh Mar 23 '20 at 09:25
  • @Pavneet_Singh The layout file is very basic, I added it now to the original post. I'm not testing it on emulator, but on my own device OP6. Also, the theme of the activity is the regular one without any customizations (AppTheme). It is fairly easy to reproduce this behaviour with the code above. – idish Mar 23 '20 at 10:02
  • the behavior is not the same as android R, it's only moving once, that's why I wasn't able to reproduce the exact behavior. You might end up using `viewobserver` to handle the changes. – Pavneet_Singh Mar 24 '20 at 12:39
  • @Pavneet_Singh So this behavior is not reproducible in Android R? Interesting.. Can you suggest a solution? I wouldn't mind using `ViewTreeObserver` to prevent the layout bouncing. – idish Mar 24 '20 at 21:41
  • well, that will require lot of testing on different devices. Better, You should [report the bug here](https://www.google.com/search?client=safari&rls=en&q=open+issue+on+android.developer&ie=UTF-8&oe=UTF-8). – Pavneet_Singh Apr 02 '20 at 11:12

0 Answers0