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

一部のコンテンツは、ステータスバーやナビゲーション バーにインジケーターを表示せずに全画面表示することをおすすめします。動画、ゲーム、画像ギャラリー、書籍、 プレゼンテーションのスライドなどがこれに該当しますこれは、没入モードと呼ばれます。このページでは、全画面のコンテンツでユーザーをさらに引きつける方法について説明します。

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

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

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

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

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

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

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

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

  • WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE を使用すると、システム ジェスチャーによって、非表示のシステムバーを一時的に表示できます(バーが非表示になっている画面の端からスワイプするなど)。このような一時的なシステムバーは、アプリのコンテンツに重なって表示されます。ある程度の透明性が確保されている可能性があり、短いタイムアウト後に自動的に非表示になります。