Prevent android activity from expanding into navigation bar

Viewed 29

I am using a NativeActivity with the style @android:style/Theme.DeviceDefault.NoActionBar.FullScreen. However, the Activity expanded behind the system navigation bar, which is not what I want.

(Black region on the left is due to device cutout, which is what I intended)

I tried to search about this topic and couldn't find posts addressing this problem. Any help would be appreciated.

Unintended behavior

The effect that I intend would be like: Intended behavior

1 Answers

You can try this function. It will remove only status bar. Call this on onCreate().

private fun hideStatusBar() {
    WindowCompat.getInsetsController(window,window.decorView).apply {
        systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
        hide(WindowInsetsCompat.Type.statusBars())
    }
}

To hide only navigation bar, use hide(WindowInsetsCompat.Type.navigationBars()) To hide both status and navigation bar, use hide(WindowInsetsCompat.Type.systemBars())

Additional thing, if you need this in multiple activities, you can create a BaseActivity and call this in BaseActivity on create and extend required activities with BaseActivity. So it will work for activities that extend BaseActivity

Here is Java Equivalent.

private void hideSystemBars() {
    WindowInsetsControllerCompat windowInsetsController =
            WindowCompat.getInsetsController(getWindow(),getWindow().getDecorView());
    windowInsetsController.setSystemBarsBehavior(
            WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    );
    windowInsetsController.hide(WindowInsetsCompat.Type.statusBars());
}

Hope it helps Read more here

Related