Cómo ocultar las barras del sistema para el modo envolvente

Parte del contenido se experimenta mejor en pantalla completa sin ningún indicador en la barra de estado ni en la de navegación. Algunos ejemplos son los videos, los juegos, las galerías de imágenes, los libros y las diapositivas de presentaciones. Esto se conoce como modo envolvente. En esta página, se muestra cómo puedes atraer más a los usuarios con contenido en pantalla completa.

Figura 1: Ejemplo de modo envolvente.

El modo envolvente ayuda a los usuarios a evitar salidas accidentales durante un juego y ofrece una experiencia envolvente para disfrutar imágenes, videos y libros. Sin embargo, ten en cuenta la frecuencia con la que los usuarios entran y salen de las apps para consultar notificaciones, realizar búsquedas espontáneas o realizar otras acciones. Como el modo envolvente hace que los usuarios pierdan el fácil acceso a la navegación del sistema, usa este modo solo cuando el beneficio para la experiencia del usuario vaya más allá del uso de espacio adicional en la pantalla.

Usa WindowInsetsControllerCompat.hide() para ocultar las barras del sistema y WindowInsetsControllerCompat.show() para recuperarlas.

En el siguiente fragmento, se muestra un ejemplo de cómo configurar un botón para ocultar y mostrar las barras del 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.
    window.decorView.setOnApplyWindowInsetsListener { 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())
            }
        }
        view.onApplyWindowInsets(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.
    getWindow().getDecorView().setOnApplyWindowInsetsListener((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 view.onApplyWindowInsets(windowInsets);
    });
}

De manera opcional, puedes especificar el tipo de barras del sistema para ocultar y determinar su comportamiento cuando un usuario interactúa con ellas.

Cómo especificar qué barras del sistema se deben ocultar

Para especificar el tipo de barras del sistema que se ocultarán, pasa uno de los siguientes parámetros a WindowInsetsControllerCompat.hide().

Cómo especificar el comportamiento de las barras del sistema ocultas

Usa WindowInsetsControllerCompat.setSystemBarsBehavior() para especificar cómo se comportan las barras ocultas del sistema cuando el usuario interactúa con ellas.