Nascondi le barre del sistema per la modalità immersiva

Alcuni contenuti possono essere visualizzati in modo ottimale a schermo intero senza indicatori sulla barra di stato o sulla barra di navigazione. Alcuni esempi sono video, giochi, gallerie di immagini, libri e slide di presentazioni. In questo caso si parla di modalità immersiva. Questa pagina mostra come coinvolgere più a fondo gli utenti con i contenuti a schermo intero.

Figura 1. Esempio di modalità immersiva.

La modalità immersiva consente agli utenti di evitare uscite accidentali durante un gioco e offre un'esperienza immersiva per guardare immagini, video e libri. Tuttavia, fai attenzione alla frequenza con cui gli utenti accedono ed escono dalle app per controllare le notifiche, eseguire ricerche estemporanee o eseguire altre azioni. Poiché la modalità immersiva fa perdere agli utenti un facile accesso alla navigazione del sistema, utilizzala solo se il vantaggio per l'esperienza utente va oltre il semplice utilizzo di spazio sullo schermo extra.

Utilizza WindowInsetsControllerCompat.hide() per nascondere le barre di sistema e WindowInsetsControllerCompat.show() per visualizzarle di nuovo.

Lo snippet seguente mostra un esempio di configurazione di un pulsante per nascondere e mostrare le barre di sistema.

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);
    });
}

Facoltativamente, puoi specificare il tipo di barre di sistema da nascondere e determinarne il comportamento quando un utente interagisce con queste.

Specifica quali barre di sistema nascondere

Per specificare il tipo di barre di sistema da nascondere, passa uno dei seguenti parametri a WindowInsetsControllerCompat.hide().

Specifica il comportamento delle barre di sistema nascoste

Utilizza WindowInsetsControllerCompat.setSystemBarsBehavior() per specificare il comportamento delle barre di sistema nascoste quando l'utente interagisce con esse.