Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Menu item IDs in an Android library project?

People also ask

Where do you define the items for a menu in an Android application?

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.

What is a menu resource in Android?

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.

What method should you use to determine which menu items was selected by the user?

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