Search code examples
androidandroid-actionbarandroid-toolbar

Changing all toolbar icon colors dynamically


I implemented following function to change the color of all items in actionbar. and i use it to change the color of elements smoothly, when expanding or collapsing CollapsingToolbarLayout.

private void setToolbarElementsColor(int color) {
    PorterDuffColorFilter colorFilter
            = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    toolbar.getNavigationIcon().setColorFilter(colorFilter);
    //overflowDrawable is IconicDrawable from com.mikepenz.iconics
    if (toolbar.getOverflowIcon() != overflowDrawable){
        toolbar.setOverflowIcon(overflowDrawable);
    }
    overflowDrawable.color(color);
    toolbar.setTitleTextColor(color);
    for (int i = 0; i< toolbar.getMenu().size(); i++){
        Drawable icon = toolbar.getMenu().getItem(i).getIcon();
        if (icon !=null) {
            // HERE IS THE CODE WITH EXPLAINED PROBLEM
            icon.setColorFilter(colorFilter);
        }
    }
}

And here is code for menu items:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_back"
    android:title="@string/add_remove_filter"
    app:showAsAction="always" />
<item
    android:id="@+id/action_filter"
    android:title="@string/add_filter"
    app:showAsAction="always" />

<item
    android:id="@+id/action_save_report"
    app:showAsAction="never"
    android:title="@string/action_save_report">
</item>
</menu>

The code works fine. but one strange problem: when i open overflow menu once and close it, the menu icons retain the last color they have before opening overflow menu, and do not change in accordance to other toolbar elements (the new colors i set for all elements).

Example: Elements are black for expanded state with light toolbar, and white for collapsed state with dark toolbar. now if i collapse the toolbar and open the overflow menu and close it, after that the icon colors remain white, regardless of toolbar expanded or collapsed state, even thought the piece of [icon.setColorFilter(colorFilter);] is run correctly.

The problem solves when the activity goes to background and resumes again. e.g. pressing home button and returning to the activity using recent app list or opening new activity from that activity and returning.

I changed the code inside for loop to the code below but no luck (I am using IconicDrawables for menu icons)

IconicsDrawable icon = (IconicsDrawable) toolbar.getMenu().getItem(i).getIcon();
if (icon !=null) {
    icon.color(color);
}

Replacing the icon drawable from IconicDrawable to PNG drawable inside drawables folder also didn't solve the problem.

Illustration of the problem


Solution

  • After some search, I solved the problem by adding the code below:

    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        invalidateOptionsMenu();
        return super.onMenuOpened(featureId, menu);
    }
    

    It just redraws icons just after opening the overflow menu. but the reason of above mentioned problem is yet unknown to me.