How to change status bar color to match app in Lollipop? [Android]
Solution 1:
To change status bar color use setStatusBarColor(int color). According the javadoc, we also need set some flags on the window.
Working snippet of code:
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(ContextCompat.getColor(activity, R.color.example_color));
Keep in mind according Material Design guidelines status bar color and action bar color should be different:
- ActionBar should use primary 500 color
- StatusBar should use primary 700 color
Look at the screenshot below:
Solution 2:
Just add this in you styles.xml. The colorPrimary is for the action bar and the colorPrimaryDark is for the status bar.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:colorPrimary">@color/primary</item>
<item name="android:colorPrimaryDark">@color/primary_dark</item>
</style>
This picture from developer android explains more about color pallete. You can read more on this link.
Solution 3:
Another way to set the status bar color is through the style.xml.
To do that, create a style.xml file under res/values-v21 folder with this content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="android:Theme.Material">
<!-- darker variant for the status bar and contextual app bars -->
<item name="android:colorPrimaryDark">@color/blue_dark</item>
</style>
</resources>
Edit: as pointed out in comments, when using AppCompat the code is different. In file res/values/style.xml use instead:
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
<!-- Set AppCompat’s color theming attrs -->
<item name="colorPrimary">@color/my_awesome_red</item>
<item name="colorPrimaryDark">@color/my_awesome_darker_red</item>
<!-- Other attributes -->
</style>