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

7

19

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;

}
Lanza answered 4/1, 2017 at 16:3 Comment(0)
W
13

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);
}
Whitworth answered 24/1, 2017 at 7:47 Comment(3)
min required sdk is 19 for this solution.Abscond
DialogTheme can be put in res/value-v19/styleMelleta
Works, but causes dialog to go under the status bar, when you set lp.height = WindowManager.LayoutParams.MATCH_PARENT; and then getWindow().setAttributes(lp); in your dialog. So not ideal solution, sadly..Ketose
A
26

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); 
    }
Aeolipile answered 9/4, 2018 at 6:50 Comment(1)
This enabled me to set the status bar color while showing a Dialog but draws over the top of the transparency. Could be tricky to darken to the same transparency level as the Dialog.Martinson
W
13

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);
}
Whitworth answered 24/1, 2017 at 7:47 Comment(3)
min required sdk is 19 for this solution.Abscond
DialogTheme can be put in res/value-v19/styleMelleta
Works, but causes dialog to go under the status bar, when you set lp.height = WindowManager.LayoutParams.MATCH_PARENT; and then getWindow().setAttributes(lp); in your dialog. So not ideal solution, sadly..Ketose
S
13

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);
    }
}
Sawtelle answered 21/11, 2018 at 11:46 Comment(0)
C
4

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>
Cascarilla answered 2/12, 2019 at 11:10 Comment(0)
R
4

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);

}
Ratafia answered 21/4, 2020 at 11:56 Comment(0)
M
1

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")
    }
Mycorrhiza answered 6/5, 2021 at 12:47 Comment(0)
D
-1

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>
Danford answered 25/11, 2021 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.