Option Menu


Option Menu

Within the android framework, there is a primary collection of menus for a given activity, which is referred to as the options menu. This is where the developer should accommodate the actions that will have an impact on the app. For example, when you open a mobile browser app such as chrome, we get multiple options as shown below.

Image Not Found

In simpler words, the options menu will have options for the user. The user can make a selection, from any of those options. The options menu will appear at a specific area of the screen, however where it will appear will solely depend on the Android version, for which the app has been developed. For example, if the app has been developed for version 2.3 or lower, the options menu will be placed at the top of the screen. It will appear as soon as the menu button is pressed. For 3.0 and above, it will appear within the app bar. Something which is shown below.

Image Not Found

Just like any other declaration in Android, we can declare the options menu as well from the Activity subclass. A developer can also use Fragment subclass for this purpose. Based on the requirement, both the subclasses can be used for declaration.

The code below shows, how to override onCreateOptionsMenu(), for creating the options menu. For this, we need to inflate the “Menu” as well.

The following example creates three simple menu items.

Image Not Found