没入モードのシステムバーを非表示にする

コンテンツによっては、ステータスバーやナビゲーション バーにインジケーターが表示されない全画面モードが最適です。たとえば、動画、ゲーム、イメージ ギャラリー、書籍、プレゼンテーション スライドなどです。これは没入モードと呼ばれます。このページでは、全画面表示のコンテンツでユーザーをさらに深く引き込む方法について説明します。

図 1. 没入モードの例。

没入モードは、ユーザーがゲーム中に誤って終了するのを防ぐとともに、画像、動画、書籍を楽しむ没入型エクスペリエンスを提供します。ただし、ユーザーが通知の確認、即席の検索などのためにアプリを切り替えてしまう頻度に留意してください。没入モードでは、ユーザーがシステム ナビゲーションに簡単にアクセスできなくなるため、単に追加の画面スペースを使用するだけではユーザー エクスペリエンスにとってのメリットが得られない場合にのみ、没入モードを使用してください。

システムバーを非表示にするには WindowInsetsControllerCompat.hide() を、元に戻すには WindowInsetsControllerCompat.show() を使用します。

次のスニペットは、システムバーの表示と非表示を切り替えるボタンの設定例を示しています。

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

必要に応じて、非表示にするシステムバーのタイプを指定して、ユーザーが操作したときの動作を指定できます。

非表示にするシステムバーを指定する

非表示にするシステムバーのタイプを指定するには、次のいずれかのパラメータを WindowInsetsControllerCompat.hide() に渡します。

非表示のシステムバーの動作を指定する

非表示のシステムバーをユーザーが操作したときの動作を指定するには、WindowInsetsControllerCompat.setSystemBarsBehavior() を使用します。

  • WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH を使用すると、対応するディスプレイ上のあらゆるユーザー操作で、非表示のシステムバーを表示できます。

  • WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_SWIPE を使用すると、システム ジェスチャー(バーが非表示になっている画面の端からのスワイプなど)で、非表示のシステムバーを表示できます。

  • WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE を使用すると、バーが非表示になっている画面の端からスワイプするなどのシステム ジェスチャーによって、非表示になっているシステムバーを一時的に表示できます。これらの一時的なシステムバーはアプリのコンテンツに重なり、ある程度の透明度を持つ場合がありますが、しばらくすると自動的に非表示になります。