Search code examples
androidmaterial-designandroid-toolbarandroid-appbarlayout

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 activity. Setting the AppBarLayout's background to a light colour (e.g. light gray with appBarLayout.setBackgroundResource(..);) results in white icons and a white title which are barely visible.

Its layout is as follows:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/ToolbarStyle"
        app:layout_scrollFlags="scroll|enterAlways"/>

</android.support.design.widget.AppBarLayout>

Solution found


Solution

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

    public static void setOverflowButtonColor(final Toolbar toolbar, final int color) {
        Drawable drawable = 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

    final Drawable upArrow = 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!!