Change Status Bar color when entering Contextual Action Mode
Asked Answered
H

3

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.

Hypothesis answered 14/4, 2015 at 3:6 Comment(4)
Have you tried Window.setStatusBarColor()?Coster
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.Hypothesis
Do you mean that actually you want to change the value of the attribute colorPrimaryDark, rather than the status bar color directly?Coster
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.Hypothesis
B
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); 
        }
        ...
    }
});
Braden answered 20/4, 2015 at 13:9 Comment(4)
Is there a way to make it through xml (styles)? I searched, but not found a working solution.Pistachio
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?Gillard
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.Lek
@Gillard 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. 😅Dorfman
N
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>
Nought answered 16/4, 2020 at 21:33 Comment(0)
N
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>
Nougat answered 25/2, 2022 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.