Inflate a menu hierarchy from the specified XML resource.
Returns a MenuInflater with this context.
onCreateOptionsMenu (Menu menu)
Initialize the contents of the Activity's standard options menu. You should place your menu items in to menu.
onOptionsItemSelected (MenuItem item)
This method is called whenever an item in your options menu is selected
To know more about Menus, read this. Hope it helps!
Apply custom font to Menu
and then in the Activity:
Creating a Menu in an Activity
To define your own 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 holds all the menu items.
<item> : Creates a MenuItem, which represents a single item in a menu. We can also create a nested element in order to create a submenu.
Create your own xml file as the following:
To specify the options menu, override onCreateOptionsMenu() in your activity.
In this method, you can inflate your menu resource (defined in your XML file i.e., res/menu/main_menu.xml)
When the user selects an item from the options menu, the system calls your activity's overridden onOptionsItemSelected() method.
This method passes the MenuItem selected.
You can identify the item by calling getItemId(), which returns the unique ID for the menu item (defined by the android:id attribute in the menu resource - res/menu/main_menu.xml)*/
Your Activity code should look like below:
Screenshot of how your own Menu looks:
Options menu with dividers
In Android there is a default options menu, which can take a number of options. If a larger number of options needs to be displayed, then it makes sense to group those options in order to maintain clarity. Options can be grouped by putting dividers (i.e. horizontal lines) between them. In order to allow for dividers, the following theme can be used:
By changing the theme, dividers can be added to a menu.
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0