28

I wanna change default ActionBar homeAsUp indicator (drawable) in my AppCompat Toolbar. How to achieve that? Only default arrow shows up.

styles (same for other API's level):

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

        <!-- API 14 theme customizations can go here. -->
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:homeAsUpIndicator">@drawable/home</item>

        <!-- Support library compatibility -->
        <item name="windowActionBarOverlay">true</item>
        <item name="homeAsUpIndicator">@drawable/home</item>
</style>

Toolbar in my fragment layout:

<android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/my_awesome_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:homeAsUpIndicator="@drawable/home"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/AppTheme" />

In Fragment:

toolbar = (Toolbar) rootView.findViewById(R.id.my_awesome_toolbar);

        activity.setSupportActionBar(toolbar);

        ActionBar actionBar = activity.getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
dasar
  • 5,321
  • 4
  • 24
  • 36

6 Answers6

33

To change icon just call at runtime:

toolbar.setNavigationIcon(R.drawable.home);

Trick with styles/themes not working.

How to enable homeAsUp or call setDisplayHomeAsUpEnabled() on standalone toolbar with appcompat v21

Community
  • 1
  • 1
dasar
  • 5,321
  • 4
  • 24
  • 36
  • Sigh. I had to attempt 10 other things before I found this... so simple. Thanks @daser – PGMacDesign Sep 25 '15 at 16:28
  • 7
    Note toolbar.setNavigationIcon doesn't work with setSupportActionBar(toolbar) – Peter Zhao Jan 04 '16 at 09:09
  • 4
    Use below code for appcompat v21 --> ActionBar actionbar = getSupportActionBar(); actionbar.setDisplayHomeAsUpEnabled(true); // Enable actionbar.setHomeAsUpIndicator(R.drawable.ic_menu_white); //Change Icon – Jaldeep Asodariya Sep 29 '16 at 05:41
19

I tried setHomeAsUpIndicator(int resId) ,it works.

private void initToolbar ( Toolbar mToolbar ) {

    mToolbar.setTitleTextColor ( getResources ().getColor ( R.color.main_title ) );
    setSupportActionBar ( mToolbar );

    ActionBar actionbar = getSupportActionBar ();
    actionbar.setDisplayHomeAsUpEnabled ( true );
    actionbar.setHomeAsUpIndicator ( R.drawable.ic_action_back );
}

But mToolbar.setNavigationIcon(R.drawable.ic_action_back) doesn't work :(

RockerFlower
  • 727
  • 2
  • 10
  • 28
19

Just add this line to your styles.xml

        <item name="navigationIcon">@drawable/ic_back_arrow</item>
Ioane Sharvadze
  • 2,118
  • 21
  • 35
  • 2
    As per August 2016, this is the most correct answer. This way, you have a single place in which to change the icon, plus with the added bonus of not having to call the dreaded `getSupportActionBar().setDisplayHomeAsUpEnabled(true)` – Henrique de Sousa Aug 03 '16 at 11:31
  • It worked for me but I think this will work only if you are using `Toolbar`. I set this in my theme.xml and it took effect everywhere in the app. – JaydeepW Sep 04 '17 at 08:05
9

If android.support.v7.app.ActionBarDrawerToggle used together with DrawerLayout and Toolbar you can change homeAsUp icon with the following code:

//set home as up indicator
mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_up_indicator);

//remove home as up indicator
mDrawerToggle.setHomeAsUpIndicator(null);

to show homeAsUpIndicator indicator instead of home indicator do following:

mDrawerToggle.setDrawerIndicatorEnabled(false);

Docs:

ActionBarDrawerToggle#setHomeAsUpIndicator ActionBarDrawerToggle#setDrawerIndicatorEnabled

Nikolay Nikiforchuk
  • 1,998
  • 24
  • 20
4

i just found out that it actually works toolbar.setNavigationIcon(R.drawable.ic_action_back) and here is the code to make it work.

  @Override
   protected void onCreate(Bundle savedInstanceState) {

   .....
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setNavigationIcon(R.drawable.ic_action_back);

Post the following code in onOptionsItemSelected(MenuItem item)

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   ......
    if (id == android.R.id.home) {
        startActivity(new Intent(this, MainActivity.class));
        return true;
    }

    return super.onOptionsItemSelected(item);
}

Note Mainactivity in this code is the current activity i want my back or home button to go to. I hope this helps

2

Unfortunately, if this style line is used in a commonly shared AppCompat ToolBar/App bar with other activities in the App, including the main activity, the "navigationIcon" specified will be automatically shown, unlike the standard default HomeAsUpIndicator, which is not shown unless explicitly enabled as desired in an Activity, typically as follows: (in the onCreate())

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

With the above style line, since it behaves in an opposite manner, in a similar fashion, the indicator needs to be explicitly disabled if it is not desired to be shown, as on the main activity, as follows: (in the onCreate())

getSupportActionBar().setDisplayHomeAsUpEnabled(false);

(as observed on Android 4.0.4, 4.3, and 4.4.2 phones)

Dan K
  • 46
  • 2