Joshua W
Joshua W

Reputation: 4983

How to change Toolbar home icon color

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>

Upvotes: 120

Views: 171567

Answers (15)

android developer
android developer

Reputation: 116372

For me, as I wanted the toolbar title&up-button to be white color, this is what worked for me:

    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
        <item name="android:statusBarColor">?android:colorBackground</item>
        <item name="android:navigationBarColor">@android:color/transparent</item>
        <item name="toolbarStyle">@style/Widget.App.Toolbar</item>
    </style>


    <style name="Widget.App.Toolbar" parent="Widget.MaterialComponents.Toolbar">
        <item name="materialThemeOverlay">@style/ThemeOverlay.App.Toolbar</item>
        <item name="titleTextColor">@android:color/white</item>
        <item name="navigationIconTint">@android:color/white</item>
    </style>

    <style name="ThemeOverlay.App.Toolbar" parent="">
        <item name="colorControlNormal">@android:color/white</item>
    </style>

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Light" />

Works with layout as such:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout" android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar" android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:id="@+id/scrollView" android:layout_width="match_parent"
        android:layout_height="match_parent" android:fadeScrollbars="false"
        android:overScrollMode="ifContentScrolls" android:scrollbars="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
...

Upvotes: 0

Jeiner Mar&#237;n
Jeiner Mar&#237;n

Reputation: 1

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

Upvotes: 0

A1m
A1m

Reputation: 3107

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

Upvotes: 6

Moreno
Moreno

Reputation: 180

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

Upvotes: 3

android developer
android developer

Reputation: 116372

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)

Upvotes: 14

Narender Gusain
Narender Gusain

Reputation: 2440

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;
    

    }

Upvotes: 0

Hermandroid
Hermandroid

Reputation: 2200

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.

Upvotes: 5

Shiva Kanumala
Shiva Kanumala

Reputation: 284

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

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.arrowleft);

Upvotes: 9

Kammaar
Kammaar

Reputation: 1741

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

Upvotes: 8

Mayank Jain
Mayank Jain

Reputation: 271

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

Upvotes: 13

osrl
osrl

Reputation: 7838

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>

Upvotes: 72

sreekumar
sreekumar

Reputation: 2479

   <!-- 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

Upvotes: 9

PayToPwn
PayToPwn

Reputation: 1257

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.

Upvotes: 37

Tuss
Tuss

Reputation: 945

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" />

Upvotes: 14

Joshua W
Joshua W

Reputation: 4983

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"/>

Upvotes: 272

Related Questions