Android Menus
Android Menus
In android, Menu is a part of user interface (UI) component which is used to handle some
common functionality around the application. By using Menus in our applications, we can
provide better and consistent user experience throughout the application.
We can use Menu APIs to represent user actions and other options in our android application
activities.
In android, we can define a Menu in separate XML file and use that file in our activities or
fragments based on our requirements.
In android, to define menu, we need to create a new folder menu inside of our project resource
directory (res/menu/) and add a new XML file to build the menu with the following elements.
1
Element Description
<menu> It’s a root element to define a Menu in XML file and it will hold one or more and
elements.
<item> It is used to create a menu item and it represent a single item in menu. This element
may contain a nested <menu> element in order to create a submenu.
<group> It’s an optional and invisible for <item> elements. It is used to categorize the menu
items so they share properties such as active state and visibility.
The <item> element in menu supports different type of attributes to define item’s behaviour
and appearance. Following are the some of commonly used <item> attributes in android
applications.
Attribute Description
android:showAsAction It is used to specify how the item should appear as an action item in the
app bar.
In case if we want to add submenu in menu item, then we need to add a <menu> element as
the child of an <item>. Following is the example of defining a submenu in menu item.
Once we are done with creation of menu, we need to load the menu resource from
our activity using MenuInflater.inflate() like as shown below.
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo
menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_example, menu);
}
If we observe above code we are calling our menu using MenuInflater.inflate() method in the
form of R.menu.menu_file_name. Here our xml file name is menu_example.xml so we used file
name menu_example.
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mail:
// do something
return true;
case R.id.share:
// do something
return true;
default:
return super.onContextItemSelected(item);
}
}
If we observe above code, the getItemId() method will get the id of selected menu item based
on that we can perform our actions.
In android, we have a three fundamental type of Menus available to define a set of options and
actions in our android applications.
Options Menu
Context Menu
Popup Menu
3
Android Options Menu
In android, Options Menu is a primary collection of menu items for an activity and it is useful to
implement actions that have a global impact on the app, such as Settings, Search, etc.
In android, Context Menu is a floating menu that appears when the user performs a long click
on an element and it is useful to implement an actions that effect the selected content or context
frame.
In android, Popup Menu displays a list of items in a vertical list that’s anchored to the view that
invoked the menu and it’s useful for providing an overflow of actions that related to specific
content.
Questions:
1. How to create “menu” in Android, what are the different items of “menu”?
2. What are the commonly used <item> attributes in android applications for “menu”?
3. How to call the method “MenuInflater.inflate()” for “menu”? Write the code.
4. What is the method to handle a menu item click events? Write the code.
5. Why to use onContextItemSelected()?