Nascondi le barre del sistema per la modalità immersiva

Alcuni contenuti vengono visualizzati meglio a schermo intero senza indicatori nella barra di stato o nella barra di navigazione. Alcuni esempi sono video, giochi, gallerie di immagini, libri e slide di presentazioni. Questa modalità è chiamata modalità immersiva. Questa pagina mostra come coinvolgere maggiormente gli utenti con i contenuti a schermo intero.

Figura 1. Esempio di modalità immersiva.

La modalità immersiva aiuta gli utenti a evitare uscite accidentali durante un gioco e offre un'esperienza immersiva per la fruizione di immagini, video e libri. Tuttavia, tieni presente la frequenza con cui gli utenti entrano ed escono dalle app per controllare le notifiche, eseguire ricerche improvvisate o intraprendere altre azioni. Poiché la modalità immersiva impedisce agli utenti di accedere facilmente alla navigazione di sistema, utilizzala solo quando il vantaggio per l'esperienza utente va oltre il semplice utilizzo di spazio aggiuntivo sullo schermo.

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

Il seguente snippet 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);
    });
}

Se vuoi, puoi specificare il tipo di barre di sistema da nascondere e determinare il loro comportamento quando un utente interagisce con loro.

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 loro.