This is my question: I have an android app which allows users to go full screen for a better readability. The full screen fab toggle button is placed inside a fragment which actually contains the readings.
To make it dead simple:
To trigger the full screen I use this snippet:
this.fullScreenFab.setOnClickListener(v -> {
WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes();
if (this.isFullScreen) {
this.isFullScreen = false;
((AppCompatActivity)
getActivity()).getSupportActionBar().show();
getActivity().getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
} else {
this.isFullScreen = true;
((AppCompatActivity) getActivity()).getSupportActionBar().hide();
getActivity().getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
getActivity().getWindow().setAttributes(attrs);
});
Now, it works well, except that the status bar and action bar spaces keep showing. The activity goes FS (even Android warn me of this) but the space occupied by those two elements remain.
Full screen disabled:
Full screen enabled:
As you can see, the FS one has the top and bottom occupied, so the fragment does not go for a real full screen.
Fragment has the
android:fitsSystemWindows="true"
Send help please! Thanks in advance. Valerio
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.
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);
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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With