483

First of all it's not a duplicate as in How to change the background color of android status bar

How do I change the status bar color which should be same as in navigation bar.

I want the status bar color to be same as the navigation bar color

enter image description here

Jonik
  • 74,291
  • 66
  • 249
  • 356
codercat
  • 21,439
  • 9
  • 56
  • 84

22 Answers22

705

Android 5.0 Lollipop introduced Material Design theme which automatically colors the status bar based on the colorPrimaryDark value of the theme.

This is supported on device pre-lollipop thanks to the library support-v7-appcompat starting from version 21. Blogpost about support appcompat v21 from Chris Banes

enter image description here

Read more about the Material Theme on the official Android Developers website

Dmitry
  • 13,126
  • 20
  • 95
  • 177
Giorgio
  • 11,673
  • 12
  • 42
  • 72
  • 32
    in some reason colorPrimaryDark attribute doesnt work for me. tried on emulator v21 – deviant Oct 19 '14 at 20:21
  • 3
    It works you have to see the changes on the emulator not on the layout preview[it doesn't show there] – Mightian Oct 29 '14 at 10:37
  • 7
    colorPrimaryDark isn't working for me either on an API 18 emulator. From the article linked about appcompat: "On older platforms, AppCompat emulates the color theming where possible. At the moment this is limited to coloring the action bar and some widgets." – Adam Johns Feb 03 '15 at 22:15
  • 15
    What about changing the **font** color in the status bar? The docs don't say anything about that. – SMBiggs Aug 07 '15 at 16:21
  • I tried the same way. it works perfectly in most of all devices, but i have samsung galaxy s4 running OS 5.0.1 and in this phone status bar color is black. And once i go to another activity and comes again to previous activity my status bar color is changed to app theme color. Any reason why this is happening – Hitesh Kamani Sep 09 '16 at 05:06
  • Now in `themes.xml` it wil be `colorPrimaryVariant` instead of `colorPrimaryDark` – Reejesh PK May 07 '21 at 08:01
  • What if I want it to be `windowBackground` when no action bar theme is set? I don't want to do it programatically. – Minh Nghĩa May 18 '21 at 03:14
401

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!

enter image description here

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

Black
  • 12,789
  • 26
  • 116
  • 196
codercat
  • 21,439
  • 9
  • 56
  • 84
  • 2
    Any solution for backwards compatibility? – Adam Hurwitz Apr 17 '17 at 23:13
  • As noted in the answer by Niels: you can also configure the status bar color via theme/style by setting `android:statusBarColor` in your `values-v21/styles.xml` file per http://www.androiddocs.com/training/material/theme.html like so `@color/primary_color` – JL West Dec 17 '18 at 15:33
  • 6
    i can change the status bar color to white, but i can't see the notification icon like batery, how can i solve this? – MNFS Jul 24 '19 at 10:28
250

Place this is your values-v21/styles.xml, to enable this on Lollipop:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/color_primary</item>
        <item name="colorPrimaryDark">@color/color_secondary</item>
        <item name="colorAccent">@color/color_accent</item>
        <item name="android:statusBarColor">@color/color_primary</item>
    </style>
</resources>
Niels
  • 8,614
  • 4
  • 27
  • 23
50

this is very easy way to do this without any Library: if the OS version is not supported - under kitkat - so nothing happend. i do this steps:

  1. in my xml i added to the top this View:
<View
        android:id="@+id/statusBarBackground"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

then i made this method:

 public void setStatusBarColor(View statusBar,int color){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
           Window w = getWindow();
           w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
           //status bar height
           int actionBarHeight = getActionBarHeight();
           int statusBarHeight = getStatusBarHeight();
           //action bar height
           statusBar.getLayoutParams().height = actionBarHeight + statusBarHeight;
           statusBar.setBackgroundColor(color);
     }
}

also you need those both methods to get action Bar & status bar height:

public int getActionBarHeight() {
    int actionBarHeight = 0;
    TypedValue tv = new TypedValue();
    if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
    {
       actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
    }
    return actionBarHeight;
}

public int getStatusBarHeight() {
    int result = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

then the only thing you need is this line to set status bar color:

setStatusBarColor(findViewById(R.id.statusBarBackground),getResources().getColor(android.R.color.white));
itzhar
  • 11,479
  • 6
  • 50
  • 57
45

For Java Developers:

As @Niels said you have to place in values-v21/styles.xml:

<item name="android:statusBarColor">@color/black</item>

But add tools:targetApi="lollipop" if you want single styles.xml, like:

<item name="android:statusBarColor" tools:targetApi="lollipop">@color/black</item>

For Kotlin Developers:

window.statusBarColor = ContextCompat.getColor(this, R.color.color_name)
Faisal Shaikh
  • 3,131
  • 2
  • 34
  • 66
  • 4
    Thank's alot for "targetApi". I don't like having my theme spread out across multiple files. :) – plexus Jan 21 '19 at 11:03
39

You can use this simple code:

One-liner in Kotlin:

window.statusBarColor = ContextCompat.getColor(this, R.color.colorName)

Original answer with Java & manual version check:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    getWindow().setStatusBarColor(getResources().getColor(R.color.colorAccentDark_light, this.getTheme()));
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    getWindow().setStatusBarColor(getResources().getColor(R.color.colorAccentDark_light));
}
sunadorer
  • 3,700
  • 29
  • 40
steve moretz
  • 1,900
  • 10
  • 21
35

To change the color for above lolipop just add this to your styles.xml

<item name="android:statusBarColor">@color/statusBarColor</item>

but remember, if you want to have a light color for the status bar, add this line too

<item name="android:windowLightStatusBar">true</item>
Amin Keshavarzian
  • 2,335
  • 1
  • 26
  • 28
31

Well, Izhar solution was OK but, personally, I am trying to avoid from code that looks as this:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//Do what you need for this SDK
};

As well, I don't like to duplicate code either. In your answer I have to add such line of code in all Activities:

setStatusBarColor(findViewById(R.id.statusBarBackground),getResources().getColor(android.R.color.white));

So, I took Izhar solution and used XML to get the same result: Create a layout for the StatusBar status_bar.xml

<View xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="@dimen/statusBarHeight"
     android:background="@color/primaryColorDark"
     android:elevation="@dimen/statusBarElevation">

Notice the height and elevation attributes, these will be set in values, values-v19, values-v21 further down.

Add this layout to your activities layout using include, main_activity.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/Black" >

<include layout="@layout/status_bar"/>
<include android:id="@+id/app_bar" layout="@layout/app_bar"/>
//The rest of your layout       
</RelativeLayout>

For the Toolbar, add top margin attribute:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@color/primaryColor"
app:theme="@style/MyCustomToolBarTheme"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
android:elevation="@dimen/toolbarElevation"
android:layout_marginTop="@dimen/appBarTopMargin"
android:textDirection="ltr"
android:layoutDirection="ltr">

In your appTheme style-v19.xml and styles-v21.xml, add the windowTranslucent attr:

styles-v19.xml, v21:

<resources>
<item name="android:windowTranslucentStatus">true</item>
</resources>

And finally, on your dimens, dimens-v19, dimens-v21, add the values for the Toolbar topMargin, and the height of the statusBarHeight: dimens.xml for less than KitKat:

<resources>
<dimen name="toolbarElevation">4dp</dimen>
<dimen name="appBarTopMargin">0dp</dimen>
<dimen name="statusBarHeight">0dp</dimen>
</resources>

The status bar height is always 24dp dimens-v19.xml for KitKat and above:

<resources>
<dimen name="statusBarHeight">24dp</dimen>
<dimen name="appBarTopMargin">24dp</dimen>
</resources>

dimens-v21.xml for Lolipop, just add the elevation if needed:

<resources>
<dimen name="statusBarElevation">4dp</dimen>
</resources>

This is the result for Jellybean KitKat and Lollipop:

enter image description here

Moti Bartov
  • 2,642
  • 22
  • 31
23

Just create a new theme in res/values/styles.xml where you change the "colorPrimaryDark" which is the color of the status bar:

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

And modify the activity theme in AndroidManifest.xml to the one you want, on the next activity you can change the color back to the original one by selecting the original theme:

<activity
    android:name=".LoginActivity"
    android:theme="@style/AppTheme.GrayStatusBar" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

This is how your res/values/colors.xml should look like:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#c6d6f0</color>
    <color name="colorGray">#757575</color>
</resources>
lcompare
  • 731
  • 8
  • 8
  • 1
    I think this is the simplest yet cleanest solution. **No need to have minimum API level of 21**, just simple overriding. Deserves more up-votes. – tahsinRupam Aug 10 '18 at 17:45
  • This didn't work when running on Nougat and api level set to 19. It did work if I set android:theme under the application node in the manifest. – AndroidDev May 16 '19 at 08:44
18

You can change the status bar color with this function. works on android L means API 21 and higher and needs a color string such as "#ffffff".

private void changeStatusBarColor(String color){
    if (Build.VERSION.SDK_INT >= 21) {
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.parseColor(color));
    }
}
sajad abbasi
  • 1,620
  • 2
  • 15
  • 36
15

I had this requirement: Changing programmatically the status bar color keeping it transparent, to allow the Navigation Drawer to draw itself overlapping the trasparent status bar.

I cannot do that using the API

getWindow().setStatusBarColor(ContextCompat.getColor(activity ,R.color.my_statusbar_color)

If you check here in stack overflow everyone before that line of code set the transparency of the status bar to solid with

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)

I'm able to manage color and transparency of status bar like this:

  • Android 4: there's not much you can do, because you can't manage status bar color from the API ... the only thing you can do is to set the status bar as translucent and move a colored element of you UI under the status bar. To do this you need to play with

    android:fitsSystemWindows="false"
    

    in your main layout. This allows you to draw you layout under the status bar. Then you need to play with some padding with the top of your main layout.

  • Android 5 and above: you have to define a style with

    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    

    this allows the navigation drawer to overlap the status bar.

    Then to change the color keeping the status bar transparent you have to set the status bar color with

    drawerLayout.setStatusBarBackgroundColor(ContextCompat.getColor(activity, R.color.my_statusbar_color))
    

    where drawerLayout is defined like this

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    
DSoldo
  • 899
  • 9
  • 19
  • It worked for me without `true`. Anyway, after days of search, I found something that really works (tested only on Lolllipop for the moment). Thanks – DenisGL Jul 09 '18 at 20:45
  • i was having same problem and your answer helped me to solve this. – A.Gun Dec 17 '18 at 12:03
15

To Change the color of the status bar go to res/values-v21/styles.xml and color of status bar

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/color_primary</item>
        <item name="colorPrimaryDark">@color/color_secondary</item>
        <item name="colorAccent">@color/color_accent</item>
        <item name="android:statusBarColor">#0000FF</item>
    </style>
</resources>
Mark Keen
  • 7,535
  • 5
  • 25
  • 54
ranojan
  • 647
  • 6
  • 9
  • Hey, could you help me? I have no idea about Android and I can't start learning now. I have a simple app witch runs a html file. I'm dying to change the status bar color because that top and bottom on #000 is horrible to my palete. But I just can't, I can't even find the styles.xml file... If I could send you my apk and you set it to me I'll be fckng grateful! – Thiago Soubra Apr 28 '20 at 21:34
10

If you want to change the status bar color programmatically (and provided the device has Android 5.0). This is a simple way to change statusBarColor from any Activity and very easy methods when differents fragments have different status bar color.

 /**
 * @param colorId id of color
 * @param isStatusBarFontDark Light or Dark color
 */
fun updateStatusBarColor(@ColorRes colorId: Int, isStatusBarFontDark: Boolean = true) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        val window = window
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.statusBarColor = ContextCompat.getColor(this, colorId)
        setSystemBarTheme(isStatusBarFontDark)
    }
}

/** Changes the System Bar Theme.  */
@RequiresApi(api = Build.VERSION_CODES.M)
private fun setSystemBarTheme(isStatusBarFontDark: Boolean) {
    // Fetch the current flags.
    val lFlags = window.decorView.systemUiVisibility
    // Update the SystemUiVisibility depending on whether we want a Light or Dark theme.
    window.decorView.systemUiVisibility = if (isStatusBarFontDark) lFlags and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv() else lFlags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
}
Nabin
  • 995
  • 1
  • 12
  • 23
7

Edit the colorPrimary in the colors.xml in Values to the color you want the Status Bar to be. For example:

   <resources>
<color name="colorPrimary">#800000</color> // changes the status bar color to Burgundy
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="red">#FF0000</color>
<color name="white">#FFFFFF</color>
<color name="cream">#fffdd0</color>
<color name="burgundy">#800000</color>

PhilipS
  • 369
  • 4
  • 15
6

If you want to work on Android 4.4 and above, try this. I refer to Harpreet's answer and this link. Android and the transparent status bar

First, call setStatusBarColored method in Activity's onCreate method(I put it in a util class). I use a image here, you can change it to use a color.

public static void setStatusBarColored(Activity context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
    {
        Window w = context.getWindow();
        w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        int statusBarHeight = getStatusBarHeight(context);

        View view = new View(context);
        view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        view.getLayoutParams().height = statusBarHeight;
        ((ViewGroup) w.getDecorView()).addView(view);
        view.setBackground(context.getResources().getDrawable(R.drawable.navibg));
    }
}

public static int getStatusBarHeight(Activity context) {
    int result = 0;
    int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = context.getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

Before: before

After: after

The color of the status bar has been changed, but the navi bar is cut off, so we need to set the margin or offset of the navi bar in the onCreate method.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, (int)(this.getResources().getDimension(R.dimen.navibar_height)));
        layoutParams.setMargins(0, Utils.getStatusBarHeight(this), 0, 0);

        this.findViewById(R.id.linear_navi).setLayoutParams(layoutParams);
    }

Then the status bar will look like this.

status bar

Allan
  • 141
  • 1
  • 7
6

Solution is very Simple, put the following lines into your style.xml

For dark mode:

<item name="android:windowLightStatusBar">false</item>
<item name="android:statusBarColor">@color/black</item>
Sophia Gray
  • 159
  • 1
  • 12
4

This is what worked for me in KitKat and with good results.

public static void setTaskBarColored(Activity context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            Window w = context.getWindow();
            w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //status bar height
            int statusBarHeight = Utilities.getStatusBarHeight(context);

            View view = new View(context);
            view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            view.getLayoutParams().height = statusBarHeight;
            ((ViewGroup) w.getDecorView()).addView(view);
            view.setBackgroundColor(context.getResources().getColor(R.color.colorPrimaryTaskBar));
        }
    }
Harpreet
  • 2,578
  • 3
  • 31
  • 48
3

change the colorPrimaryDark to your desire color into the res/values/styles.xml file

    <resources>
        <color name="colorPrimary">#800000</color>
        <color name="colorPrimaryDark">#303F9F</color> //This Line
        <color name="colorAccent">#FF4081</color>
        <color name="red">#FF0000</color>
        <color name="white">#FFFFFF</color>
       <color name="cream">#fffdd0</color>
       <color name="burgundy">#800000</color>
    </resources>
2

One more solution:

final View decorView = w.getDecorView();
View view = new View(BaseControllerActivity.this);
final int statusBarHeight = UiUtil.getStatusBarHeight(ContextHolder.get());
view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight));
view.setBackgroundColor(colorValue);
((ViewGroup)decorView).addView(view);
Vova K.
  • 668
  • 6
  • 19
2

Just add these lines in your styles.xml file

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- This is used for statusbar color. -->
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <!-- This is used for statusbar content color. If statusbarColor is light, use "true" otherwise use "false"-->
    <item name="android:windowLightStatusBar">false</item>
</style>
Rubaiyat Jahan Mumu
  • 2,741
  • 1
  • 25
  • 32
0

i used this code to change status bar to transparent

    activity?.window?.setFlags(
        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
    )

to change it to color in style used this code i used in fragment in onDetach()

activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
Hamdy Abd El Fattah
  • 450
  • 1
  • 5
  • 10
0

If you want to set a custom drawable file use this code snippet

fun setCustomStatusBar(){
    if (Build.VERSION.SDK_INT >= 21) {
        val decor = window.decorView
        decor.viewTreeObserver.addOnPreDrawListener(object :
            ViewTreeObserver.OnPreDrawListener {
            override fun onPreDraw(): Boolean {
                decor.viewTreeObserver.removeOnPreDrawListener(this)
                val statusBar = decor.findViewById<View> 
                  (android.R.id.statusBarBackground)
                statusBar.setBackgroundResource(R.drawable.bg_statusbar)
                return true
            }
        })
    }
}
Ahmet
  • 266
  • 3
  • 9