Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change statusbar color on Fragment change [Android Lollipop]

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!

like image 998
user3634770 Avatar asked Nov 04 '14 19:11

user3634770


1 Answers

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
like image 61
ztan Avatar answered Oct 23 '22 05:10

ztan