k1ngarthur
k1ngarthur

Reputation: 124

How to hide Android System UI correctly

I have searched now for a while, but get no working solution to work in my Android app. I've got an activity with several fragments. I want to hide the system UI (action and navigation bar) and also hide instantly, if the user wants to open it again.

The app is for disabled older people, that can trigger the action bars randomly and will get confused with it.

I did the following function to hide the ui (Kotlin):

private fun hideSystemUI(){
    Log.d(TAG, "hide system ui")
    WindowCompat.setDecorFitsSystemWindows(window, false)
    WindowInsetsControllerCompat(window, binding.root).let { controller ->
        controller.hide(WindowInsetsCompat.Type.systemBars())
        controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    }

    @Suppress("DEPRECATION")
    window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE)
    actionBar?.hide()
}

It includes old and newer style to hide the ui, I found online. in my onCreate() of the activity I do the following:

    // disable system bars
    hideSystemUI()

    // hide system ui on changes of system ui
    WindowInsetsControllerCompat(window, binding.root).let { controller ->
        controller.addOnControllableInsetsChangedListener { _, _ ->
            hideSystemUI()
        }
    }

    @Suppress("DEPRECATION")
    window.decorView.setOnSystemUiVisibilityChangeListener { visibility ->
        if (visibility == 0){
            hideSystemUI()
        }
    }

again, both options i found.

Also I implemented the following listener:

override fun onWindowFocusChanged(hasFocus: Boolean) {
    super.onWindowFocusChanged(hasFocus)
    // if app gets focus back, hide the system ui
    Log.d(TAG, "window focus changed")
    if (hasFocus){
        hideSystemUI()
    }
}

The system ui seems to hide at app start (it is an overlay by default and gets hided automatically once an app starts). But if I swipe up (or down) to show the system UI, nothing gets called.

Maybe I have to register the correct listener? Or is there any other issue?

Upvotes: 1

Views: 1125

Answers (0)

Related Questions