Change statusbar color on Fragment change [Android Lollipop]

15,073

Have you tried to change the color of the status bar using Window.setStatusBarColor? For example, you can do the following to change the status bar color to red programmatically.

getWindow().setStatusBarColor(Color.RED);

The documentation of setStatusBarColor can be found here. You can also read the documentation on how to Customize the Status Bar.

Note: This method only works at Lollipop or above.

If you are seeing an unexpected color, make sure the integer value you are passing is a color integer and not a resource ID.

getWindow().setStatusBarColor(getResources().getColor(R.color.custom_color)); // RIGHT
getWindow().setStatusBarColor(R.color.custom_color); // WRONG
Share:
15,073
user3634770
Author by

user3634770

Updated on June 05, 2022

Comments

  • user3634770
    user3634770 about 2 years

    I'm wondering how to change the StatusBar color dependent on the current active Fragment (on 5.0 Lollipop). Currently, I change the ActionBar color dependent on the Fragment I am in, but now I want the StatusBar color to change as well - in order to look nice on Lollipop devices.

    I've tried using setStyle to programmatically change the theme depending on the fragment, but it doesn't seem to be changing the status bar color.

    Any ideas would be appreciated!

  • user3634770
    user3634770 over 9 years
    So, getWindow().setStatusBarColor(...) works to change the status bar color, but its not setting the right color. So for example, when I set the status bar color to orange, it comes out as purple. Any idea what's going on?
  • Amin Keshavarzian
    Amin Keshavarzian over 9 years
    is there a way to do the same in android 4.4 ?
  • ztan
    ztan over 9 years
    @AminKeshavarzian You have to use app compact, and do `<item name="colorPrimaryDark">@color/BackgroundColor</item>' in your style or theme xml to support android 4.4 devices.
  • Morteza Soleimani
    Morteza Soleimani about 9 years
    Changing status bar's color programmatically is just possible in API 21 ? I want to change the color programmatically in API less than 21 , Is it possible ?