I'm trying to re-create the menu on the toolbar. That it was possible to display there new elements with different id. While I was looking for information on the network, I realized that it is possible to hypothetically remove all the elements and add new ones with the necessary id, titles, and so it is possible to solve my problem. But maybe you can somehow recreate the menu so that in this function:

@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.message_list_menu, menu); return true; } 

it was possible to use not only this one line:

 getMenuInflater().inflate(R.menu.message_list_menu, menu); 

and do something like this:

 @Override public boolean onCreateOptionsMenu(Menu menu) { switch (menu_item) { case 1: getMenuInflater().inflate(R.menu.message_list_menu, menu); break; case 2: Toast.makeText(this,"2",Toast.LENGTH_LONG).show(); break; case 3: Toast.makeText(this,"3",Toast.LENGTH_LONG).show(); break; } return true; } 

maybe there is some more or less working way? I hope that I will not have to remove and add elements dynamically.

    1 answer 1

    in Drawer e you insert fragments, why not just make a custom toolbar and not show menus from the fragment?

    Like that:

    in the drawer itself

      @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_items, menu); MenuItem searchItem = menu.findItem(R.id.action_search); if (searchItem != null) { SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); searchView.setQueryHint("Поиск"); **searchItem.setVisible(false);** } return true; } 

    Well, in the fragment where you need to show:

     @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public void onPrepareOptionsMenu(Menu menu) { MenuItem item = menu.findItem(R.id.action_search); item.setVisible(true);//показываем меню } 

    and in onCreateOptionsMenu put your menu. I put the main topic NoActionBar and NoActionBar it in the markup like this

      <include layout="@layout/toolbar" /> 
    • Tell me please, did you take into account that the framelayout is in the navigationDrawer and that my fragments are already switched there? because I'm not very sure that from the fragment you can somehow influence the menu in the general drawer. - Andrew Goroshko
    • one
      Well, this scheme works for me, it is at least. - Romanych
    • The only thing is that in those fragments where menus are not needed, they will have to hide item.setVisible (false); - Romanych