Skip to content Skip to sidebar Skip to footer

Set Toolbar Icon Colour Programmatically

How can I set the colour of icons (home and overflow menu icon) in a Toolbar/AppBarLayout programmatically? I want to change the toolbar's colour scheme for a single fragment in an

Solution 1:

Change overflow icon is easy with support 23. Here is a method from Lorne Laliberte answer

publicstaticvoidsetOverflowButtonColor(final Toolbar toolbar, finalint color) {
    Drawabledrawable= toolbar.getOverflowIcon();
    if(drawable != null) {
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTint(drawable.mutate(), color);
        toolbar.setOverflowIcon(drawable);
    }
}

You can change your home as up passing your custom drawable..

getSupportActionBar().setHomeAsUpIndicator(R.drawable.your_drawable)

or changing its color

finalDrawableupArrow= getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(Color.parseColor("#FFFFFF"), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);

EDIT: If you want to change more elements here is good post to change all toolbar icons colors.

Hope this helps!!

Solution 2:

I've accepted the most helpful answer (and commented on it) explaining that I used a combination of its linked code snippets to form a single AppBarLayout/Toolbar colouring method. It covers background, title, subtitle, back/drawer icon and overflow icon colours, as well as any custom ImageButtons added. Here's my result (forgive the English 'colour' spelling(!)..):

publicstaticvoidcolouriseToolbar(AppBarLayout appBarLayout, @ColorIntint background, @ColorIntint foreground) {
    if (appBarLayout == null) return;

    appBarLayout.setBackgroundColor(background);

    finalToolbartoolbar= (Toolbar)appBarLayout.getChildAt(0);
    if (toolbar == null) return;

    toolbar.setTitleTextColor(foreground);
    toolbar.setSubtitleTextColor(foreground);

    finalPorterDuffColorFiltercolorFilter=newPorterDuffColorFilter(foreground, PorterDuff.Mode.MULTIPLY);

    for (inti=0; i < toolbar.getChildCount(); i++) {
        finalViewview= toolbar.getChildAt(i);

        //todo: cal icon?
        Log.d(Globals.TAG, "view: "+i+" "+view.getClass().toString());

        //Back button or drawer open buttonif (view instanceof ImageButton) {
            ((ImageButton)view).getDrawable().setColorFilter(colorFilter);
        }

        if (view instanceof ActionMenuView) {
            for (intj=0; j < ((ActionMenuView) view).getChildCount(); j++) {

                finalViewinnerView= ((ActionMenuView)view).getChildAt(j);

                //Any ActionMenuViews - icons that are not back button, text or overflow menuif (innerView instanceof ActionMenuItemView) {
                    Log.d(Globals.TAG, "view (actionmenuitemviwe): "+i);

                    final Drawable[] drawables = ((ActionMenuItemView)innerView).getCompoundDrawables();
                    for (intk=0; k < drawables.length; k++) {

                        finalDrawabledrawable= drawables[k];
                        if (drawable != null) {
                            finalintdrawableIndex= k;
                            //Set the color filter in separate thread//by adding it to the message queue - won't work otherwise
                            innerView.post(newRunnable() {
                                @Overridepublicvoidrun() {
                                    ((ActionMenuItemView) innerView).getCompoundDrawables()[drawableIndex].setColorFilter(colorFilter);
                                }
                            });
                        }
                    }
                }
            }
        }
    }

    //Overflow iconDrawableoverflowIcon= toolbar.getOverflowIcon();
    if (overflowIcon != null) {
        overflowIcon.setColorFilter(colorFilter);
        toolbar.setOverflowIcon(overflowIcon);
    }
}

Solution 3:

You can change home as up icon by below code:

DrawableupArrow= ContextCompat.getDrawable(this, R.drawable.back);
getSupportActionBar().setHomeAsUpIndicator(upArrow);

Post a Comment for "Set Toolbar Icon Colour Programmatically"