97

In the new lollipop update I noticed that with the native Google apps the color of the status bar changes to match the action bar on the app you're running. I see it's on the Twitter app also so I'm guessing it's not exclusively Google who can do it.

Does anyone know how to do this if it is possible?

bbb
  • 1,479
  • 2
  • 15
  • 28

7 Answers7

223

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:

enter image description here

klimat
  • 24,711
  • 7
  • 63
  • 70
  • Is this supposed to work without using Material theme? – sealskej Nov 28 '14 at 15:16
  • 4
    Note that the effect may not show up in the emulator. For example, [this sample project](https://github.com/commonsguy/cw-omnibus/tree/master/MaterialDesign/StatusBarColor) tints the status bar on a Nexus 9 but not on an Android 5.0 emulator. – CommonsWare Nov 30 '14 at 15:10
  • @CommonsWare it works for me both: device and emulator(genymotion). – klimat Dec 01 '14 at 10:52
  • 1
    @mate: I was referring to the Android SDK emulator. Good to know that it is working on Genymotion. – CommonsWare Dec 01 '14 at 11:51
  • Thanks for this answer. Does this works on those devices that have android version under Kitkat ? – Azad Jan 07 '15 at 21:14
  • 3
    @Azad No, it dosen't. To use above snippet of code you have to ensure that your device running on API 21 or higher. – klimat Jan 08 '15 at 07:08
  • About changing statusBar color; is it matter difference between pre-lollipop or lollipop devices? If it is matter; how can I change my statusBar color on api level below lollipop? – TeachMeJava Jan 30 '15 at 02:14
  • What does it mean "700" vs "500" ? Isn't see 700 as the higher number, make it also have more color, thus making it closer to white ? – android developer Feb 28 '15 at 14:20
  • 2
    I'm working on a relatively simple project and I must say that I just used `getWindow().setStatusBarColor(activity.getResources().getColor(R.color.example_color));` and it worked perfectly. Not sure about the context in which the flags are strictly necessary. – Joaquin Iurchuk Mar 06 '15 at 03:48
  • @androiddeveloper // the meaning of 500 and 700 colors in android: http://graphicdesign.stackexchange.com/a/44453 – Youngjae Mar 19 '15 at 03:01
  • @Youngjae This is weird. so what's the math that's needed for this? Suppose you have #550000" , this is probably 500 (base), but how will you compute 600 and 400 ? do you convert the "55" it to decimal, and for 400 you add 15% and for 600 you subtract 10% (and you convert back to hexadecimal) ? – android developer Mar 19 '15 at 06:34
  • @androiddeveloper // agree. I have no idea. – Youngjae Mar 19 '15 at 06:36
  • Not working in my case. Gives exception : java.lang.NoSuchMethodError: android.view.Window.setStatusBarColor – WISHY Mar 25 '15 at 06:19
  • @WISHY you propably didn't run the code on device with Lollipop. – klimat Mar 25 '15 at 07:11
  • 3
    I want to show the color in pre lollipop devices also. How can it be done? – WISHY Mar 25 '15 at 07:13
  • Are the flags needed for Lollipop and above? I've just tried what @joaquin said and it's working fine as far as I only want to tint it on Lollipop and above. – Sotti May 08 '15 at 10:53
  • 1
    @WISHY You can't change the Status Bar Color on pre-Lollipop devices. – Joaquin Iurchuk May 08 '15 at 11:39
  • Having a color X for the action bar , what is the formula to get the best color for the status bar ? How can one convert from color 500 to color 700 ? – android developer May 26 '15 at 14:29
  • @androiddeveloper Read this one http://graphicdesign.stackexchange.com/questions/37520/how-does-one-generate-a-palette-similar-to-the-ones-in-googles-material-design – klimat May 27 '15 at 12:04
  • @mklimek None of them found out the formula. At best, they show other places with built in palettes colors... – android developer May 27 '15 at 16:12
  • Works on genymotion emulator running Google Nexus 5 API 21. – Simon Jul 11 '15 at 09:56
  • @WISHY The status bar is a system window owned by the operating system. On pre-5.0 Android devices, applications do not have permission to alter its color, so this is not something that the AppCompat library can support for older platform versions. The best AppCompat can do is provide support for coloring the ActionBar and other common UI widgets within the application. – Shubham A. Jul 17 '15 at 10:14
  • @mklimek HI, I want to set status bar color like in Google Maps App. Map should display behind status bar, i tried this library "com.readystatesoftware.systembartint:systembartint" but didn't worked for me. with your code status bar color is setting with RGB value only but not with ARGB value. I want to set tinted background with ARGB value. Please help me! – Onkar Nene Jun 11 '16 at 12:14
  • its very useful. be careful its works on api-21 and later. – Amintabar Jul 27 '16 at 11:31
48

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.

enter image description here

Gjoko Bozhinov
  • 1,137
  • 11
  • 15
  • Android Studio creates the following entries for each project: `#somecolor` and `#somecolor`. They can be changed to achieve the desired effect. – Neurotransmitter Apr 05 '16 at 21:17
41

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>
Levon
  • 1,681
  • 2
  • 18
  • 40
Alberto Malagoli
  • 1,173
  • 10
  • 14
23

To set the status bar color, create a style.xml file under res/values-v21 folder with this content:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppBaseTheme" parent="AppTheme">
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@color/blue</item>
    </style>

</resources>
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
4

Also if you want different status-bar color for different activity (fragments) you can do it with following steps (work on API 21 and above):

First create values21/style.xml and put following code:

 <style name="AIO" parent="AIOBase">
            <item name="android:windowDrawsSystemBarBackgrounds">true</item>
            <item name="android:windowContentTransitions">true</item>
    </style>

Then define White|Dark themes in your values/style.xml like following:

 <style name="AIOBase" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/color_primary</item>
        <item name="colorPrimaryDark">@color/color_primary_dark</item>
        <item name="colorAccent">@color/color_accent</item>
        <item name="android:textColorPrimary">@android:color/black</item>
        <item name="android:statusBarColor" tools:targetApi="lollipop">@color/color_primary_dark
        </item>
        <item name="android:textColor">@color/gray_darkest</item>
        <item name="android:windowBackground">@color/default_bg</item>
        <item name="android:colorBackground">@color/default_bg</item>
    </style>


    <style name="AIO" parent="AIOBase" />

    <style name="AIO.Dark" parent="AIOBase">
        <item name="android:statusBarColor" tools:targetApi="lollipop">#171717
        </item>
    </style>

    <style name="AIO.White" parent="AIOBase">
        <item name="android:statusBarColor" tools:targetApi="lollipop">#bdbdbd
        </item>
    </style>

Also don't forget to apply themes in your manifest.xml.

Community
  • 1
  • 1
Amir
  • 16,067
  • 10
  • 80
  • 119
4

Add this line in style of v21 if you use two style.

  <item name="android:statusBarColor">#43434f</item>
Amit Walke
  • 293
  • 3
  • 6
2

In android pre Lollipop devices you can do it from SystemBarTintManager If you are using android studio just add Systembartint lib in your gradle file.

dependencies {
    compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
    ...
}

Then in your activity

// create manager instance after the content view is set
SystemBarTintManager mTintManager = new SystemBarTintManager(this);
// enable status bar tint
mTintManager.setStatusBarTintEnabled(true);
mTintManager.setTintColor(getResources().getColor(R.color.blue));
wnafee
  • 2,126
  • 16
  • 23
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57