40
votes

I'm using the AppCompat-v7:21.0.0 support library for Android 5.0 Lollipop in Android Studio. My problem is that the Status Bar Color that can be changed by setting colorPrimaryDark in the values/styles.xml file, is showing up as black, in both the xml layout preview and the emulator.

So what's wrong? Am I missing something? Please let me know. Thanks.

EDIT: I'm aware of the fact that changing the status bar color on Pre-Lollipop versions is not possible. My XML Layout Editor Preview and my Emulator are both set to API Level 21 (5.0 Lollipop). But, the status bar still isn't of the color I set it to in colorPrimaryDark. I tried doing statusBarColor in styles.xml but to no avail. It's still black.

ALSO: I saw one of the answers on a similar question where they advised me to put my minSdkVersion to 21. I tried that, but it didn't work. And I want my app to run on devices with API Level 15 and above.

11
I'm having same issue with my 5.0 emulator. Statusbar does not change color. Same code on a real device works fine.Patrick
ok cool. Then I will try it on a real device and see. Thanks.Adifyr
Same problem here. With Emulator of Android 5.0 it doesn't work. It did work with L-developer preview. Sad.greywolf82
Did you find a solution to this? I currently can't get colorPrimaryDark to color the status bar on 5.0MattWilliams89
@MattWilliams89 is the problem on your device or the emulator? On the emulator and the layout.xml on the android studio, the status bar won't show up as colored. On the device however, according to what Patrick said on the above comment, it should work just fine. If it doesn't work on the device either, check out the below answer that I marked and see if that solves your problem... :)Adifyr

11 Answers

35
votes

This worked for me:

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().setStatusBarColor(getResources().getColor(R.color.some_color));
    }
35
votes

Please read this: For this to take effect, the window must be drawing the system bar backgrounds with

android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS

but

android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS

must not be set (Source)

In case of you don't know how to add that flag:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
12
votes

Did you set the target SDK version to 21? I had the same issue when I left the target SDK version to 19. You can leave the min SDK to anything lower.

And of course you need to inherit from the proper theme and make sure your Activity uses it.

7
votes

Check if you are editing styles.xml in values-v21 folder. If you set SDK version to 21 then it won't look for styles.xml in values folder(but it should do so).

enter image description here

6
votes

test on my nexus7 5.1.1

set in style.xml v21/v22 is not work

<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/holo_red_dark</item>

but

set in actvivity

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    getWindow().setStatusBarColor(getResources().getColor(android.R.color.holo_red_dark));
}

is work for me

3
votes

Changing the Status Bar color in pre-Lollipop(5.0) is not possible by setting colorPrimaryDark. See this article.

On older platforms, AppCompat emulates the color theming where possible. At the moment this is limited to coloring the action bar and some widgets.

1
votes

In my case values-v21/styles.xml contained the following line which overrode the status bar color defined in values/style.xml with colorPrimaryDark:

<item name="android:statusBarColor">@android:color/transparent</item>

Removing this worked for me.

0
votes

In my case, the culprit was jfeinstein10/SlidingMenu library. I replaced the library with Android navigation drawer and now it displays status bar color correctly.

0
votes

In v22-appcompat they (Android) have now added rendering of the status bar color in the android studio preview!

About time... Anyways, make sure your appcompat library is updated to the latest version, which is v22.0.+.

Cheers!

0
votes

I was working on an old app and trying to convert it to material style. The code and everything was fine however the only mistake I had that was hindering the status bar tinting on >= Lollipop devices was "TargetSDKVersion" in build.gradle. It was set to less then 21. I changed it to 21 and statusbar tinting started working.

0
votes

This worked for me. Removed the status bar color from styles. Add flag and then put the color you want like so

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    getWindow().setStatusBarColor(getResources().getColor(R.color.your_color));
}