Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I alter a MenuItem on the Options Menu on Android?

I have an Options Menu on my Activity with an MenuItem "Start". When this MenuItem is selected I would like to alter the Menu so it contains a MenuItem "Stop". And finally when "Stop" is selected, I would like to alter back to "Start".

Here is parts of my code that isn't working. I have both "Start" and "Stop" listed in mymenu.xml I should probably remove "stop" when the menu is created:

public class MyActivity extends Activity {     private boolean isStarted = false;      @Override     public boolean onCreateOptionsMenu(Menu menu) {         MenuInflater inflater = getMenuInflater();         menu.removeItem(R.id.stop);         inflater.inflate(R.menu.mymenu, menu);         return true;     }      @Override     public boolean onOptionsItemSelected(MenuItem item) {         switch (item.getItemId()) {         case R.id.start:             isStarted = true;             return true;         case R.id.stop:             isStarted = false;             return true;         default:             return super.onOptionsItemSelected(item);         }     }      @Override     public boolean onPrepareOptionsMenu(Menu menu) {         if(isStarted) {             menu.removeItem(R.id.start);             menu.add(R.id.stop);         } else {             menu.removeItem(R.id.stop);             menu.add(R.id.start);         }         return true;     } } 
like image 365
Jonas Avatar asked Nov 16 '10 22:11

Jonas


2 Answers

For this type of operation I usually choose not to alter the menu items, but just hide the ones you don't need:

@Override public boolean onPrepareOptionsMenu(Menu menu) {     super.onPrepareOptionsMenu(menu);     menu.findItem(R.id.start).setVisible(!isStarted);     menu.findItem(R.id.stop).setVisible(isStarted);     return true; } 
like image 119
Flygenring Avatar answered Oct 08 '22 18:10

Flygenring


Flygenring answer is correct, but menu.findItem() is laggy and calling it within onPrepareOptionsMenu(Menu menu) produces bad user experience. It's better to get MenuItem object once while creating menu, and then just call setVisible each time menu occures on screen:

    MenuItem mDynamicMenuItem;      @Override     public boolean onCreateOptionsMenu(Menu menu) {         // Inflate the menu; this adds items to the action bar if it is present.         getMenuInflater().inflate(R.menu.main, menu);         // Get dynamic menu item         mDynamicMenuItem = menu.findItem(R.id.menu_item);         return true;     }      // Prepare the Screen's standard options menu to be displayed. This is called right      // before the menu is shown, every time it is shown. You can use this method to     // efficiently enable/disable items or otherwise dynamically modify the contents.     @Override     public boolean onPrepareOptionsMenu(Menu menu) {         super.onPrepareOptionsMenu(menu);         // Here is just a good place to update item         mDynamicMenuItem.setVisible(isVisible);         return true;     } 
like image 26
i.shadrin Avatar answered Oct 08 '22 19:10

i.shadrin