setting status bar color android code example

Example 1: how to change status bar color

Window window = activity.getWindow();

// 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
window.setStatusBarColor(ContextCompat.getColor(activity,R.color.my_statusbar_color));

Example 2: reset status bar color in android

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

Tags:

Java Example