How to change color of the back arrow in the new material theme?

181,396

Solution 1

You can achieve it through code. Obtain the back arrow drawable, modify its color with a filter, and set it as back button.

final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(getResources().getColor(R.color.grey), 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.

EDIT:

You can use this code to achieve the results you want:

toolbar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.blue_gray_15), PorterDuff.Mode.SRC_ATOP);

Solution 2

Looking at the Toolbar and TintManager source, drawable/abc_ic_ab_back_mtrl_am_alpha is tinted with the value of the style attribute colorControlNormal.

I did try setting this in my project (with <item name="colorControlNormal">@color/my_awesome_color</item> in my theme), but it's still black for me.

Update:

Found it. You need to set the actionBarTheme attribute (not actionBarStyle) with colorControlNormal.

Eg:

<style name="MyTheme" parent="Theme.AppCompat.Light">        
    <item name="actionBarTheme">@style/MyApp.ActionBarTheme</item>
    <item name="actionBarStyle">@style/MyApp.ActionBar</item>
    <!-- color for widget theming, eg EditText. Doesn't effect ActionBar. -->
    <item name="colorControlNormal">@color/my_awesome_color</item>
    <!-- The animated arrow style -->
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

<style name="MyApp.ActionBarTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar">       
    <!-- THIS is where you can color the arrow! -->
    <item name="colorControlNormal">@color/my_awesome_color</item>
</style>

<style name="MyApp.ActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="elevation">0dp</item>      
    <!-- style for actionBar title -->  
    <item name="titleTextStyle">@style/ActionBarTitleText</item>
    <!-- style for actionBar subtitle -->  
    <item name="subtitleTextStyle">@style/ActionBarSubtitleText</item>

    <!-- 
    the actionBarTheme doesn't use the colorControlNormal attribute
    <item name="colorControlNormal">@color/my_awesome_color</item>
     -->
</style>

Solution 3

Tried all suggestions above. The only way that I managed to change the color of navigation icon default Back button arrow in my toolbar is to set colorControlNormal in base theme like this. Probably due to the fact that the parent is using Theme.AppCompat.Light.NoActionBar

<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorControlNormal">@color/white</item> 
  //the rest of your codes...
</style>

Solution 4

Need to add a single attribute to your toolbar theme -

<style name="toolbar_theme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="colorControlNormal">@color/arrow_color</item>
</style>

Apply this toolbar_theme to your toolbar.

OR

you can directly apply to your theme -

<style name="CustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorControlNormal">@color/arrow_color</item> 
  //your code ....
</style>

Solution 5

Just add

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

to your current app theme.

Share:
181,396

Related videos on Youtube

Ferran Maylinch
Author by

Ferran Maylinch

Developer at Yandex, and sometimes a teacher.

Updated on September 28, 2021

Comments

  • Ferran Maylinch
    Ferran Maylinch over 2 years

    I've updated my SDK to API 21 and now the back/up icon is a black arrow pointing to the left.

    Black back arrow

    I would like it to be grey. How can I do that?

    In the Play Store, for example, the arrow is white.

    I've done this to set some styles. I have used @drawable/abc_ic_ab_back_mtrl_am_alpha for homeAsUpIndicator. That drawable is transparent (only alpha) but the arrow is displayed in black. I wonder if I can set the color like I do in the DrawerArrowStyle. Or if the only solution is to create my @drawable/grey_arrow and use it for homeAsUpIndicator.

    <!-- Base application theme -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
    
        <item name="android:actionBarStyle" tools:ignore="NewApi">@style/MyActionBar</item>
        <item name="actionBarStyle">@style/MyActionBar</item>
    
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    
        <item name="homeAsUpIndicator">@drawable/abc_ic_ab_back_mtrl_am_alpha</item>
        <item name="android:homeAsUpIndicator" tools:ignore="NewApi">@drawable/abc_ic_ab_back_mtrl_am_alpha</item>
    </style>
    
    <!-- ActionBar style -->
    <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
    
        <item name="android:background">@color/actionbar_background</item>
        <!-- Support library compatibility -->
        <item name="background">@color/actionbar_background</item>
    </style>
    
    <!-- Style for the navigation drawer icon -->
    <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@color/actionbar_text</item>
    </style>
    

    My solution so far has been to take the @drawable/abc_ic_ab_back_mtrl_am_alpha, which seems to be white, and paint it in the color I desire using a photo editor. It works, although I would prefer to use @color/actionbar_text like in DrawerArrowStyle.

    • ban-geoengineering
      ban-geoengineering over 8 years
      None of the XML-based answers so far have solved this issue for me, but I have successfully used your homeAsUpIndicator / @drawable/abc_ic_ab_back_mtrl_am_alpha code to make the back arrow white. I prefer this than hacking into the Java.
    • f470071
      f470071 about 7 years
      Old but still. Are (were) you using ActionBar or the new Toolbar?
    • android developer
      android developer over 6 years
      The default one is supposed to be a bit gray, by using android:theme="@style/Widget.AppCompat.Light.ActionBar" inside the Toolbar XML tag. Its color is #737373
    • Sakiboy
      Sakiboy over 3 years
      Check my answer - it is super simple and has no side-effects.
  • Ferran Maylinch
    Ferran Maylinch over 9 years
    I already tried that and the arrow is black. Note this arrow is not the same arrow you see when you open the drawer (that arrow IS affected by the color attribute). The drawer arrow is a bit bigger.
  • Flávio Faria
    Flávio Faria over 9 years
    So, it seems you'll have to provide your own drawable resource. You can generate it using this tool: shreyasachar.github.io/AndroidAssetStudio
  • Daniel Ochoa
    Daniel Ochoa over 9 years
    This should be the accepted answer. Thank you for this. Question, I'm unable to find the style for coloring the arrow shown when SearchView is expanded. Any clue? It apparently isn't affected by this answer.
  • Seraphim's
    Seraphim's over 9 years
    I need to add also <item name="android:colorControlNormal">...</item> with the android: prefix
  • Joris
    Joris over 9 years
    This is the only thing that worked for me without changing the tint off all other widgets with colorControlNormal
  • luthier
    luthier over 9 years
    Works like a charm! :)
  • gphilip
    gphilip over 9 years
    Unfortunately this is a level 21 API and if you want to support anything below it won't work.
  • dabluck
    dabluck over 9 years
    problem with this solution is it paints all your other back arrows, if you only want to change one and leave the rest white
  • wbk727
    wbk727 about 9 years
    What about the colour of the overflow dots?
  • Sergey Shustikov
    Sergey Shustikov about 9 years
    cant find R.color.grey what is the color?
  • pdegand59
    pdegand59 about 9 years
    SRC_ATOP helped me there ! Thanks
  • sk1pro99
    sk1pro99 almost 9 years
    @ssh you have to define the a string attribute in your colors.xml file(in res/values) with the name as grey as follows <color name="grey">#808080</color>
  • Sid
    Sid almost 9 years
    @Carles, it coloured the back arrow white alright. But now it doesn't function properly! It doesn't go back to the previous activity!
  • mrroboaat
    mrroboaat almost 9 years
    It works. Please note you should use ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_mtrl_am_alpha) because getResources.getDrawable(int) is deprecated
  • Neo
    Neo over 8 years
    It's better to change the color of arrow rather than using images. The attribute colorControlNormal decides the color of arrow. So change this attribute in your toolbar custom theme and enjoy :)
  • Rohan Kandwal
    Rohan Kandwal over 8 years
    Simplest answer of all, instead of fiddling around with other code, just add this line in your style.xml
  • Pavan
    Pavan over 8 years
    for problem where it changes all back arrow color in application you can use mutate().. upArrow.mutate().setColorFilter(getResources().getColor(R.co‌​lor.white), PorterDuff.Mode.SRC_ATOP);
  • Mauker
    Mauker over 8 years
    Another possible solution would be for you to get that resource yourself and put it on your drawable folders :)
  • android developer
    android developer over 8 years
    how do i also customize the selector of the background of the up-button ?
  • kellogs
    kellogs over 8 years
    Worked, but with a tweak. Instead of using colorControlNormal as rockgecko suggested I threw it inside <style name="AppTheme.NoActionBar"> which is then applied per activity.
  • Jason Robinson
    Jason Robinson over 8 years
    If your parent theme derives from one of the "NoActionBar" themes, then setting colorControlNormal on the root theme rather than in the actionBarTheme is the route to go.
  • jdonmoyer
    jdonmoyer over 8 years
    ContextCompat.getColor() should also be used instead of getResources().getColor()
  • Jon
    Jon over 8 years
    Note that abc_ic_ab_back_mtrl_am_alpha changed to abc_ic_ab_back_material in the 23.2.0 support library
  • Adnan Ali
    Adnan Ali about 8 years
    remember that colorControlNormal requires API 21
  • YawaraNes
    YawaraNes about 8 years
    In my case, the drawable resource is private and I can't use it, so I solved it using this another solution that worked for me (using styles only): stackoverflow.com/a/28631979/2969811
  • Vicky Chijwani
    Vicky Chijwani about 8 years
    @AdnanAli if you're using AppCompat it'll work on < API 21 as well. Clearly, AppCompat is being used here because colorControlNormal is not prefixed with android:.
  • Bron Davies
    Bron Davies almost 8 years
    It only took me 2 hours of searching and failing and searching again to finally get to this. Sometimes the style 'system' in android drives me nuts! This method is especially important to use when you have one activity that doesn't use the same styling as the others.
  • Hiren
    Hiren almost 8 years
    Expected answer Thank you
  • Khaled Saifullah
    Khaled Saifullah almost 8 years
    Note that, if you are using toolbar with theme ".NoActionBar", then getSupportActionBar().setHomeAsUpIndicator(upArrow); won't work. Use toolbar.setNavigationIcon(upArrow); then
  • Bruce
    Bruce almost 8 years
    But this changes the tint off all other widgets as well.. any way not to affect other widgets such as checkbox and radio buttons?
  • King King
    King King almost 8 years
    Trying the accepted answer (maybe the most popular solution found on the net) does not work for me but this one works :) so crazy when programming with Android.
  • Rahul Hawge
    Rahul Hawge almost 8 years
    Brilliant... I tried all the about answers to add it in the theme but it did not work... As you mentioned, I had android:theme="@style/ThemeOverlay.AppCompat.ActionBar" property in my tool bar. Once I removed it, it started working.
  • SimplyProgrammer
    SimplyProgrammer over 7 years
    Simplest answer. This works without changing other control themes. All you need is to set the custom toolbar theme to your toolbar. :)
  • f470071
    f470071 about 7 years
    Is this relevant to old ActionBar or new Toolbar?
  • Narendra Singh
    Narendra Singh about 7 years
    It should be abc_ic_ab_back_material, instead of abc_ic_ab_back_mtrl_am_alpha.
  • Antek
    Antek about 7 years
    This should be the accepted answer, imho - it targets just the arrow, gets the arrow to set using simplest means, and is compatible also below API 21
  • Pavan
    Pavan almost 7 years
    this will not work when you upgrade support design version
  • P. Savrov
    P. Savrov almost 7 years
    Setting the {@link #DISPLAY_HOME_AS_UP} display option will automatically enable the home button.
  • TheJudge
    TheJudge almost 7 years
    If you seek more custom solution (different colors for titles and menu icons in my case) then this is the solution you want
  • Udi Oshi
    Udi Oshi almost 7 years
    This is changing the android.support.v7.widget.AppCompatCheckBox color also
  • rrbrambley
    rrbrambley over 6 years
    Note that you might need to call this before attempting to call getNavigationIcon(): getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  • CoolMind
    CoolMind over 6 years
    Though this solution works, Lint will show a warning, so I applied a solution with getNavigationIcon() here.
  • Aba
    Aba about 6 years
    Oh the pains you have to go through to change the color of just 1 icon.. The first one works for me. The 2nd one works for custom Toolbars (and other things too apparently). 3 years later, many thanks Neo.
  • daka
    daka over 5 years
    @JasonRobinson Your comment worked for me, thanks +1. This answer however, did not, not sure why.
  • daka
    daka over 5 years
    But this also changes the colour of other controls, like radio buttons and the line under text edit.
  • daka
    daka over 5 years
    Future readers, make sure you do supportActionBar?.setDisplayHomeAsUpEnabled(true) before doing toolbar?.navigationIcon?.setColorFilter(ContextCompat.getCol‌​or(this,android.R.co‌​lor.white), PorterDuff.Mode.SRC_ATOP)
  • Henry
    Henry over 5 years
    Changing theme to Xxx.NoActionBar causes NPE on "getSupportActionBar".
  • Harry Aung
    Harry Aung over 5 years
    @Henry NoActionBar theme won't provide default action bar. So you need to setup your own toolbar by like so setSupportActionBar(toolbar) where toolbar is the one you setup in xml.
  • shariful islam
    shariful islam over 5 years
    The best answer! Thank you.
  • John T
    John T over 5 years
    Excellent! Best solution.
  • Johnny
    Johnny almost 5 years
    Great answer! I saved me the whole life.
  • Dhaval Patel
    Dhaval Patel about 4 years
    Buddy, You saved my day. I spent the whole day on this one only. I wish I can give you more votes.
  • Pushpendra
    Pushpendra almost 3 years
    How to change colour of @drawable/your_drawable_icon as per our theme colour?
  • famfamfam
    famfamfam over 2 years
    not working to me
  • famfamfam
    famfamfam over 2 years
    not working to me, i dont know why?