To define the menu, create an XML file inside your project's res/menu/ directory and build the menu with the following elements: <menu> Defines a Menu , which is a container for menu items.
A menu resource defines an application menu (Options Menu, Context Menu, or submenu) that can be inflated with MenuInflater . For a guide to using menus, see the Menus developer guide. file location: res/menu/filename.xml. The filename will be used as the resource ID.
The getItemId() method queries the ID for the selected menu item, which you should assign to each menu item in XML using the android:id attribute, as shown in the section about Defining a Menu in XML. When you successfully handle a menu item, return true .
Substitute the switch
with an if/else if
construct.
int id = item.getItemId();
if(id == R.id.menu_item_one) {
// ...
}
else if(id == R.id.menu_item_two) {
// ...
}
This is neccessary since ADT 14 because the final modifier was removed from id's in the R class.
See Non-constant Fields in Case Labels
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With