Adding / removing menu menu menu Dynamic

I have a problem, I want to add an item to the toolbar (material design) programmatically.

This is my menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <item android:id="@+id/action_settings" android:title="@string/action_settings" android:orderInCategory="100" android:showAsAction="never" /> </menu> 

I know that you change text, color, background and @override listener

 toolbar.setBackgroundColor toolbar.setTextColor toolbar.setNavigationIcon toolbar.setText 

I do not know how to add a menu with the category " android:orderInCategory="300 "

Thanks.

Note: I have the whole fragment, without 1 activity

 Tree - > Activity - > Fragment1(here add menu item) - > Fragment2(add/remove menu item) - > Fragmentx .. 
+6
source share
6 answers

Try using getMenu () on this toolbar, which returns a menu

 private static final String placeholder1="Something"; private static final int FIRST_ID=Menu.FIRST; private static final int SECOND_ID=Menu.FIRST+1; //To add an item toolbar.getMenu().add(Menu.NONE,FIRST_ID,Menu.NONE,R.string.placeholder1); toolbar.getMenu().add(Menu.NONE,SECOND_ID,Menu.NONE,R.string.placeholder2); //and to remove a element just remove it with id toolbar.getMenu().removeItem(FIRST_ID); 
+3
source

First of all, put the element you want to show / hide in your XML file. Say this is "action_settings", as indicated in your code, override "onCreateOptionsMenu"

 @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.your_menu, menu); MenuItem item = menu.findItem(R.id.action_settings); if (yourConditionToShow) { item.setVisible(true); } else { item.setVisible(false); } return true; } 
+1
source

If you do not have access to the toolbar, you can do the following:

1 - add an item to the menu through onCreateOptionsMenu ():

 public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.my_item_id, menu); return true; } 

2 - Create a boolean equal to true if the item should be visible

3 - Hide or show the item according to the value of your boolean in onPrepareOptionsMenu ()

 @Override public boolean onPrepareOptionsMenu(Menu menu) { MenuItem mi = menu.findItem(R.id.my_item_id); if(displayItem) mi.setVisible(true); else mi.setVisible(false); return super.onPrepareOptionsMenu(menu); } 
+1
source

In your XML menu file, you can add a menu item that is not visible (android: visible = false) and render it visually whenever you want.

0
source

In onCreateOptionmenu , try this code

 //menu.add("groupId", "ItemId", "OrderID", "title") MenuItem item = menu.add(1, 100, 300, "Settings"); item.setIcon(R.drawable.ic_launcher); item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 

You can also add settings like xml. You can check the itemid element ( "100" ) in onOptionsItemSelected .

If you do not want to use 100 as an ItemId, you can create an ids resource file. And you can use it like this

 //R.id.action_settings is from ids resource file MenuItem item = menu.add(1, R.id.action_settings, 300, "Settings"); 
0
source

1 Solution:

@Chitrang Thanks to the idea, I decided with this line:

 toolbar.inflateMenu(R.menu.menu_activity1); toolbar.inflateMenu(R.menu.menu_activity2); toolbar.inflateMenu(R.menu.menu_activityx); 

Options select R.menu.item1,2, x

2 Solution: Create your own toolbar: in "android.support.v7.widget.Toolbar" and you can manage all the icons.

  <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"></LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Toolbar Title" android:layout_gravity="center" android:id="@+id/toolbar_title" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:gravity="right" android:layout_marginRight="15dp"> <ImageView android:layout_width="wrap_content" android:layout_height="24dp" android:id="@+id/imageView10" android:src="@drawable/sun" android:adjustViewBounds="true" /> </LinearLayout> </LinearLayout> 
0
source

All Articles