Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set status bar color from DialogFragment

I'm creating an app which changes status bar color dynamically.

My method in main Activity class works fine when called from any fragment. Fragments are placed in Activity pager:

public void setStatusBarColorIfPossible(int color) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        getWindow().setStatusBarColor(color);
    }
}

However, in DialogFragment which is created in any fragment and made fullscreen, calling my method has no effect. Status bar is instantly black (as set in styles.xml) and I can't change it.

public class AddOrEditWeekPlanDialog extends DialogFragment {

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

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_dialog, container, false);
        Button button = (Button)root.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((ThermostatActivity) getActivity()).setStatusBarColorIfPossible(0xFF0D47A1);
            }
        });

        return root;
    }

    @Override
    public void onResume() {
        super.onResume();

    }
}

styles.xml:

// Set in manifest for main activity
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>

// Set in dialog class
<style name="MyDialog" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowIsFloating">false</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>

resources.xml:

<resources>
    <color name="colorPrimary">#FFB72020</color>
    <color name="colorPrimaryDark">#000</color>
    <color name="textColor">#fff</color>
    <color name="tabsScrollColor">#fff</color>
</resources>

Anyone can explain me why?

like image 876
Adrian Adamczyk Avatar asked Jul 29 '15 12:07

Adrian Adamczyk


People also ask

Is DialogFragment deprecated?

This method is deprecated. androidx. activity.

How do I know if DialogFragment is showing?

Showing the DialogFragment Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.

What is DialogFragment?

Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.


1 Answers

In order to setStatusBarColor you need to set the flag: FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS

"Flag indicating that this Window is responsible for drawing the background for the system bars." (Google API level 21)

Try this code:

public void setStatusBarColorIfPossible(int color) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
       getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
       getWindow().setStatusBarColor(color);
    }
}
like image 185
Raz Tourgman Avatar answered Sep 20 '22 06:09

Raz Tourgman