How to change Toolbar home icon color

154,683

Solution 1

I solved it by editing styles.xml:

<style name="ToolbarColoredBackArrow" parent="AppTheme">
    <item name="android:textColorSecondary">INSERT_COLOR_HERE</item>
</style>

...then referencing the style in the Toolbar definition in the activity:

<LinearLayout
    android:id="@+id/main_parent_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        app:theme="@style/ToolbarColoredBackArrow"
        app:popupTheme="@style/AppTheme"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"/>

Solution 2

Here is what you are looking for. But this also changes the color of radioButton etc. So you might want to use a theme for it.

<item name="colorControlNormal">@color/colorControlNormal</item>

Solution 3

I solved it programmatically using this code:

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);

Revision 1:

Starting from API 23 (Marshmallow) the drawable resource abc_ic_ab_back_mtrl_am_alpha is changed to abc_ic_ab_back_material.

Solution 4

This answer maybe too late, but here is how I do it. Styling the toolbar will do the trick. Create toolbar.xml with following code.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_alignParentTop="true"
android:layout_gravity="bottom"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

and in the styles.xml

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!--
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>

Finally, include the toolbar inside layout

<include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

Solution 5

Change your Toolbar Theme to ThemeOverlay.AppCompat.Dark

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:orientation="vertical"
    app:theme="@style/ThemeOverlay.AppCompat.Dark">

</android.support.v7.widget.Toolbar>

and set it in activty

mToolbar = (Toolbar) findViewById(R.id.navigation);
setSupportActionBar(mToolbar);
Share:
154,683

Related videos on Youtube

Joshua W
Author by

Joshua W

Updated on October 09, 2021

Comments

  • Joshua W
    Joshua W over 2 years

    I am using a android.support.v7.widget.Toolbar and learned from this post how to change the color of the hamburger icon to white, but the up/back arrow remains a dark color when I call

    setDisplayHomeAsUpEnabled(true);
    

    How can I make the arrow white as well?

    Here is what my toolbar looks like when I call setDisplayHomeAsUpEnabled():

    enter image description here

    ...and here is the relevant portion of my styles.xml file:

    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">#194C5F</item>
        <item name="colorAccent">@color/accent</item>
        <item name="drawerArrowStyle">@style/WhiteDrawerIconStyle</item>
    </style>
    
        <style name="WhiteDrawerIconStyle" parent="Widget.AppCompat.DrawerArrowToggle">
            <item name="spinBars">true</item>
            <item name="color">@android:color/white</item>
        </style>
    
    • Surender Kumar
      Surender Kumar about 9 years
      You can use white arrow png icon and replace with black one.
    • Joshua W
      Joshua W about 9 years
      I'm trying to avoid that and just indicate a color as a style... if that's the only way I know I can do it via actionBarDrawerToggle.setHomeAsUpIndicator(R.drawable.arrow)‌​;
    • Joshua W
      Joshua W about 9 years
      ...also the png /com.android.support/appcompat-v7/21.0.3/res/drawable-xxxhdp‌​i/abc_ic_ab_back_mtr‌​l_am_alpha.png appears white
    • user2056563
      user2056563 about 9 years
      @JoshuaWitter Thanks it solved my issue, can you also please help me to detect the click on back button ? here is my question stackoverflow.com/questions/29138629/…
    • osrl
      osrl about 9 years
      @JoshuaWitter even if it is white, it gets tinted. Change the colorControlNormal value
  • mbelsky
    mbelsky almost 9 years
    Also you can set colorControlNormal instead android:textColorSecondary
  • Ishan Fernando
    Ishan Fernando over 7 years
    set this one also. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  • Dr. aNdRO
    Dr. aNdRO over 7 years
    but that will change the entire widget's color right? @mbelsky
  • Francisco Romero
    Francisco Romero over 7 years
    @mbelsky Note that it requires API level 21.
  • Mavamaarten
    Mavamaarten over 7 years
    This should be the accepted answer. This achieves what I was looking for: <style name="ActionBarTheme" parent="ThemeOverlay.AppCompat.Dark.ActionBar"> <item name="colorControlNormal">###COLOR###</item> </style> apply the theme directly to the Toolbar.
  • Mohamed Hatem Abdu
    Mohamed Hatem Abdu about 7 years
    @Error404 : it's added in API level 1 as u see here
  • Deepak
    Deepak over 6 years
    Hi All, I have tried your code but I am not getting required result. In my toolbar.axml file I am setup theme is android:theme=@style/ThemeOverlay.AppCompat.Dark.ActionBar and in my styles.axml file I have write your given code but navigation bar back button color in no change.
  • AndroidDevBro
    AndroidDevBro about 6 years
    Does anyone know how to change the colour of the icon on a single fragment instead of doing so globally?
  • AndroidDevBro
    AndroidDevBro about 6 years
    Would this work in a Fragment? If so, where would I put this code into? The OnCreateView method, the root of the class, or...?
  • PayToPwn
    PayToPwn about 6 years
    @AndroidDevBro Yes, just put it in the OnCreate function and remember to update the resource as Nouman Tahir wrote in the revision (using R.drawable.abc_ic_ab_back_material instead of R.drawable.abc_ic_ab_back_mtrl_am_alpha) and getting the SupportActionBar like this: ((AppCompatActivity)getActivity()).getSupportActionBar().set‌​HomeAsUpIndicator(up‌​Arrow);
  • user25
    user25 about 6 years
    @FranciscoRomero colorControlNormal doesn't require API level 21
  • Henry
    Henry over 5 years
    but AppTheme isn't referenced anywhere. is this a typo?
  • C. Skjerdal
    C. Skjerdal about 5 years
    I was surprised of the other answers, I always use this method
  • ataravati
    ataravati almost 5 years
    The parent attribute is very important.
  • Zaraki596
    Zaraki596 about 4 years
    This is by far best answer and it solves my problem
  • Yeung
    Yeung about 3 years
    No..it would affect the normal text too but not the arrow