Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set and reset status bar color to original based on some condition in android

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

like image 747
Sauda Sadaf Avatar asked Oct 24 '25 12:10

Sauda Sadaf


1 Answers

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);
        }
    }
}
like image 60
Thiago Avatar answered Oct 26 '25 02:10

Thiago



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!