the status bar changes it's color to black inside fullscreen dialog fragment android

26.9k Views Asked by At

I'm using dialog fragment. The problem is that the status bar color is changed to black. How to change it to some other color? It's strange cause inside fragment, activity it works fine. Its only black inside DialogFragment

        @Override
            public void onStart() {
                super.onStart();    //super.onStart() is where dialog.show() is actually called on the underlying dialog, so we have to do it after this point
                Dialog d = getDialog();
                if (d != null) {

                    int width = ViewGroup.LayoutParams.MATCH_PARENT;
                    int height = ViewGroup.LayoutParams.MATCH_PARENT;
                    d.getWindow().setLayout(width, height);
                    d.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

                }
            }
     @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Dialog dialog = new Dialog(getActivity(), R.style.full_screen_dialog);
            return dialog;

}
7

There are 7 best solutions below

3
On BEST ANSWER

I just posted the solution to this problem here

Add following theme to res/value-v21/style

<style name="DialogTheme" parent="@style/Base.Theme.AppCompat.Light.Dialog">
     <item name="android:windowTranslucentStatus">true</item>
</style>

And then apply Style on DialogFragment in onCreate

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogTheme);
}
0
On

in kotlin

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        window?.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window?.statusBarColor = Color.parseColor("#0173B7")
    }
0
On

GUYS THE BEST SOLUTION IS IN THE WEBSITE LINK I AM POSTING HERE

https://zocada.com/android-full-screen-dialogs-using-dialogfragment/

I'll also upload the code here for reference

1st create a style FullScreenDialogStyle in your style file :

<style name="FullScreenDialogStyle" parent="Theme.AppCompat.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorPrimary">@color/colorPrimary</item>

    <!-- Set this to true if you want Full Screen without status bar -->
    <item name="android:windowFullscreen">false</item>

    <item name="android:windowIsFloating">false</item>

    <!-- This is important! Don't forget to set window background -->
    <item name="android:windowBackground">@color/colorWhite</item>

    <!-- Additionally if you want animations when dialog opening -->
    <!--<item name="android:windowEnterAnimation">@anim/slide_up</item>-->
    <!--<item name="android:windowExitAnimation">@anim/slide_down</item>-->
</style>

inside your dialogFragment class override a method onCreate

 @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL,R.style.FullScreenDialogStyle);

    }

then override Onstart method, through which u can access the getDialog() and set the height and width

@Override
public void onStart() {
    super.onStart();
    Dialog dialog = getDialog();
    if (dialog != null) {
        int width = ViewGroup.LayoutParams.MATCH_PARENT;
        int height = ViewGroup.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setLayout(width, height);
    }
}
0
On

To change Status Bar Color under DialogFragment, set below style to your dialogFragment under onCreate Method.

like:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setStyle(DialogFragment.STYLE_NO_TITLE,R.style.FullScreenDialogWithStatusBarColorAccent)
}

Add this style in style.xml

 <style name="FullScreenDialogWithStatusBarColorAccent">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/white</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:statusBarColor">@color/colorAccent</item>


    <!--<item name="android:windowAnimationStyle">@style/MateriDocumentAlertsActivityalDialogSheetAnimation</item>-->
</style>
0
On

A little late to the party but setting IS_FLOATING flag worked for me.

<style name="MyTheme.AlertDialog.FullScreen" parent="MyTheme.AlertDialog">
    <item name="windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowAnimationStyle">@style/Animation.AppCompat.Dialog</item>
    <item name="android:windowBackground">@color/white</item>
</style>
1
On

You have to set FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDSto indicate that this Window is responsible for drawing the background for the system bars.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
         dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
         dialog.getWindow().setStatusBarColor(yourColor); 
    }
0
On

here is the solution i found:

add this to your style.xml file

<style name="BottomSheetDialogTheme" parent="BaseBottomSheetDialog">
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

<!-- set the rounded drawable as background to your bottom sheet -->
<style name="BottomSheet" parent="@style/Widget.Design.BottomSheet.Modal">
    <item name="android:background">@color/transparent</item>
</style>

<style name="BaseBottomSheetDialog" parent="@style/Theme.Design.Light.BottomSheetDialog">
    <item name="android:windowIsFloating">false</item>
    <item name="bottomSheetStyle">@style/BottomSheet</item>
</style>

then you should override onCreate() method in BottomSheetDialogFragment class and call:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.BottomSheetDialogTheme);

}