Prevent BottomSheetDialogFragment covering navigation bar Prevent BottomSheetDialogFragment covering navigation bar android android

Prevent BottomSheetDialogFragment covering navigation bar


I had the same problem and I finally found a solution which is not hacky or needs an exorbitant amount of code.

This Method replaced the window background with a LayerDrawable which consists of two elements: the background dim and the navigation bar background.

@RequiresApi(api = Build.VERSION_CODES.M)private void setWhiteNavigationBar(@NonNull Dialog dialog) {    Window window = dialog.getWindow();    if (window != null) {        DisplayMetrics metrics = new DisplayMetrics();        window.getWindowManager().getDefaultDisplay().getMetrics(metrics);        GradientDrawable dimDrawable = new GradientDrawable();        // ...customize your dim effect here        GradientDrawable navigationBarDrawable = new GradientDrawable();        navigationBarDrawable.setShape(GradientDrawable.RECTANGLE);        navigationBarDrawable.setColor(Color.WHITE);        Drawable[] layers = {dimDrawable, navigationBarDrawable};        LayerDrawable windowBackground = new LayerDrawable(layers);        windowBackground.setLayerInsetTop(1, metrics.heightPixels);        window.setBackgroundDrawable(windowBackground);    }}

The method "setLayerInsetTop" requires the API 23 but that's fine because dark navigation bar icons were introduced in Android O (API 26).

So the last part of the solution is to call this method from your bottom sheets onCreate method like this.

@NonNull@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {    Dialog dialog = super.onCreateDialog(savedInstanceState);    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {        setWhiteNavigationBar(dialog);    }    return dialog;}

I hope it helps and please let me know if you find a device or case in which this solution does not work.

before and after


I know there is many solutions here already but all of them seems too much to me, so I found this very simple solution here, credit goes to Arthur Nagy:

just override the getTheme method in the BottomSheetDialogFragment:

override fun getTheme(): Int  = R.style.Theme_NoWiredStrapInNavigationBar

and in styles.xml:

<style name="Theme.NoWiredStrapInNavigationBar" parent="@style/Theme.Design.BottomSheetDialog">    <item name="android:windowIsFloating">false</item>    <item name="android:navigationBarColor">@color/bottom_sheet_bg</item>    <item name="android:statusBarColor">@android:color/transparent</item></style>

You can also add support for night mode by changing the color @color/bottom_sheet_bg in the values-night assets folder


Answer from j2esu works pretty well. However if you insist on 'completely white' navigation bar you have to omit part of it.

Please note that this solution is applicable from Android O (API 26) since dark navigation bar icons were introduced in this version. On older versions you would get white icons on white background.

You need to:

  1. Add android:fitsSystemWindows="true" to root of your dialog layout.
  2. Modify Window of your Dialog properly.

Place this code to onStart of your child of BottomSheetDialogFragment. If you are using design library instead of material library use android.support.design.R.id.container.

@Overridepublic void onStart() {    super.onStart();    if (getDialog() != null && getDialog().getWindow() != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {        Window window = getDialog().getWindow();        window.findViewById(com.google.android.material.R.id.container).setFitsSystemWindows(false);        // dark navigation bar icons        View decorView = window.getDecorView();        decorView.setSystemUiVisibility(decorView.getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);    }}

Result might look like this:

White navigation bar on Android P in dialog