Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android toolbar setNavigationIcon not working

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.

like image 548
Mathbl Avatar asked Oct 29 '14 21:10

Mathbl


3 Answers

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

like image 125
Hsieh Avatar answered Oct 15 '22 08:10

Hsieh


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);
like image 36
Oleh Pavliuchenko Avatar answered Oct 15 '22 08:10

Oleh Pavliuchenko


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);
    }
like image 4
Mạnh Hoàng Huynh Avatar answered Oct 15 '22 07:10

Mạnh Hoàng Huynh