Menyembunyikan kolom sistem untuk mode imersif

Beberapa konten paling nyaman dinikmati dalam layar penuh tanpa indikator apa pun di status bar atau menu navigasi. Beberapa contohnya adalah video, {i>game<i}, galeri gambar, buku, dan {i>slide<i} presentasi. Mode ini disebut sebagai mode imersif. Halaman ini menunjukkan cara berinteraksi secara lebih mendalam dengan pengguna dengan konten dalam layar penuh.

Gambar 1. Contoh mode imersif.

Mode imersif membantu pengguna menghindari keluar yang tidak disengaja selama bermain game dan memberikan pengalaman yang imersif untuk menikmati gambar, video, dan buku. Namun, perhatikan seberapa sering pengguna masuk dan keluar aplikasi untuk memeriksa notifikasi, melakukan penelusuran secara mendadak, atau melakukan tindakan lain. Karena mode imersif menyebabkan pengguna kehilangan akses mudah ke navigasi sistem, gunakan mode imersif hanya jika manfaat pengalaman pengguna lebih dari sekadar menggunakan ruang layar tambahan.

Gunakan WindowInsetsControllerCompat.hide() untuk menyembunyikan kolom sistem dan WindowInsetsControllerCompat.show() untuk menampilkannya kembali.

Cuplikan berikut menunjukkan contoh konfigurasi tombol untuk menyembunyikan dan menampilkan kolom sistem.

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

Secara opsional, Anda dapat menentukan jenis kolom sistem yang akan disembunyikan dan menentukan perilakunya saat pengguna berinteraksi dengannya.

Menentukan kolom sistem yang akan disembunyikan

Untuk menentukan jenis kolom sistem yang akan disembunyikan, teruskan salah satu parameter berikut ke WindowInsetsControllerCompat.hide().

Menentukan perilaku kolom sistem tersembunyi

Gunakan WindowInsetsControllerCompat.setSystemBarsBehavior() untuk menentukan perilaku bar sistem tersembunyi saat pengguna berinteraksi dengannya.