4

I want to change the color of status bar,and did the same as specified here . I have the following doubts. 1) but i want to reset status bar color to original based on some condition, how will i do that. I have tried like this.

public class StoreDisableHelper {

    public static void changeStatusBarColor(boolean status,Activity activity){
        Window window = activity.getWindow();
        if(status){


// clear FLAG_TRANSLUCENT_STATUS flag:
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

// finally change the color
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.setStatusBarColor(activity.getResources().getColor(R.color.theme_red));
            }

        }
        else{
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        }

    }
}

when the condition is false, i am resetting

Community
  • 1
  • 1
Sauda Sadaf
  • 139
  • 9
  • Can you please show what you have tried yet? Please take a look at our [how to ask guide](http://stackoverflow.com/help/how-to-ask) – dubes Mar 22 '16 at 13:41
  • see, on some condition like lets say on monday and tuesday i want to change the color of status bar to red, and on wednesday i want to reset to how it was previously without red color. How will i do that? I did it by re-setting the flag, but in devices below lollipop, its affecting the Title bar . – Sauda Sadaf Mar 24 '16 at 16:10
  • By "show what you have tried" I meant to provide some code samples. This will help experts from the field point you to errors if any. Right now, they will have to guess what you are doing! – dubes Mar 24 '16 at 16:34
  • Use AppCompat Library if you want your code to work same on all devices. – Sumanth Jois Mar 26 '16 at 04:13

1 Answers1

2

This this:

https://gist.github.com/MrThiago/4897424c3c773aec57081325f273311a

private static int defaultStatusBarColor;

public static void changeStatusBarColor(Activity context, boolean change, int color){
    if (Build.VERSION.SDK_INT >= 21)
    {
        Window window = context.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

        if(change){
            defaultStatusBarColor = window.getStatusBarColor();
            window.setStatusBarColor(color);
        }
        else{
            // reset
            window.setStatusBarColor(defaultStatusBarColor);
        }
    }
}
Thiago
  • 12,778
  • 14
  • 93
  • 110