Android: onCreateOptionsMenu() item action
Asked Answered
P

4

25

I have a menu created through:

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    menu.add("Email");

    return super.onCreateOptionsMenu(menu);
  }

But I can't remember how to set a onclicklistener so when its selected I can run my email function.

Prunella answered 13/7, 2011 at 14:31 Comment(0)
M
43

Override onOptionsItemSelected(MenuItem item). So it would be like

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case 0:
            // do whatever
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

EDIT:

Since this has gotten so many points, I should note that it is very good to add ID's to the menu options. A good way to ensure they are always unique is to define them in an ids.xml resource that is put in the res/values folder.

ids.xml

<resources>
    <item name="menu_action1" type="id"/>
    <item name="menu_action2" type="id"/>
    <item name="menu_action3" type="id"/>
</resources>

Then when you override the onCreateOptionsMenu(Menu menu) method, you can use the IDs like so:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);

  menu.add(Menu.NONE, R.id.menu_action1, Menu.NONE, R.string.menu_action1);
  menu.add(Menu.NONE, R.id.menu_action2, Menu.NONE, R.string.menu_action1);

  return true;
}

Override onOptionsItemSelected(MenuItem item).

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_action1:
            // do whatever
            return true;
        case R.id.menu_action2:
            // do whatever
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

The reason you do this is the Activity would override this with menu options, but Fragments can also add their own menu items. Using the ids.xml ensures the IDs are unique no matter which order they are placed.

Munson answered 13/7, 2011 at 14:35 Comment(1)
As a good programming practice, I'd use ids rather than hardcoding the numbers in the case.Metalepsis
B
18

That won't work. You should define IDs for your menu items:

public static final int MENU_ADD = Menu.FIRST;
public static final int MENU_DELETE = Menu.FIRST + 1;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    menu.add(Menu.NONE, MENU_ADD, Menu.NONE, "Add");
    menu.add(Menu.NONE, MENU_DELETE, Menu.NONE, "Delete");
    return true;
}

   @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch(item.getItemId())
        {
            case MENU_ADD:

            return true;
        case MENU_DELETE:

            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
Bradybradycardia answered 22/11, 2012 at 15:14 Comment(0)
M
9

From Android developer guide

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
    newGame();
    return true;
case R.id.help:
    showHelp();
    return true;
default:
    return super.onOptionsItemSelected(item);
}
}
Mycenaean answered 13/7, 2011 at 14:35 Comment(0)
F
6
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
        case R.id.itemid:
            //do cool stuff
            break;
          }
     }
Few answered 13/7, 2011 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.