How to Change Status Bar Color to Match App in Lollipop? - Android

How to change status bar color to match app in Lollipop? [Android]

To change status bar color use setStatusBarColor(int color).
According the javadoc, we also need set some flags on the window.

Working snippet of code:

Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(ContextCompat.getColor(activity, R.color.example_color));



Keep in mind according Material Design guidelines status bar color and action bar color should be different:

  • ActionBar should use primary 500 color
  • StatusBar should use primary 700 color

Look at the screenshot below:

Sample Image

How to change the color of the status bar in Android?

You can change it by setting the android:statusBarColor or android:colorPrimaryDark attribute of the style you're using for your app in styles.xml.

(android:statusBarColor inherits the value of android:colorPrimaryDark by default)

For example (since we're using an AppCompat theme here, the android namespace is omitted):

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimaryDark">@color/your_custom_color</item>
</style>

On API level 21+ you can also use the Window.setStatusBarColor() method from code.

From its docs:

For this to take effect, the window must be drawing the system bar
backgrounds with
WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS and
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS must not be set.
If color is not opaque, consider setting
View.SYSTEM_UI_FLAG_LAYOUT_STABLE and
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN.

To set these flags you could do something like this:

// getWindow() is a method of Activity
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

How to change the status bar color in Android?

Update:

Lollipop:

public abstract void setStatusBarColor (int color)

Added in API level 21

Android Lollipop brought with it the ability to change the color of status bar in your app for a more immersive user experience and in tune with Google’s Material Design Guidelines.

Here is how you can change the color of the status bar using the new window.setStatusBarColor method introduced in API level 21.

Changing the color of status bar also requires setting two additional flags on the Window; you need to add the FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag and clear the FLAG_TRANSLUCENT_STATUS flag.

Working Code:

import android.view.Window;

...

Window window = activity.getWindow();

// clear FLAG_TRANSLUCENT_STATUS flag:
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

// finally change the color
window.setStatusBarColor(ContextCompat.getColor(activity,R.color.my_statusbar_color));

Offcial developer reference : setStatusBarColor(int)

Example :material-design-everywhere

Chris Banes Blog- appcompat v21: material design for pre-Lollipop devices!

Sample Image

The transitionName for the view background will be android:status:background.

Android lollipop change navigation bar color

It can be done inside styles.xml using

<item name="android:navigationBarColor">@color/theme_color</item>

or

window.setNavigationBarColor(@ColorInt int color)

http://developer.android.com/reference/android/view/Window.html#setNavigationBarColor(int)

Note that the method was introduced in Android Lollipop and won't work on API version < 21.

The second method (works on KitKat) is to set windowTranslucentNavigation to true in the manifest and place a colored view beneath the navigation bar.

Change status bar color with AppCompat ActionBarActivity

I'm not sure I understand the problem.

I you want to change the status bar color programmatically (and provided the device has Android 5.0) then you can use Window.setStatusBarColor(). It shouldn't make a difference whether the activity is derived from Activity or ActionBarActivity.

Just try doing:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.BLUE);
}

Just tested this with ActionBarActivity and it works alright.


Note: Setting the FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag programmatically is not necessary if your values-v21 styles file has it set already, via:

    <item name="android:windowDrawsSystemBarBackgrounds">true</item>


Related Topics



Leave a reply



Submit