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

ステータスバーやナビゲーション バーのインジケーターを表示しない全画面表示が最適なコンテンツもあります。たとえば、動画、ゲーム、画像ギャラリー、書籍、プレゼンテーションのスライドなどです。これは没入型モードと呼ばれます。 このページでは、全画面表示を利用してユーザーに没入型のコンテンツを提供する方法について説明します。

図 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 を使用すると、 バーが非表示になっている画面の端からのスワイプなど、システム ジェスチャーによって非表示のシステムバーが一時的に表示されます。これらの一時的なシステムバーはアプリのコンテンツに重ねて表示され、透明度がある場合があります。また、短いタイムアウト後に自動的に非表示になります。