3

How Can I set color for status bar in android?

I've tried both in styles.xml and in .java file.

If I try following code in .java class

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

I'm getting as exception called : -ResourceNotFoundException

04-01 18:55:21.616: E/AndroidRuntime(2169): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.cz/com.myapp.casenotez.updateCase}: android.content.res.Resources$NotFoundException: Resource ID #0xff444444
04-01 18:55:21.616: E/AndroidRuntime(2169):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
04-01 18:55:21.616: E/AndroidRuntime(2169):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
04-01 18:55:21.616: E/AndroidRuntime(2169):     at android.app.ActivityThread.-wrap11(ActivityThread.java)
04-01 18:55:21.616: E/AndroidRuntime(2169):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
04-01 18:55:21.616: E/AndroidRuntime(2169):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-01 18:55:21.616: E/AndroidRuntime(2169):     at android.os.Looper.loop(Looper.java:148)
04-01 18:55:21.616: E/AndroidRuntime(2169):     at android.app.ActivityThread.main(ActivityThread.java:5417)
04-01 18:55:21.616: E/AndroidRuntime(2169):     at java.lang.reflect.Method.invoke(Native Method)
 04-01 18:55:21.616: E/AndroidRuntime(2169):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
 04-01 18:55:21.616: E/AndroidRuntime(2169):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 04-01 18:55:21.616: E/AndroidRuntime(2169): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0xff444444
 04-01 18:55:21.616: E/AndroidRuntime(2169):    at android.content.res.Resources.getValue(Resources.java:1351)
 04-01 18:55:21.616: E/AndroidRuntime(2169):    at android.content.res.Resources.getColor(Resources.java:963)
 04-01 18:55:21.616: E/AndroidRuntime(2169):    at android.content.res.Resources.getColor(Resources.java:936)
 04-01 18:55:21.616: E/AndroidRuntime(2169):    at com.myapp.cz.updateCase.onCreate(updateCase.java:112)
 04-01 18:55:21.616: E/AndroidRuntime(2169):    at android.app.Activity.performCreate(Activity.java:6237)
 04-01 18:55:21.616: E/AndroidRuntime(2169):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
 04-01 18:55:21.616: E/AndroidRuntime(2169):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
 04-01 18:55:21.616: E/AndroidRuntime(2169):    ... 9 more

And I also tried by adding styles in styles.xml:-

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#630</item>
     <item name="android:titleTextStyle">       @style/MyActionBarTitle</item>
</style>
<style name="MyActionBarTitle" parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/Blue</item>
</style>
2
  • Check out this stackoverflow.com/a/27093330/4790490 Commented Apr 1, 2016 at 14:28
  • 1
    I don't think you have to call getResources().getColor(Color.DKGRAY)) I think you can just pass in Color.DKGRAY. The error says it's trying to get Resource ID #0xff444444 but that is the hex value for a solid Dark Gray color. Commented Apr 1, 2016 at 14:29

2 Answers 2

5

Color.DKGRAY is already a properly-formed color int. You can just use

getWindow().setStatusBarColor(Color.DKGRAY);

You only need to use Resources.getColor() if you are resolving a color resource id, like so:

<resources>
    <color name="dark_gray">#ff444444</color>
</resources>

getWindow().setBackgroundColor(getResources.getColor(R.color.dark_gray));

In this case, R.color.dark_gray is not a color int, it is a resource id. That's the difference.

EDIT

You can easily set the status bar color in your theme instead. Note that it only takes effect on Lollipop and above. You could use translucent status bar on KitKat. If you do it this way, you don't need any java code to set window flags.

Note that I am assuming you are using the AppCompat library. If you are not, you probably should.

in res/values/styles.xml:

<!-- Extend from any AppCompat theme -->
<style name="AppTheme" parent="Theme.AppCompat">
    <!-- put your theme customizations in here -->
</style>

<style name="AppTheme.TranslucentStatus" />

in res/values-19/styles.xml:

<style name="AppTheme.TranslucentStatus">
    <item name="android:windowTranslucentStatus">true</item>
</style>

in res/values-21/styles.xml:

<style name="AppTheme.TranslucentStatus">
    <item name="android:statusBarColor">#ff444444</item>
</style>

Use AppTheme.TranslucentStatus on the Activities that should have translucent status bar.

Sign up to request clarification or add additional context in comments.

1 Comment

OKay.. Thank you ..:) And Is it possible to set status bar color in style.xml ? In the above code which I have posted in question..Its changing only the color of texts of actionbar not status bar.?
1

This method is available in API Level 21,so you may want to check what the Target Framework setting is configured as within the project options.

    And, you will need to include a check like this to verify 

this only executes when running on Lollipop (API 21) or higher:

                if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
                {
                    Window.ClearFlags(WindowManagerFlags.TranslucentStatus);
                    Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
                    Window.SetStatusBarColor(Android.Graphics.Color.Blue);
                }

Or you may try this function...

public void setStatusBarColor(@ColorRes int statusBarColor) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    int color = ContextCompat.getColor(this, statusBarColor);

    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(color);
  }
}

1 Comment

OKay.. Thank you ..:) And Is it possible to set status bar color in style.xml ? In the above code which I have posted in question..Its changing only the color of texts of actionbar not status bar.?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.