40

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.

kingston
  • 9,372
  • 12
  • 51
  • 96
Adifyr
  • 2,350
  • 6
  • 35
  • 55
  • 4
    I'm having same issue with my 5.0 emulator. Statusbar does not change color. Same code on a real device works fine. – Patrick Oct 24 '14 at 13:58
  • ok cool. Then I will try it on a real device and see. Thanks. – Adifyr Oct 25 '14 at 02:14
  • 1
    Same problem here. With Emulator of Android 5.0 it doesn't work. It did work with L-developer preview. Sad. – greywolf82 Oct 25 '14 at 11:36
  • Did you find a solution to this? I currently can't get colorPrimaryDark to color the status bar on 5.0 – MattWilliams89 Oct 30 '14 at 19:42
  • @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 Nov 06 '14 at 06:48
  • I'm finding colorPrimaryDark colors the status bar if you use the Galaxy Nexus device in the emulator (or a xhdpi or xxhdp?). Unfortunately it also colors the toolbar as well. – Dan Brough Dec 03 '14 at 03:01
  • I have the same issue. On device the status bar color is shown correctly as expected but when I try it on emulator it doesn't work. I tried emulator with 5.0.2 also 5.1. If anyone has found solution please share. Thanks in advance. – Abhijit Kurane Apr 11 '15 at 07:02

11 Answers11

35

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);
tomrozb
  • 23,522
  • 30
  • 89
  • 110
Wayne
  • 6,093
  • 10
  • 43
  • 65
35

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));
    }
Philipp E.
  • 3,129
  • 3
  • 31
  • 52
12

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.

BladeCoder
  • 11,697
  • 2
  • 51
  • 46
  • 1
    Yes. I had done everything. Turns out the problem is that the emulator and the layout preview are unable to draw the status bar color. The same code is said to work on a real device running lollipop – Adifyr Nov 19 '14 at 13:04
  • It doesn't work in the preview but it does work in the Lollipop emulator. If you open the phone dialer app in the emulator, you should see a blue status bar for example. – BladeCoder Nov 20 '14 at 09:02
7

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

gkiko
  • 2,275
  • 3
  • 27
  • 47
6

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

Tryagain Tsai
  • 69
  • 1
  • 3
3

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.

Patrick
  • 14,618
  • 20
  • 77
  • 136
  • I'm aware of that. But the fact that my emulator and my XML Layout Editor Preview are both running API level 21 and it still doesn't work, is where my dilemma arises. – Adifyr Oct 24 '14 at 02:16
  • Hi @patrick, I am new to Android. If Statusbar colors can't be changed with AppCompat library. Then all the google apps are using some other ways to change the status bar colors in their apps? I am using Kitkat and i can see their apps changing the status bar colors. Your view appreciated on this... – Raju Nov 04 '14 at 12:42
  • @javaman sure we are both talikinh about the status bar? The bar at the top of screen with the clock and battery? – Patrick Nov 04 '14 at 13:39
1

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.

mehulmpt
  • 13,725
  • 12
  • 41
  • 79
0

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!

AndiGeeky
  • 10,644
  • 6
  • 46
  • 64
Adifyr
  • 2,350
  • 6
  • 35
  • 55
0

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.

AndiGeeky
  • 10,644
  • 6
  • 46
  • 64
Starrover
  • 259
  • 1
  • 5
  • 12
0

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.

Umer Farooq
  • 7,030
  • 6
  • 40
  • 58
0

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));
}
Mwongera808
  • 762
  • 10
  • 15