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.
Override
onOptionsItemSelected(MenuItem item). So it would be likeEDIT:
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.xmlresource that is put in theres/valuesfolder.ids.xml
Then when you override the
onCreateOptionsMenu(Menu menu)method, you can use the IDs like so:Override
onOptionsItemSelected(MenuItem item).The reason you do this is the
Activitywould override this with menu options, butFragmentscan also add their own menu items. Using theids.xmlensures the IDs are unique no matter which order they are placed.