Android: change program menu items programmatically

Can I change menu items programmatically? Can anyone give me an example please?

In addition, I want to disable certain elements so that they do not listen for clicks, is this possible?

+79
java android option menuitem
Aug 20 2018-11-18T00:
source share
11 answers

Everyone needs to dynamically change the menu options:

private Menu menu; // ... @Override public boolean onCreateOptionsMenu(Menu menu) { this.menu = menu; getMenuInflater().inflate(R.menu.options, menu); return true; } // ... private void hideOption(int id) { MenuItem item = menu.findItem(id); item.setVisible(false); } private void showOption(int id) { MenuItem item = menu.findItem(id); item.setVisible(true); } private void setOptionTitle(int id, String title) { MenuItem item = menu.findItem(id); item.setTitle(title); } private void setOptionIcon(int id, int iconRes) { MenuItem item = menu.findItem(id); item.setIcon(iconRes); } 
+192
Jul 28 '12 at 9:44
source share

menu.xml

  <item android:id="@+id/item1" android:title="your Item"> </item> 

enter your java file

  public void onPrepareOptionsMenu(Menu menu) { menu.removeItem(R.id.item1); } 
+29
Aug 20 '11 at 17:33
source share

As Nikolai said, do it in onPrepareOptionsMenu ().

For menu items in the action bar, you need to invalidate the menu using activity.invalidateOptionsMenu();

This is described in more detail. How to update the ActionBar when onPrepareOptionsMenu switched menu entries?

+15
Nov 14
source share

If I need to change the contents of my options menu, I execute it during onMenuOpened (). This allows me to check the current state at the moment when the user accesses the menu.

 public boolean onMenuOpened(int featureid, Menu menu) { menu.clear(); if (!editable) { MenuItem itemAdd = menu.add(0, REASSIGN, Menu.NONE, context.getString(R.string.reassign)); MenuItem itemMod = menu.add(1, EDIT, Menu.NONE, context.getString(R.string.modify)); MenuItem itemDel = menu.add(2, DELETE, Menu.NONE, context.getString(R.string.delete)); itemAdd.setShortcut('0', 'a'); itemMod.setShortcut('1', 'm'); itemDel.setShortcut('2', 'd'); } else { MenuItem itemSave = menu.add(3, SAVE, Menu.NONE, context.getString(R.string.savechanges)); itemSave.setShortcut('0', 'S'); } return true; } 
+7
Aug 20 '11 at 17:38
source share

To disable certain items:

 MenuItem item = menu.findItem(R.id.ID_ASSING_TO_THE_ITEM_IN_MENU_XML); item.setEnabled(false); 
+6
Jan 28 '13 at 4:20
source share

using the following lines i made to add values ​​to the menu

 getActivity().invalidateOptionsMenu(); 

try this job like a charm to me.

+4
Nov 25 '16 at 11:34
source share

Try this code:

 @Override public boolean onPrepareOptionsMenu(Menu menu) { this.menu=menu; updateMenuItems(menu); return super.onPrepareOptionsMenu(menu); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.document_list_activity_actions, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle presses on the action bar items if (item.getItemId() == android.R.id.home) { onHomeButtonPresssed(); }else if (item.getItemId() == R.id.action_delete) { useCheckBoxAdapter=false; deleteDocuments(); } else if (item.getItemId() == R.id.share) { useCheckBoxAdapter=false; shareDocuments(); } else if (item.getItemId() == R.id.action_tick) { useCheckBoxAdapter=true; onShowCheckboxes(); } updateMenuItems(menu); return true; } private void updateMenuItems(Menu menu){ if (useCheckBoxAdapter && menu != null) { menu.findItem(R.id.action_delete).setVisible(true); menu.findItem(R.id.share).setVisible(true); menu.findItem(R.id.action_tick).setVisible(false); } else { menu.findItem(R.id.action_delete).setVisible(false); menu.findItem(R.id.share).setVisible(false); menu.findItem(R.id.action_tick).setVisible(true); } invalidateOptionsMenu(); } 
+3
Dec 30 '15 at 6:19 06:19
source share

You can do something simple like me. Just change the text to what you need when you touch a menu item. I needed to turn off the sound and turn it on, as well as the ability to perform an action by touching it. Here is my code:

  @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.audioOn: audioOn = !audioOn; if (audioOn) item.setTitle("Audio Off"); else item.setTitle("Audio On"); return true; case R.id.touchOn: touchOn = !touchOn; if (touchOn) item.setTitle("Touch Off"); else item.setTitle("Touch On"); return true; default: return super.onOptionsItemSelected(item); } } 

audioOn and touchOn are checked by Booleans in other parts of the code. Hope this helps.

+2
Oct 30 '13 at 19:46
source share

If you have a BottomBar:

 @Override public boolean onCreateOptionsMenu(Menu menu) { if (mBottomBar.getCurrentTabId() == R.id.tab_more) { getMenuInflater().inflate(R.menu.more_menu, menu); } return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.preferences: startActivity(new Intent(this, PreferenceActivity.class)); break; } return super.onOptionsItemSelected(item); } 

Then you just need to call:

 @Override public void onBottomBarClick(int tabId) { supportInvalidateOptionsMenu(); } 
+1
Apr 14 '17 at 19:13
source share

You can complete your task simply by following these steps:

 private Menu menu; @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.drive_menu, menu); return true; } @Override public boolean onPrepareOptionsMenu(Menu menu) { this.menu = menu; return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_toggle_grid) { handleMenuOption(id); return true; } else if(id == R.id.action_toggle_list){ handleMenuOption(id); return true; } return super.onOptionsItemSelected(item); } private void handleMenuOption(int id) { MenuItem item = menu.findItem(id); if (id == R.id.action_toggle_grid){ item.setVisible(false); menu.findItem(R.id.action_toggle_list).setVisible(true); }else if (id == R.id.action_toggle_list){ item.setVisible(false); menu.findItem(R.id.action_toggle_grid).setVisible(true); } } 
0
Aug 23 '16 at 6:54
source share

Kotlin code for programmatically accessing the OptionsMenu toolbar and changing text / icon, ..:

1-We have a menu item in the file of menu items, such as: menu.xml, example code for this:

  <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/balance" android:title="0" android:orderInCategory="100" app:showAsAction="always" /> </menu> 

2- Define a variable to access the menu object in the class:

 var menu: Menu? = null 

3- initialize it in onCreateOptionsMenu:

 override fun onCreateOptionsMenu(menu: Menu): Boolean { // Inflate the menu; this adds items to the action bar if it is present. menuInflater.inflate(R.menu.main, menu) this.menu = menu return true } 

4- Access to menu items inside your code or entertainment:

 private fun initialBalanceMenuItemOnToolbar() { var menuItemBalance = menu?.findItem(R.id.balance) menuItemBalance?.title = Balance?.toString() ?: 0.toString() // for change icon : menuWalletBalance?.icon } 
0
Dec 12 '18 at 15:00
source share



All Articles