Android set full screen from fragment

You should try using this flag as it is designed to remove status bar and navigation.

getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

NOTE: You need to manually clear this while switching or closing fragment. Else this fullscreen will be applicable till the parent activity exists. To do so,

getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

Also, you can use FLAG_FULLSCREEN. Using it will effectively set a transparent notification bar but the icons on the status bar will still show up.


If you are using FLAG_LAYOUT_NO_LIMITS and trying to transparent the status bar in fragments then you should clearFlags in onPause() instead in onDetach().

        override fun onPause() {
            super.onPause()
             
             (activity as MainActivity?)!!.window.clearFlags(WindowManager
                                 .LayoutParams.FLAG_LAYOUT_NO_LIMITS)

             (activity as MainActivity?)!!.window.statusBarColor = Color.WHITE }
                                        

System UI fullscreen flags are now deprecated, for API level 30 and above override onWindowFocusChanged() in your MainActivity

override fun onWindowFocusChanged(hasFocus: Boolean) {
    super.onWindowFocusChanged(hasFocus)
    if (hasFocus) hideSystemUI() else showSystemUI()
}

private fun hideSystemUI() {
    if (Build.VERSION.SDK_INT >= 30) {
        window.insetsController?.apply {
            hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
        }
    } else {
        // Enables regular immersive mode.
        // For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
        // Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
                // Set the content to appear under the system bars so that the
                // content doesn't resize when the system bars hide and show.
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                // Hide the nav bar and status bar
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN)
    }
}
private fun showSystemUI() {
    if (Build.VERSION.SDK_INT >= 30) {
        window.insetsController?.apply {
            show(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
        }
    } else {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
    }
}

Kotlin

In onViewCreated() to make fragment fullscreen

requireActivity().window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)

In onDetach() to clear fullscreen flag so it will not affect to other fragment which is open in Activity

requireActivity().window.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)

--x--x--x--x--x--x--x--x--x--

Java

In onViewCreated() to make fragment fullscreen

requireActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

In onDetach() to clear fullscreen flag so it will not affect to other fragment which is open in Activity

requireActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

Tags:

Java

Android