Some content is best experienced in fullscreen without any indicators on the
status bar or the navigation bar. Some examples are videos, games, image
galleries, books, and presentation slides. This is referred to as
immersive mode
. This page shows how you can engage users more deeply with
content in fullscreen.
Figure 1.
Example of immersive mode.
Immersive mode helps users avoid accidental exits during a game and
delivers an immersive experience for enjoying images, videos, and books.
However, be mindful of how often users jump in and out of apps to check notifications,
to conduct impromptu searches, or to take other actions. Because immersive mode
causes users to lose easy access to system navigation, use immersive mode only
when the benefit to the user experience goes beyond simply using extra screen
space.
Use
WindowInsetsControllerCompat.hide()
to hide the system bars and
WindowInsetsControllerCompat.show()
to bring them back.
The following snippet shows an example of configuring a button to hide and show
the system bars.
Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
...
val windowInsetsController =
WindowCompat.getInsetsController(window, window.decorView)
// Configure the behavior of the hidden system bars.
windowInsetsController.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
// Add a listener to update the behavior of the toggle fullscreen button when
// the system bars are hidden or revealed.
ViewCompat.setOnApplyWindowInsetsListener(window.decorView) { view, windowInsets ->
// You can hide the caption bar even when the other system bars are visible.
// To account for this, explicitly check the visibility of navigationBars()
// and statusBars() rather than checking the visibility of systemBars().
if (windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars())
|| windowInsets.isVisible(WindowInsetsCompat.Type.statusBars())) {
binding.toggleFullscreenButton.setOnClickListener {
// Hide both the status bar and the navigation bar.
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
}
} else {
binding.toggleFullscreenButton.setOnClickListener {
// Show both the status bar and the navigation bar.
windowInsetsController.show(WindowInsetsCompat.Type.systemBars())
}
}
ViewCompat.onApplyWindowInsets(view, windowInsets)
}
}
Java
@Override
protected void onCreate(Bundle savedInstanceState) {
...
WindowInsetsControllerCompat windowInsetsController =
WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView());
// Configure the behavior of the hidden system bars.
windowInsetsController.setSystemBarsBehavior(
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
);
// Add a listener to update the behavior of the toggle fullscreen button when
// the system bars are hidden or revealed.
ViewCompat.setOnApplyWindowInsetsListener(
getWindow().getDecorView(),
(view, windowInsets) -> {
// You can hide the caption bar even when the other system bars are visible.
// To account for this, explicitly check the visibility of navigationBars()
// and statusBars() rather than checking the visibility of systemBars().
if (windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars())
|| windowInsets.isVisible(WindowInsetsCompat.Type.statusBars())) {
binding.toggleFullscreenButton.setOnClickListener(v -> {
// Hide both the status bar and the navigation bar.
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars());
});
} else {
binding.toggleFullscreenButton.setOnClickListener(v -> {
// Show both the status bar and the navigation bar.
windowInsetsController.show(WindowInsetsCompat.Type.systemBars());
});
}
return ViewCompat.onApplyWindowInsets(view, windowInsets);
});
}
Optionally, you can specify the type of system bars to hide and determine
their behavior when a user interacts with them.
Specify which system bars to hide
To specify the type of system bars to hide, pass one of the following parameters
to
WindowInsetsControllerCompat.hide()
.
Specify behavior of hidden system bars
Use
WindowInsetsControllerCompat.setSystemBarsBehavior()
to specify how hidden system bars behave when the user interacts with them.