AndroidNavigationIcon toolbar not working

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

In some of my activities that extend BaseActivity, I would like to change the navigation icon (default arrow) to another way. But when I call toolbar.setNavigationIcon (myDrawable), it does not work, it still shows the default arrow icon on the left.

Any idea? Thanks.

+7
android android-toolbar
source share
3 answers

I think you can set this as

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

+39
source share

In my case: I am not using ActionBarDrawerToggle. It was useful for me: to change the order of method 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); 
+1
source share

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

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

All Articles