Set color for Navigation Bar on DialogFragment

Viewed 2031

I have night theme with black navigation bar, styled in App Theme param

<item name="android:navigationBarColor">@color/color_navbar</item>
<item name="android:windowLightNavigationBar" tools:targetApi="o_mr1">false</item>

enter image description here

but when I show DialogFragment then navigation bar color changed to white. It hits eyes hard at night.
I tryed to set dialogfragment style but its not help me:

public void onCreate(Bundle savedInstanceState) {
    setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Theme_AppCompat_DayNight_Dialog);
    super.onCreate(savedInstanceState);
}

and result is white:
enter image description here

My base app style is Theme.MaterialComponents.DayNight.NoActionBar
I test it on android Q.
So what the way to customize navbar color on dialog fragment? Or better if there is a way to no change navbar color when dialogfragment showed and use color from activity which from showed this dialog.

2 Answers

this works for me:

window?.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window?.navigationBarColor = ContextCompat.getColor(activity, R.color.navigation_bar_color)

before set navigationBarColor, you need add flags. FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS,

enter image description here

I found solution. Just replace dialog height from math_parent to wrap_content and android automatically will use in dialogfragment the app color scheme for navigation bar.

@Override
public void onStart() {
        super.onStart();
        if (getDialog() != null) {
            int width = ViewGroup.LayoutParams.MATCH_PARENT;
            int height = ViewGroup.LayoutParams.WRAP_CONTENT;//previously I used MATCH_PARENT here 
            getDialog().getWindow().setLayout(width, height);
            getDialog().setOnKeyListener(onKeyListener);
        }
}
Related