#StackBounty: #android #android-actionbar #android-toolbar #contextual-action-bar How to change status bar color on entering contextual…

Bounty: 100

My application uses the app theme that inherits Theme.MaterialComponents.Light.NoActionBar. When user selects a list item and enters the contextual action mode, I want to change the action bar to dark grey color. I am using following code to achieve same:

In themes.xml:

<item name="windowActionModeOverlay">true</item>
<item name="actionModeStyle">@style/Widget.App.ActionMode</item>
<item name="actionModeCloseDrawable">@drawable/ic_close_24dp</item>
<item name="actionBarTheme">@style/ThemeOverlay.MaterialComponents.Dark.ActionBar</item>

In styles.xml:

<style name="Widget.App.ActionMode" parent="Widget.AppCompat.ActionMode">
    <item name="background">@color/grey_100</item>
</style>

In my fragment:

val callback = object: ActionMode.Callback {
    override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
        requireActivity().menuInflater.inflate(R.menu.contextual_action_bar, menu)
        return true
    }

    override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {return false}

    override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {return false}

    override fun onDestroyActionMode(mode: ActionMode?) {}
}

val actionMode = requireActivity().startActionMode(callback)
actionMode?.title = "1 selected"

With this code, I get following result:

action mode

How can we change this code so that it also changes the status bar background and icon colors? Following is the example of how many apps including Google Files app does this:

google-files

Notice how it correctly changes status bar background color as well as icon colors accordingly.

I have already tried this answer but it does not result in a smooth transition as many users have commented there. Is there any standard way to achieve the desired behaviour?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.