Android options menu


Download Android options menu sample

The options menu is the primary collection of menu items for an activity. It's where you should place actions that have a global impact on the app, such as "Search," "Compose email" and "Settings."

Android options menu Android options menu


Elements of menu?


It has 3 elements as explained below:

1. <menu> element

Menu, is a container for menu items. A <menu> element must be the root node for the file and can hold one or more <item> and <group> elements.

2. <item> element

<item> creates a MenuItem, which represents a single item in a menu. This element may contain a nested <menu> element in order to create a submenu.

Xml attributes of <item> element

Attribute Description
android:id A unique resource ID.
android:title The menu title as a string resource or raw string.
android:titleCondensed This title is used for situations in which the normal title is too long.
android:icon An image to be used as the menu item icon.
android:onClick The method to call when this menu item is clicked. The method must be declared in the activity as public and accept a MenuItem as its only parameter, which indicates the item clicked.
android:showAsAction specify when and how this item should appear as an action item in the app bar.
  • ifRoom: Place this item in the app bar if there is room for it. If there is not room for all the items marked "ifRoom", the items with the lowest orderInCategory values are displayed as actions, and the remaining items are displayed in the overflow menu.
  • withText: Specify to include title with action item. We can include this value along with one of the others as a flag set, by separating them with a pipe |.
  • never: Place the item in the app bar's overflow menu.
  • always: Always place this item in the app bar.
  • collapseActionView: The action view associated with this action item is collapsible.
android:menuCategory define the item's priority. Possible values are:
  • container: For items that are part of a container.
  • system: For items that are provided by the system.
  • secondary: For items that are user supplied secondary options.
  • alternative: For items that are alternative actions on the data that is currently displayed.
android:orderInCategory The order of "importance" of the item, within a group.

3. <group> element

A menu group is an invisible container, share common properties like visible, enabled, checkable etc. It can contain one or more <item> elements and it must be a child of a <menu> element.


How to add an options menu in xml and programatically?


To add an options menu in an Activity, you must override onCreateOptionsMenu method which is called only once.

Here are 2 ways, you can create option menu:

  1. Create a menu resource inside res -> menu and inflate it inside onCreateOptionMenu method of your activity. example is:

    my_menu is the name of menu resource file

  2. Second way of creating option menu is through programatically. Example of creating option menu programatically is:


How to change options menu at runtime?


Sometimes, we need to change option menu at runtime, but problem is onCreateOptionsMenu method is called only once, so how we can do this task.

call ActivityObject.invalidateOptionsMenu(), as the name suggests that will invalidate the current menu and consequently ActivityObject.onPrepareOptionsMenu() will be called again. So, override onPrepareOptionsMenu method and place menu creation code.Example is:


How to handle click event of an option menu item?


When a menu item is clicked onOptionsItemSelected method is called. This is the place where you will write your click handler code.

If you've declared onClick attribute of Menu item in menu resource, then you will create a method with the same name in your activity and will place your click handler code there.


Android options menu example


Step 1: Create a new project in Android Studio and name it Android Options Menu.

Step 2: Create a folder menu inside res directory if it is not already present.

Step 3: Open res -> menu and add a new menu resource file with a name my_menu.xml. Now, replace the code of my_menu.xml with the below given code.

my_menu.xml

Step 4: Open MainActivity.java and replace its code with below given code.

MainActivity.java

Step 5:Clean and run your project to see the result.

Share & Like

Tutorial topics

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.