21

I have an application that uses theme attribute (colorPrimaryDark) to color the Status Bar on Android v21+:

enter image description here

This is working fine. Now, when user long-presses a list item and enters the contextual action mode, I am able to color the CAB bar using attribute actionModeBackground so it looks like this:

enter image description here

So the action bar is gray, which is what I want, but the status bar is still colored using the theme dark color. I don't want that, I want to change it to dark gray or black.

How can I do this? I don't see any theme attribute that would work here.

Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
  • Have you tried [Window.setStatusBarColor()](http://developer.android.com/reference/android/view/Window.html#setStatusBarColor%28int%29)? – AndroidEx Apr 14 '15 at 03:24
  • 1
    It should be handled through theming like how all the other colors are... Otherwise I have to programmatically deal with changing the status bar color myself, and changing it back, which isn't making much sense to me. – Greg Ennis Apr 14 '15 at 03:26
  • Do you mean that actually you want to change the value of the attribute `colorPrimaryDark`, rather than the status bar color directly? – AndroidEx Apr 14 '15 at 03:30
  • No I just was thinking there must be a theme color like contextActionBackgroundDark and I must be missing it somehow but apparently sounds like there just isn't one. – Greg Ennis Apr 14 '15 at 03:33

3 Answers3

36
    private int statusBarColor;

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //hold current color of status bar
            statusBarColor = getWindow().getStatusBarColor();
            //set your gray color
            getWindow().setStatusBarColor(0xFF555555);
        }
        ...
    }

    ...

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //return to "old" color of status bar
            getWindow().setStatusBarColor(statusBarColor); 
        }
        ...
    }
});
user3484802
  • 376
  • 3
  • 5
  • 8
    Is there a way to make it through xml (styles)? I searched, but not found a working solution. – Mikhail Feb 10 '17 at 15:23
  • One problem for me with this solution is that the CAB and the status bar doesn't change color at the same time. Is there any way of getting both to change color simultaneously? – nibarius Aug 15 '17 at 20:14
  • 1
    You can also do that by overriding Activity's onActionModeStarted() and onActionModeFinished(). This comes handy, if you have multiple Activities with ActionMode and a BaseActivity they all inherit from. – Ridcully Apr 04 '18 at 07:53
  • @nibarius This problem is there because there is a delay in animating the color change for CAB and status bar. We need a way to sync these times. Even the latest Gmail app with Material 3, has this problem. Check it out. – Kahan Bhalani Feb 23 '22 at 10:39
0

Place it on your Application theme(both style.xml and v21 style.xml) or create custom theme for where you want to change.

<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/**YOUR_COLOR**</item>**
    </style>
</resources>
0

If you use AppCompat, then you can change color like this in your colors.xml

<color name="abc_decor_view_status_guard_light" tools:override="true">@color/colorBackgroundLight</color>
<color name="abc_decor_view_status_guard" tools:override="true">@color/colorBackgroundDark</color>
John
  • 1,447
  • 15
  • 16