Blanche
Engineer
Engineer
  • UID619
  • Fans2
  • Follows2
  • Posts59
Reads:840Replies:0

Study notes of Android: Android menus

Created#
More Posted time:Nov 1, 2016 17:05 PM
Menus in Android can be divided into three categories: the Option Menu, the Context Menu and the Sub Menu.
Option Menu: It will be displayed when you click the Menu button on the device. It does not support checking or ticking options.
Context Menu: It will be displayed when you press and hold a component. It does not support menu item icons.
Sub Menu: The sub menus. It does not support menu item icons or nested sub menus.
 
The methods of the Menu class for adding menu items and sub menus are add() and addSubMenu():
MenuItem add(CharSequence title): to add a menu item
MenuItem add(int groupId, int itemId, int order, int titleRes): to add a menu item in the groudId group
MenuItem add(int titleRes): to add a menu item
MenuItem add(int groupId, int itemId, int order, CharSequence title): to add a menu item in the groudId group
SubMenu addSubMenu(int groupId, int itemId, int order, CharSequence title): to add a sub menu in the groudId group
SubMenu addSubMenu(int groupId, int itemId, int order, int titleRes): to add a sub menu in the groudId group
SubMenu addSubMenu(CharSequence title): to add a sub menu
SubMenu addSubMenu(int titleRes): to add a sub menu
 
 
(1) How to create an option menu:
1. Override the onCreateOptionsMenu(Menu menu) method of Activity.
2. Call the add() method of Menu to add a menu item (MenuItem). You can call the setIcon() method of the MenuItem to set an icon for the menu item.
3. Override the onOptionsMenuSelected() method of Activity to set event listening for the menu.
 
(2) How to create a sub menu:
1. Override the onCreateOptionsMenu() method of Activity. You can call the addSubMenu() method of Menu to add a sub menu.
2. Call the add() method of SubMenu to add a sub menu item.
3. Override the onCreateItemSelected() method to set event listening for the menu.
 
The SubMenu class also provides additional methods to set the menu header information:
SubMenu setHeaderIcon(Drawable icon): to set the icon of the menu header.
SubMenu setHeaderIcon(int iconRes): to set the icon of the menu header.
SubMenu setHeaderTitle(CharSequence title): to set the title of the menu header.
SubMenu setHeaderTitle(int titleRes): to set the title of the menu header.
SubMenu setHeaderView(View view): to set the menu header using the View component.
 
(3) How to create a context menu:
1. Override the onCreateContextMenu(ContextMenu, View, ContextMenu.ContextMenuInfo) method of Activity. The View parameter sets the component to trigger the context menu.
2. Call the add() method of Menu to add a menu item MenuItem.
3. Override the onContextItemSelected() method to set event listening for the context menu.
4. Call the registerForContextMenu(View) method to register a context menu for the View component.
We can use the setHeaderIcon() and setHeaderTitle() methods to set the icon and title for the context menu as we do for the sub menu.
 
(4) How to set event listening for the menu item:
We can override the onCreateItemSelected() method of Activity to set event listening. Usually we use the switch method to identify the menu item to be clicked, so we should set an ID for every menu item for identification convenience.
Of course we have an alternative solution, that is, to bind a listener for every menu item with the setOnMenuItemClickListener() method. But this approach requires significant effort and is not recommended.
(5) How to set single-choice menu items and multi-choice menu items:
setCheckable(boolean checkable): to set whether this menu item can be checked or ticked.
setGroupCheckable(int group,boolean checkable,boolean exclusive): to set whether the menu item of the group group can be checked or ticked. If exclu is set to true, this group is a single-choice menu item.
 
(6) How to set shortcuts for the menu item (it seems to be seldom used now):
setAlphabeticShortcut(char alphaChar): to set an alphabetic shortcut.
setNumericShortcut(char numericChar): to set a numeric shortcut.
setShortcut(char numericChar, char alphaChar): to set an alphabetic shortcut and a numeric shortcut at the same time.
 
(7) How to set a menu item associated with Activity:
This function aims to enable the navigation to another Activity when you click on a menu item.
You can use the setIntent(Intent intent) method.
 
(8) How to create a menu using XML files:
To do this, follow the steps below:
1. Create the xml file of the menu in the menu directory under the res file.
2. Load corresponding resources using the inflate(int menuRes, Menu menu) method in the onCreateOptionsMenu() method or the onCreateContextMenu() method of the Activity.
3. Override the event listening method related to the Activity.
 
The xml file in the menu folder should follow the rules below:
1. The root element is <menu.../>, with no redundant attributes.
2. The <group.../> wraps multiple menu items <item.../> into a menu group.
You can configure the following attributes:
checkableBehavior: to define the checkable behavior of the menu items: none (not available to be checked), all (multiple choices) and single (single choice).
menuCategory: to specify the priority of the menu items: container, system, secondary and alternative.
visible: to define whether the menu items are visible.
enable: to define whether the menu items are available.
3. The <item.../> is used to define the menu item. It can contain <menu.../> internally to form a sub menu.
You can configure the following attributes:
android:id: to set the ID
android:title: to set the title
android:icon: to set the icon
android:alphabeticShortcut: to set an alphabetic shortcut
android:numbericShortcut: to set a numeric shortcut
android:checkable: to set whether the menu items are checkable
android:checked: to set whether the menu items are checked
android:visible: to set whether the menu items are visible
android:enable: to set whether the menu items are available
 
(9) How to create a pop-up menu:
You can follow the steps below:
1. Call the new PopupMenu(Context context,View anchor) method to create a pop-up menu. The anchor parameter indicates the component that triggers the menu.
2. Call the inflate() method of MenuInflater to load the menu resources.
3. Call the show() method of PopupMenu to display the pop-up menu.
Guest