0

I am new to android and I recently studied the documentation for option menu in which i have not understood about onPrerpareOptionMenu() .In my application when a button is clicked i want to change option menu for the same activity.

Thanks...

  public boolean onPrepareOptionsMenu(Menu menu){
    return false;
    menu.clear(); //Clear view of previous menu
    MenuInflater inflater = getMenuInflater();
    if( )//condition 
        inflater.inflate(R.menu.view_record, menu);
    else
        inflater.inflate(R.menu.add_record, menu);
    return super.onPrepareOptionsMenu(menu);
}
abhishek
  • 301
  • 1
  • 5
  • 29

1 Answers1

0

I do this in several of my camera applications to change menu depending on what camera function is enabled via a slide control (although a button also works), i.e for capturing images or videos.

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    MenuItem someThing1 = menu.findItem(R.id.someThing1);
    MenuItem someThing2 = menu.findItem(R.id.someThing2);
    MenuItem someThing3 = menu.findItem(R.id.someThing3);
    MenuItem someThing4 = menu.findItem(R.id.someThing4);
    MenuItem someThing5 = menu.findItem(R.id.someThing5);
    MenuItem help = menu.findItem(R.id.help);
    MenuItem about = menu.findItem(R.id.about);
    if (some_condition) { //could be button state or..?
        someThing1.setTitle(R.string.someThing1a);
        someThing2.setTitle(R.string.someThing2a);
        someThing3.setTitle(R.string.someThing3a);
        someThing4.setVisible(false);
        someThing5.setVisible(false);
        help.setVisible(false);
        about.setVisible(false);
    } else {
        someThing1.setTitle(R.string.someThing1b);
        someThing2.setTitle(R.string.someThing2b);
        someThing3.setTitle(R.string.someThing3b);
        someThing4.setVisible(true);
        someThing5.setVisible(true);
        help.setVisible(true);
        about.setVisible(true);
    }
     return true; // this is important to call so that new menu is shown
}
Nishant Srivastava
  • 4,723
  • 2
  • 24
  • 35
Rick
  • 878
  • 1
  • 8
  • 8
  • thanks for the solution. can u plz explain about invalidateoptionmenu() why and how it is used?thanks once again – abhishek Jan 13 '15 at 16:45
  • Per API: public void invalidateOptionsMenu() Added in API level 11 Declare that the options menu has changed, so should be recreated. The onCreateOptionsMenu(Menu) method will be called the next time it needs to be displayed. For – Rick Jan 13 '15 at 16:52