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

Parte del contenido se visualiza mejor en pantalla completa sin indicadores en la barra de estado o en la de navegación. Algunos ejemplos son videos, juegos, galerías de imágenes, libros y diapositivas de presentación. Esto se conoce como modo envolvente. En esta página, se muestra cómo puedes atraer a los usuarios de manera más profunda con el 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 de 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. Dado que el modo envolvente hace que los usuarios pierdan el acceso fácil a la navegación del sistema, úsalo solo cuando el beneficio para la experiencia del usuario vaya más allá del simple 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.
    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);
    });
}

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 las barras del sistema que se ocultarán

Para especificar el tipo de barras del sistema que quieres ocultar, 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.