Search code examples
androidtoolbarandroid-appcompatandroid-toolbarappcompat-v7-toolbar

How do I add icons to a second Toolbar


I have more than one appcompat.v7 Toolbar in my layout.

Given this is the case the solution of "main" toolbars - setSupportActionBar() and then use the hooks in onCreateOptionsMenu() and onOptionsItemSelected() - isn't what I'm looking for.

The only way I've found so far is to cheat and put the Toolbar in a horizontal LinearLayout and "fake" action bar icons with ImageViews. There has to be a real way to do this but with the solution to "main" toolbars saturating searches I can't seem to find anything.


Solution

  • You can use this code to inflate a menu in your Toolbar:

    mToolbar.inflateMenu(R.menu.menu_detail);
    

    Then you can use it to attach a listener:

    mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        //your code
                    }    
                }
    });