22

In my activity I use the following code for my two toolbars.

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Creating The Toolbar and setting it as the Toolbar for the activity
    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("My title");

    toolbar2 = (Toolbar) findViewById(R.id.tool_bar_bottom);
    setSupportActionBar(toolbar2);
    ...
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
 }

I want it to use menu_main.xml for the top toolbar and menu_bottom for bottom toolbar but for both top and bottom toolbar it uses menu_main.xml.

Can somebody explain how to do it correctly?

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
partiz
  • 1,206
  • 2
  • 13
  • 34
  • possible duplicate of [Android Toolbar Action icon not working](http://stackoverflow.com/questions/31128809/android-toolbar-action-icon-not-working) – Elltz Jun 30 '15 at 04:14
  • @Elltz how did you find it duplicate? – Paresh Mayani Jun 30 '15 at 04:44
  • as, the code in the question is exactly the same as the other-(also variable names), also they are all looking at/for the same solution even though the title might not be that convincing, so its either a dupe of this or a dupe of that, & why are you not convinced about it Sir?@PareshMayani – Elltz Jun 30 '15 at 13:15
  • @Elltz thanks for your link...But I Agree with Paresh, If you compare answers...the question is different, :) – partiz Jun 30 '15 at 21:55

1 Answers1

54

As you are using two ToolBars set the menu like this

toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("My title");

The above toolbar inflate menu from onCreateOptionsMenu, menu CallBack listener will be onOptionsItemSelected

Now Second ToolBar

 toolbar2 = (Toolbar) findViewById(R.id.tool_bar_bottom);
 toolbar2.inflateMenu(R.menu.bottom_menu);//changed
 //toolbar2 menu items CallBack listener 
 toolbar2.setOnMenuItemClickListener(new OnMenuItemClickListener() {

    @Override
    public boolean onMenuItemClick(MenuItem arg0) {
        if(arg0.getItemId() == R.id.item_id){

        }
        return false;
    }
});
Bharatesh
  • 8,943
  • 3
  • 38
  • 67
  • Thanks for the answer but I got this error:`Error:(37, 49) error: cannot find symbol class OnMenuItemClickListener` and `item_id` in my debuger is red color. – partiz Jun 30 '15 at 04:15
  • and where define 'bottom_menu' for `tool_bar_bottom` – partiz Jun 30 '15 at 04:26
  • please check updated answer.. and `item_id` is id from bottom_menu file. About OnMenuItemClickListener you need to import it – Bharatesh Jun 30 '15 at 04:29
  • @Hilal make sure you are not adding custom views in-side toolbar and menu id is matching with menu file. – Bharatesh Feb 13 '17 at 09:39
  • Yes it was exactly what you said but still did not worked. So I tried another way, which is adding ImageView inside Toolbar2. And then I setOnClickLİstener on ImageView by calling it from its id. Actually maybe there were no need for Toolbar2 I could use just ImageView but I did not remove Toolbar anyway – Hilal Feb 13 '17 at 13:45