How to change the status bar color in Android?
Android 5.0 Lollipop introduced Material Design theme which automatically colors the status bar based on the colorPrimaryDark
value of the theme.
This is supported on device pre-lollipop thanks to the library support-v7-appcompat starting from version 21. Blogpost about support appcompat v21 from Chris Banes
Read more about the Material Theme on the official Android Developers website
Update:
Lollipop:
public abstract void setStatusBarColor (int color)
Added in API level 21
Android Lollipop brought with it the ability to change the color of status bar in your app for a more immersive user experience and in tune with Google’s Material Design Guidelines
.
Here is how you can change the color of the status bar using the new window.setStatusBarColor
method introduced in API level 21
.
Changing the color of status bar also requires setting two additional flags on the Window; you need to add the FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
flag and clear the FLAG_TRANSLUCENT_STATUS
flag.
Working Code:
import android.view.Window;
...
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 windowwindow.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);// finally change the colorwindow.setStatusBarColor(ContextCompat.getColor(activity,R.color.my_statusbar_color));
Offcial developer reference : setStatusBarColor(int)
Example :material-design-everywhere
Chris Banes Blog- appcompat v21: material design for pre-Lollipop devices!
The transitionName
for the view background will be android:status:background
.
Place this is your values-v21/styles.xml, to enable this on Lollipop:
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light"> <item name="colorPrimary">@color/color_primary</item> <item name="colorPrimaryDark">@color/color_secondary</item> <item name="colorAccent">@color/color_accent</item> <item name="android:statusBarColor">@color/color_primary</item> </style></resources>