Set menus for multiple toolbars on android
Asked Answered
R

1

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?

Rolanda answered 30/6, 2015 at 2:46 Comment(4)
possible duplicate of Android Toolbar Action icon not workingLasagne
@Lasagne how did you find it duplicate?Sitdown
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?@PareshMayaniLasagne
@Lasagne thanks for your link...But I Agree with Paresh, If you compare answers...the question is different, :)Rolanda
I
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;
    }
});
Illfounded answered 30/6, 2015 at 4:6 Comment(5)
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.Rolanda
and where define 'bottom_menu' for tool_bar_bottomRolanda
please check updated answer.. and item_id is id from bottom_menu file. About OnMenuItemClickListener you need to import itIllfounded
@Hilal make sure you are not adding custom views in-side toolbar and menu id is matching with menu file.Illfounded
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 anywayEscalante

© 2022 - 2024 — McMap. All rights reserved.