11

So, I have a BaseActivity in which I have a toolbar and I call setSupportActionBar(toolbar).

In some of my activities that extends BaseActivity, I would like to change the navigation icon (the default arrow) to another drawable. But when I call toolbar.setNavigationIcon(myDrawable) it doesn't work, it still shows the default left pointing arrow icon.

Any idea? Thanks.

Mathbl
  • 5,047
  • 4
  • 19
  • 24
  • possible duplicate : http://stackoverflow.com/questions/26525229/toolbar-navigation-icon-never-set – TooCool Oct 29 '14 at 21:56

3 Answers3

49

I think you can set like this

    menuDrawerToggle = new ActionBarDrawerToggle(this, menuDrawer, toolbar, R.string.drawer_open, R.string.drawer_close){...}

    menuDrawerToggle.syncState();

    toolbar.setNavigationIcon(getResources().getDrawable(yourDrawable));

put setNavigationIcon after syncState()

Hsieh
  • 521
  • 6
  • 9
  • 2
    WTF!?!? I would never have found this, how the heck did you figure this out? I'd throw you a bunch of rep if I knew how. – Anthony May 01 '15 at 21:49
  • How does this only have (now 7) votes. Great find!! – Brian Crider Aug 15 '15 at 22:27
  • Great answer. Can't belive it – Calin Martinconi Jan 06 '16 at 15:04
  • This is the right answer. Works like a charm! Life saver! – Ajmal Salim Jun 23 '16 at 19:24
  • What logic does this use? I’ve tried `android:navigationIcon="@drawable/ic_navigation”` in my `android.support.v7.widget.Toolbar` design file and it failed. Tried toolbar.setIcon in onCreate after `setSupportActionBar(toolbar);` and it failed but this works. Is there anything in the documentation that mentions that navigationIcon must be set after a call to syncState. What is the issue here / what goes on under the hood? – Saifur Rahman Mohsin Mar 11 '17 at 23:00
5

In my case: I don`t use ActionBarDrawerToggle. For me helpful was: to change order of methods calls.

From:

Toolbar toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_chevron_left_white_24dp);

To:

Toolbar toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_chevron_left_white_24dp);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
5

In my case, setNavigationIcon after syncState as @Hsieh not work! My solution is set in onPostCreate method as below. Override this method in your activity

  @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mToolbar.setNavigationIcon(R.drawable.ic_menu_button);
    }