Stack Overflow archive
1 score

Change StatusBar color before Android 5.0

score
1
question views
150
license
CC BY-SA 3.0

You can use SystemBarTint or simply copy the static factory method below and call it in onCreate(Bundle savedInstanceState) in your Activity:

java
@SuppressLint("NewApi")
public static void setStatusBarColor(Activity activity, int color) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        activity.getWindow().setStatusBarColor(color);
    } else {
        try {
            ViewGroup decorViewGroup = (ViewGroup) activity.getWindow().getDecorView();
            View statusBarView = new View(activity);
            Resources res = activity.getResources();
            int id = res.getIdentifier("status_bar_height", "dimen", "android");
            LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    res.getDimensionPixelSize(id));
            params.gravity = Gravity.TOP;
            statusBarView.setLayoutParams(params);
            statusBarView.setBackgroundColor(color);
            decorViewGroup.addView(statusBarView);
        } catch (Exception ignored) {
            // status-bar color cannot be changed.
        }
    }
}

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.