120

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>
Joshua W
  • 4,973
  • 5
  • 24
  • 30
  • 2
    You can use white arrow png icon and replace with black one. – Surender Kumar Feb 20 '15 at 02:54
  • 1
    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 Feb 20 '15 at 03:00
  • ...also the png /com.android.support/appcompat-v7/21.0.3/res/drawable-xxxhdpi/abc_ic_ab_back_mtrl_am_alpha.png appears white – Joshua W Feb 20 '15 at 12:45
  • @JoshuaWitter Thanks it solved my issue, can you also please help me to detect the click on back button ? here is my question http://stackoverflow.com/questions/29138629/action-bar-detect-back-button-click-in-fragment#29138683 – user2056563 Mar 19 '15 at 07:13
  • 1
    @JoshuaWitter even if it is white, it gets tinted. Change the `colorControlNormal` value – osrl Mar 28 '15 at 16:19

14 Answers14

271

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"/>
Joshua W
  • 4,973
  • 5
  • 24
  • 30
71

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>
osrl
  • 8,168
  • 8
  • 36
  • 57
37

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.

Nouman Tahir
  • 819
  • 1
  • 9
  • 26
PayToPwn
  • 1,238
  • 1
  • 16
  • 29
  • 5
    set this one also. `getSupportActionBar().setDisplayHomeAsUpEnabled(true);` – Ishan Fernando Aug 08 '16 at 11:40
  • Would this work in a `Fragment`? If so, where would I put this code into? The `OnCreateView` method, the root of the class, or...? – AndroidDevBro Mar 22 '18 at 11:33
  • 1
    @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().setHomeAsUpIndicator(upArrow); – PayToPwn Mar 22 '18 at 14:30
14

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" />
Tuss
  • 935
  • 1
  • 8
  • 14
14

Here's my solution:

toolbar.navigationIcon?.mutate()?.let {
  it.setTint(theColor)
  toolbar.navigationIcon = it
}

Or, if you want to use a nice function for it:

fun Toolbar.setNavigationIconColor(@ColorInt color: Int) = navigationIcon?.mutate()?.let {
    it.setTint(color)
    this.navigationIcon = it
}

Usage:

toolbar.setNavitationIconColor(someColor)
Jéwôm'
  • 3,753
  • 5
  • 40
  • 73
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • 1
    This is by far best answer and it solves my problem – Zaraki596 May 05 '20 at 18:36
  • NavigationComponent will override it anyway (when it automatically sets menu or back icon for toolbar) https://stackoverflow.com/questions/73362085/changing-color-of-back-arrow-icon-of-toolbar-dynamically-when-using-navigation-c – user924 Aug 15 '22 at 14:21
  • @user924 Not relevant. This is about whether you use it or not. Some projects don't use NavigationComponent, or they have places that don't have it as it's too late/complex to add it. – android developer Aug 15 '22 at 23:00
13

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);
Mayank Jain
  • 271
  • 3
  • 7
9
   <!-- ToolBar -->
    <style name="ToolBarTheme.ToolBarStyle"    parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:textColorPrimary">@android:color/white</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:textColorPrimaryInverse">@color/white</item>
    </style>

Too late to post, this worked for me to change the color of the back button

sreekumar
  • 2,439
  • 1
  • 21
  • 27
9

Instead of style changes, just put these two lines of code to your activity.

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.arrowleft);
Pang
  • 9,564
  • 146
  • 81
  • 122
Shiva Kanumala
  • 284
  • 5
  • 5
8

Well there is a more easy way to do this

drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.open_drawer, R.string.close_drawer);
arrow = drawerToggle.getDrawerArrowDrawable();

And then

arrow.setColor(getResources().getColor(R.color.blue);
Kammaar
  • 1,611
  • 1
  • 14
  • 12
6

Wasn't able to color it with the solutions here for some reason. But using com.google.android.material.appbar.MaterialToolbar instead you can use

app:navigationIconTint="@color/black"

See: https://material.io/components/app-bars-top/android#regular-top-app-bar

A1m
  • 2,897
  • 2
  • 24
  • 39
  • NavigationComponent will override it anyway (when it automatically sets menu or back icon for toolbar) https://stackoverflow.com/questions/73362085/changing-color-of-back-arrow-icon-of-toolbar-dynamically-when-using-navigation-c – user924 Aug 15 '22 at 14:21
5

This code works for me:

public static Drawable changeBackArrowColor(Context context, int color) {
    String resName;
    int res;

    resName = Build.VERSION.SDK_INT >= 23 ? "abc_ic_ab_back_material" : "abc_ic_ab_back_mtrl_am_alpha";
    res = context.getResources().getIdentifier(resName, "drawable", context.getPackageName());

    final Drawable upArrow = context.getResources().getDrawable(res);
    upArrow.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);

    return upArrow;
}

...

getSupportActionBar().setHomeAsUpIndicator(changeBackArrowColor(this, Color.rgb(50, 50, 50)));               
supportInvalidateOptionsMenu();

Also, if you want to change the toolbar text color:

Spannable spannableString = new SpannableString(t);
spannableString.setSpan(new ForegroundColorSpan(Color.rgb(50, 50, 50)), 0, t.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
toolbar.setText(spannableString);

Working from API 19 through 25.

Paul Chu
  • 1,249
  • 3
  • 19
  • 27
Hermandroid
  • 2,120
  • 4
  • 29
  • 35
3

Try this: Set the toolbar's theme in your layout as follows

android:theme = "@android:style/ThemeOverlay.Material.Dark.ActionBar"

If you want further information

The curious case of the Overflow Icon Color by Martin Bonnin

Moreno
  • 171
  • 1
  • 9
0

Instead of using older drawable id "abc_ic_ab_back_material", use the new one abc_ic_ab_back_material in every api version. I have tested it in 19, 21, 27 and working fine with below code and configuration.

  • minSdkVersion = 17
  • targetSdkVersion = 26
  • compileSdkVersion = 27

    public static Drawable changeBackArrowColor(Context context, int color) {
    int res;
    res = context.getResources().getIdentifier("abc_ic_ab_back_material", "drawable", context.getPackageName());
    if (res == 0)
        return null;
    final Drawable upArrow = ContextCompat.getDrawable(context, res);
    upArrow.setColorFilter(ContextCompat.getColor(context, color), PorterDuff.Mode.SRC_ATOP);
    
    return upArrow;
    

    }

Narender Gusain
  • 2,220
  • 17
  • 13
0

This is best and easier option. getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow_back_24);

  • Already answered: https://stackoverflow.com/a/48019371/14291243 – Abhishek Dutt Jun 26 '22 at 05:42
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32099644) – CbL Jun 29 '22 at 09:50