72

I'm trying the new Toolbar component and having some trouble with the navigation icon. I want to implement a custom icon for back navigation :

In my manifest i set a parent to my activity :

<activity android:name=".CardsActivity" android:parentActivityName=".MainActivity">
    <!-- Parent activity meta-data to support API level 7+ -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity" />
</activity>

I declare the toolbar like this :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.lollitest.MainActivity" >
    
    <android.support.v7.widget.Toolbar
        android:id="@+id/my_awesome_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:layout_marginBottom="10dp"
        android:background="?attr/colorPrimary" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/my_awesome_toolbar"
        android:text="@string/hello_world" />

</RelativeLayout>

Then in my activity i configure the Toolbar like this :

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
toolbar.setNavigationIcon(R.drawable.ic_good);
toolbar.setTitle("Title");
toolbar.setSubtitle("Sub");
toolbar.setLogo(R.drawable.ic_launcher);
setSupportActionBar(toolbar);

Which giving me : Toolbar with back button

The back icon is not the one i set with setNavigationIcon() ! Whatever drawable i give to the method the navigation icon is always the back arrow.

I have tried to remove the parent association in the manifest but the only effect is (obviously) to prevent the button to go back.

On contrary if i want the default back arrow icon and don't call setNavigationIcon() i don't have any icon at all.

What is the correct way to handle the navigation icon in toolbar (custom and default) ?

NOte : i'm running my test on Android 4.4

Community
  • 1
  • 1
grunk
  • 14,718
  • 15
  • 67
  • 108
  • ive done this a lot of times and somehow its not working for me now? – filthy_wizard Jun 15 '18 at 08:00
  • It's worth noting that adding app:navigationIcon is not recommended as "all Android devices provide a Back button for this type of navigation, so you should not add a Back button to your app’s UI" https://developer.android.com/guide/navigation/navigation-custom-back – apex39 Jan 27 '21 at 13:42

13 Answers13

118

Currently you can use it, changing the order: (it seems to be a bug)

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

toolbar.setNavigationIcon(R.drawable.ic_good);
toolbar.setTitle("Title");
toolbar.setSubtitle("Sub");
toolbar.setLogo(R.drawable.ic_launcher);
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 1
    Indeed that work for a custom icon ! And what if want the default icon , there is no equivalent to sethomebuttonenabled(true) , is it ? – grunk Oct 23 '14 at 11:18
  • 4
    getSupportedActionBar().setHomeButtonEnabled(true); – Gabriele Mariotti Oct 23 '14 at 11:28
  • 10
    @GabrieleMariotti just correcting! getSupportActionBar().setHomeButtonEnabled(true); – shaktiman_droid Oct 24 '14 at 02:12
  • @Jabbar_Jigariyo This doesn't appear to work for me. Is there any specific ordering bug for setting the home button as well? – cnfw Nov 17 '14 at 21:03
  • 13
    Never mind, I found a way around. You have to add `getSupportActionBar().setDisplayHomeAsUpEnabled(true);` AFTER you setSupportActionBar to your toolbar. Similar to setting the navigation icon. – cnfw Nov 17 '14 at 21:13
  • @cw1998 You got it! Yeah! – shaktiman_droid Nov 17 '14 at 22:32
  • In my case `getSupportActionBar().setDisplayHomeAsUpEnabled(true)` change the navigation icon to black arrow. Solved this problem by setting `toolbar.setNavigationOnClickListener` with `onBackPressed()`. – Alexey Dec 22 '14 at 05:14
  • @Pulimet could you share the code snippet where you fixed the back arrow icon? – user802421 Feb 09 '15 at 04:29
  • @user802421 answered you below. Good luck. – Alexey Feb 09 '15 at 06:00
  • If you are using ActionBarDrawerToggle, move the *toolbar.setNavigationIcon* below *toggle.syncState()* – Maragues May 19 '16 at 10:10
25

Specific to the navigation icon, this is the correct order

// get the actionbar as Toolbar and set it up
Toolbar toolbar = (Toolbar) findViewById(R.id.signIn_toolbar);
setSupportActionBar(toolbar);

Inform the Toolbar to provide back navigation. This will set the icon to the default material icon

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Later override the icon with the custom one, in my case the Holo back icon

toolbar.setNavigationIcon(R.drawable.ic_chevron_left_white_36dp);
Raffaeu
  • 6,694
  • 13
  • 68
  • 110
20

(The answer to user802421)

private void setToolbar() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_action_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }
}

toolbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/toolbar_height"
    android:background="?attr/colorPrimaryDark" />
Alexey
  • 4,384
  • 1
  • 26
  • 34
8

Use setNavigationIcon to change it. don't forget create ActionBarDrawerToggle first!

sample code work for me:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    drawer = (DrawerLayout)findViewById(R.id.drawer_layout);

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);

    toggle.syncState();

    toolbar.setNavigationIcon(R.drawable.ic_menu);
Nguyên Phạm
  • 441
  • 5
  • 2
  • This worked for me - finally! This job of changing the standard burgermenu icon was WAY to hard. I can't figure out why this worked after everything else was set and then assigning the icon - does anyone know? – ZooMagic Feb 13 '18 at 14:49
4

I had simillar problem. After a big headache I found, that my ActionBarDrawerToggle was modifying the icon, also when it should not modify the icon (because I didn't give reference to toolbar to the toggle component). So in my NavigationDrawerFragment class (that handles the opening and closing) in setUp(...) method I set
mDrawerToggle.setHomeAsUpIndicator(R.drawable.app_icon);
and finally it worked.

Jakub Michalko
  • 2,099
  • 1
  • 11
  • 12
3

I tried to set up toolbar like @Gabriele Mariotti, but I had some problem with title. So then I set order to

toolbar.setTitle("Title")
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_good);

and it works.

Valentin Blokhin
  • 515
  • 1
  • 6
  • 16
3

I just found the solution. It is really very simple:

mDrawerToggle.setDrawerIndicatorEnabled(false);

Hope it will help you.

xiaohu Wang
  • 859
  • 7
  • 4
3

I used the method below which really is a conundrum of all the ones above. I also found that onOptionsItemSelected is never activated.

    mDrawerToggle.setDrawerIndicatorEnabled(false);
    getSupportActionBar().setHomeButtonEnabled(true);

    Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
    if (toolbar != null) {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }
Lettings Mall
  • 300
  • 3
  • 10
3

You can use invalidate() method to change toolbar state in any place. Example:

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

toolbar.setNavigationIcon(R.mipmap.arrow_white);
toolbar.invalidate();       // restore toolbar
Gogi Bobina
  • 1,086
  • 1
  • 8
  • 25
3

Remove this line from activity if you have added

 @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

Then set icon

 getSupportActionBar().setHomeAsUpIndicator(icon);
Nidhin
  • 1,818
  • 22
  • 23
1

work for me...

<android.support.v7.widget.Toolbar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/toolBar"
        android:background="@color/colorGreen"
        app:title="Title"
        app:titleTextColor="@color/colorBlack"
        app:navigationIcon="@drawable/ic_action_back"/>
1

In case you don't wish to set the toolbar as the action bar, you can use this:

        val toggle = ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
        toggle.isDrawerSlideAnimationEnabled = false
        toggle.isDrawerIndicatorEnabled = false
        toggle.setHomeAsUpIndicator(AppCompatResources.getDrawable(this, ...))
        drawer!!.addDrawerListener(toggle)
        toggle.setToolbarNavigationClickListener {
            setDrawerOpened(!isDrawerOpened())
        }
        toggle.syncState()

fun setDrawerOpened(open: Boolean) {
    if (open == drawerLayout.isDrawerOpen(GravityCompat.START))
        return
    if (open)
        drawerLayout.openDrawer(GravityCompat.START)
    else drawerLayout.closeDrawer(GravityCompat.START)
}
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • This solution worked for me after trying a lot of different options. And I believe this option ```toggle.isDrawerIndicatorEnabled = false``` was critical in ensuring a new icon can be set. – Anil Gorthy Nov 23 '19 at 14:35
-1

Try this:

  <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:toolbar="http://schemas.android.com/apk/res-auto"
        android:id="@+id/tool_drawer"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        toolbar:navigationIcon="@drawable/ic_navigation">

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