Professional Documents
Culture Documents
Menu
android.view.Menu Known Indirect Subclasses ContextMenu, SubMenu ContextMenu Extension of Menu for context menus providing functionality to modify the header of the context menu. SubMenu Subclass of Menu for sub menus.
Class Overview
Interface for managing the items in a menu. By default, every Activity supports an options menu of actions or options. You can add items to this menu and handle clicks on your additions. The easiest way of adding menu items is inflating an XML file into the Menu via MenuInflater. The easiest way of attaching code to clicks is via onOptionsItemSelected(MenuItem) and onContextItemSelected(MenuItem). Different menu types support different features:
1. Context menus: Do not support item shortcuts and item icons. 2. Options menus: The icon menus do not support item check marks and only show the item's condensed title. The expanded menus (only available if six or more menu items are visible, reached via the 'More' item in the icon menu) do not show item icons, and item check marks are discouraged. 3. Sub menus: Do not support item icons, or nested sub menus.
Summary
Constants Category code for the order integer for items/groups that are alternative actions on the data that is currently displayed -- or/add this with your base value. Category code for the order integer for items/groups that are part of a container -- or/add this with your base value. Category code for the order integer for items/groups that are user-supplied secondary (infrequently used) options -- or/add this with your base value. Category code for the order integer for items/groups that are provided by the system -- or/add this with your base value.
int CATEGORY_ALTERNATIVE
int CATEGORY_CONTAINER
int CATEGORY_SECONDARY
int CATEGORY_SYSTEM
First value for group and item identifier integers. Flag for performShortcut(int, KeyEvent, int): if set, always close the menu after executing the shortcut. Flag for addIntentOptions(int, int, int, ComponentName, Intent[], Intent, int, MenuItem[]): if set, do not automatically remove any existing menu items in the same group. Flag for performShortcut(int, KeyEvent, int): if set, do not close the menu after executing the shortcut. Value to use for group and item identifier integers when you don't care about them.
int FLAG_APPEND_TO_GROUP
int FLAG_PERFORM_NO_CLOSE
int NONE
Public Methods
abstract add(int titleRes) MenuItem Add a new item to the menu. abstract add(int groupId, int itemId, int order, CharSequence title) MenuItem Add a new item to the menu. add(int groupId, int itemId, int order, int titleRes) abstract MenuItem Variation on add(int, int, int, CharSequence) that takes a string resource identifier instead of the string itself. abstract add(CharSequence title) MenuItem Add a new item to the menu. addIntentOptions(int groupId, int itemId, int order, ComponentName caller, Intent[] specifics, Intent intent, int flags, MenuItem[] outSpecificItems) Add a group of menu items corresponding to actions that can be performed for a particular Intent. abstract addSubMenu(int titleRes) SubMenu Add a new sub-menu to the menu. addSubMenu(int groupId, int itemId, int order, int titleRes) abstract SubMenu Variation on addSubMenu(int, int, int, CharSequence) that takes a string resource identifier for the title instead of the string itself.
abstract int
abstract addSubMenu(CharSequence title) SubMenu Add a new sub-menu to the menu. abstract addSubMenu(int groupId, int itemId, int order, CharSequence title) SubMenu Add a new sub-menu to the menu. abstract void abstract void clear() Remove all existing items from the menu, leaving it empty as if it had just been created. close() Closes the menu, if open.
abstract findItem(int id) MenuItem Return the menu item with a particular identifier. abstract getItem(int index) MenuItem Gets the menu item at the given index. abstract boolean abstract boolean abstract boolean abstract boolean abstract void abstract void abstract void hasVisibleItems() Return whether the menu currently has item items that are visible. isShortcutKey(int keyCode, KeyEvent event) Is a keypress one of the defined shortcut keys for this window. performIdentifierAction(int id, int flags) Execute the menu item action associated with the given menu identifier. performShortcut(int keyCode, KeyEvent event, int flags) Execute the menu item action associated with the given shortcut character. removeGroup(int groupId) Remove all items in the given group. removeItem(int id) Remove the item with the given identifier. setGroupCheckable(int group, boolean checkable, boolean exclusive) Control whether a particular group of items can show a check mark. A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
setGroupEnabled(int group, boolean enabled) Enable or disable all menu items that are in the given group. setGroupVisible(int group, boolean visible) Show or hide all menu items that are in the given group. setQwertyMode(boolean isQwerty)
abstract void
Control whether the menu should be running in qwerty mode (alphabetic shortcuts) or 12-key mode (numeric shortcuts). size() Get the number of items in the menu.
abstract int
Constants
public static final int CATEGORY_ALTERNATIVE
Category code for the order integer for items/groups that are alternative actions on the data that is currently displayed -- or/add this with your base value.
Constant Value: 262144 (0x00040000) public static final int CATEGORY_CONTAINER
Category code for the order integer for items/groups that are part of a container -- or/add this with your base value.
Constant Value: 65536 (0x00010000) public static final int CATEGORY_SECONDARY
Category code for the order integer for items/groups that are user-supplied secondary (infrequently used) options -- or/add this with your base value.
Constant Value: 196608 (0x00030000) public static final int CATEGORY_SYSTEM
Category code for the order integer for items/groups that are provided by the system -- or/add this with your base value.
Constant Value: 131072 (0x00020000) public static final int FIRST
Flag for performShortcut(int, KeyEvent, int): if set, always close the menu after executing the shortcut. Closing the menu also resets the prepared state.
Constant Value: 2 (0x00000002) public static final int FLAG_APPEND_TO_GROUP
Flag for addIntentOptions(int, int, int, ComponentName, Intent[], Intent, int, MenuItem[]) : if set, do not automatically remove any existing menu items in the same group.
Constant Value: 1 (0x00000001) public static final int FLAG_PERFORM_NO_CLOSE
Flag for performShortcut(int, KeyEvent, int): if set, do not close the menu after executing the shortcut.
Constant Value: 1 (0x00000001) public static final int NONE
Value to use for group and item identifier integers when you don't care about them.
Constant Value: 0 (0x00000000)
Public Methods
public abstract MenuItem add (int titleRes)
Add a new item to the menu. This item displays the given title for its label.
Parameters titleRes Resource identifier of title string. Returns
public abstract MenuItem add (int groupId, int itemId, int order, CharSequence title)
Add a new item to the menu. This item displays the given title for its label.
Parameters groupId The group identifier that this item should be part of. This can be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group.
itemId Unique item ID. Use NONE if you do not need a unique ID. order The order for the item. Use NONE if you do not care about the order. See getOrder(). title Returns
public abstract MenuItem add (int groupId, int itemId, int order, int titleRes)
Variation on add(int, int, int, CharSequence) that takes a string resource identifier instead of the string itself.
Parameters groupId The group identifier that this item should be part of. This can also be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group.
itemId Unique item ID. Use NONE if you do not need a unique ID. order The order for the item. Use NONE if you do not care about the order. See getOrder(). titleRes Resource identifier of title string. Returns
Add a new item to the menu. This item displays the given title for its label.
Parameters title The text to display for the item. Returns
public abstract int addIntentOptions (int groupId, int itemId, int order, ComponentName caller, Intent[] specifics, Intent intent, int flags, MenuItem[] outSpecificItems)
Add a group of menu items corresponding to actions that can be performed for a particular Intent. The Intent is most often configured with a null action, the data that the current activity is working with, and includes either
the CATEGORY_ALTERNATIVE or CATEGORY_SELECTED_ALTERNATIVE to find activities that have said they would like to be included as optional action. You can, however, use any Intent you want. See queryIntentActivityOptions(ComponentName, Intent[], Intent, int) for more * details on the caller, specifics, and intent arguments. The list returned by that function is used to populate the resulting menu items. All of the menu items of possible options for the intent will be added with the given group and id. You can use the group to control ordering of the items in relation to other items in the menu. Normally this function will automatically remove any existing items in the menu in the same group and place a divider above and below the added items; this behavior can be modified with the flags parameter. For each of the generated items setIntent(Intent) is called to associate the appropriate Intent with the item; this means the activity will automatically be started for you without having to do anything else.
Parameters groupId itemId order caller specifics intent flags outSpecificItems The group identifier that the items should be part of. This can also be used to define groups of items for batch state changes. Normally use NONE if the items should not be in a group. Unique item ID. Use NONE if you do not need a unique ID. The order for the items. Use NONE if you do not care about the order. See getOrder(). The current activity component name as defined by queryIntentActivityOptions(). Specific items to place first as defined by queryIntentActivityOptions(). Intent describing the kinds of items to populate in the list as defined by queryIntentActivityOptions(). Additional options controlling how the items are added. Optional array in which to place the menu items that were generated for each of the specifics that were requested. Entries may be null if no activity was found for that specific action.
Returns
Add a new sub-menu to the menu. This item displays the given title for its label. To modify other attributes on the submenu's menu item, use getItem().
Parameters titleRes Resource identifier of title string.
Returns
public abstract SubMenu addSubMenu (int groupId, int itemId, int order, int titleRes)
Variation on addSubMenu(int, int, int, CharSequence) that takes a string resource identifier for the title instead of the string itself.
Parameters groupId The group identifier that this item should be part of. This can also be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group.
itemId Unique item ID. Use NONE if you do not need a unique ID. order The order for the item. Use NONE if you do not care about the order. See getOrder(). titleRes Resource identifier of title string. Returns
Add a new sub-menu to the menu. This item displays the given title for its label. To modify other attributes on the submenu's menu item, use getItem().
Parameters title The text to display for the item. Returns
public abstract SubMenu addSubMenu (int groupId, int itemId, int order, CharSequence title)
Add a new sub-menu to the menu. This item displays the given title for its label. To modify other attributes on the submenu's menu item, use getItem(). Note that you can only have one level of sub-menus, i.e. you cannnot add a subMenu to a subMenu: An UnsupportedOperationException will be thrown if you try.
Parameters groupId The group identifier that this item should be part of. This can also be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group.
itemId Unique item ID. Use NONE if you do not need a unique ID. order The order for the item. Use NONE if you do not care about the order. See getOrder(). title Returns
Remove all existing items from the menu, leaving it empty as if it had just been created.
public abstract void close ()
The menu item object, or null if there is no item with this identifier.
Throws IndexOutOfBoundsException when index < 0 || >= size() public abstract boolean hasVisibleItems ()
Return whether the menu currently has item items that are visible.
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
Returns
Execute the menu item action associated with the given menu identifier.
Parameters id Identifier associated with the menu item. flags Additional option flags or 0. Returns
If the given identifier exists and is shown, returns true; else returns false.
public abstract boolean performShortcut (int keyCode, KeyEvent event, int flags)
Execute the menu item action associated with the given shortcut character.
Parameters keyCode The keycode of the shortcut key. event Key event message. flags Returns
If the given shortcut exists and is shown, returns true; else returns false.
Parameters groupId The group to be removed. If there are no items in this group, nothing happens. public abstract void removeItem (int id)
Control whether a particular group of items can show a check mark. This is similar to calling setCheckable(boolean) on all of the menu items with the given group identifier, but in addition you can control whether this group contains a mutually-exclusive set items. This should be called after the items of the group have been added to the menu.
Parameters group The group of items to operate on.
checkable Set to true to allow a check mark, false to disallow. The default is false. exclusive If set to true, only one item in this group can be checked at a time; checking an item will automatically uncheck all others in the group. If set to false, each item can be checked independently of the others.
Enable or disable all menu items that are in the given group.
Parameters group The group of items to operate on. enabled If true the items will be enabled, else they will be disabled. public abstract void setGroupVisible (int group, boolean visible)
Show or hide all menu items that are in the given group.
Parameters group The group of items to operate on. visible If true the items are visible, else they are hidden.
Control whether the menu should be running in qwerty mode (alphabetic shortcuts) or 12-key mode (numeric shortcuts).
Parameters isQwerty If true the menu will use alphabetic shortcuts; else it will use numeric shortcuts. public abstract int size ()
Get the number of items in the menu. Note that this will change any times items are added or removed from the menu.
Returns
MenuInflater
extends Object java.lang.Object android.view.MenuInflater
Class Overview
This class is used to instantiate menu XML files into Menu objects. For performance reasons, menu inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use MenuInflater with an XmlPullParser over a plain XML file at runtime; it only works with an XmlPullParser returned from a compiled resource (R. something file.)
Summary
Public Constructors MenuInflater(Context context) Constructs a menu inflater. Public Methods inflate(int menuRes, Menu menu) void Inflate a menu hierarchy from the specified XML resource.
[Expand] Inherited Methods From class java.lang.Object clone() Object Creates and returns a copy of this Object. equals(Object o) boolean Compares this instance with the specified object and indicates if they are equal. finalize() void Is called before the object's memory is being reclaimed by the VM. getClass() final Class<? extends Object> Returns the unique instance of Class which represents this object's class. hashCode() int Returns an integer hash code for this object. notify() final void Causes a thread which is waiting on this object's monitor (by means of calling one of the wait() methods) to be woken up. notifyAll() final void Causes all threads which are waiting on this object's monitor (by means of calling one of the wait() methods) to be woken up. toString() String Returns a string containing a concise, human-readable description of this object. wait(long millis, int nanos) final void Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the specified timeout expires. wait(long millis) final void Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the specified timeout expires.
wait() final void Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
Public Constructors
public MenuInflater (Context context)
Public Methods
public void inflate (int menuRes, Menu menu)
Inflate a menu hierarchy from the specified XML resource. Throws InflateException if there is an error.
Parameters menuRes Resource ID for an XML layout resource to load (e.g., R.menu.main_activity) menu The Menu to inflate into. The items and submenus will be added to this Menu.
MenuItem
android.view.MenuItem
Class Overview
Interface for direct access to a previously created menu item. An Item is returned by calling one of the add(int) methods.
Summary
Nested Classes interface MenuItem.OnMenuItemClickListener Interface definition for a callback to be invoked when a menu item is clicked. Public Methods A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
getAlphabeticShortcut() abstract char Return the char for this menu item's alphabetic shortcut. getGroupId() abstract int Return the group identifier that this menu item is part of. getIcon() abstract Drawable Returns the icon for this item as a Drawable (getting it from resources if it hasn't been loaded before). getIntent() abstract Intent Return the Intent associated with this item. getItemId() abstract int Return the identifier for this menu item. getMenuInfo() abstract ContextMenu.ContextMenuInfo Gets the extra information linked to this menu item. getNumericShortcut() abstract char Return the char for this menu item's numeric (12-key) shortcut. getOrder() abstract int Return the category and order within the category of this item. getSubMenu() abstract SubMenu Get the sub-menu to be invoked when this item is selected, if it has one. getTitle() abstract CharSequence Retrieve the current title of the item. getTitleCondensed() abstract CharSequence Retrieve the current condensed title of the item. hasSubMenu() abstract boolean Check whether this item has an associated sub-menu. abstract boolean isCheckable()
Return whether the item can currently display a check mark. isChecked() abstract boolean Return whether the item is currently displaying a check mark. isEnabled() abstract boolean Return the enabled state of the menu item. isVisible() abstract boolean Return the visibility of the menu item. setAlphabeticShortcut(char alphaChar) abstract MenuItem Change the alphabetic shortcut associated with this item. setCheckable(boolean checkable) abstract MenuItem Control whether this item can display a check mark. setChecked(boolean checked) abstract MenuItem Control whether this item is shown with a check mark. setEnabled(boolean enabled) abstract MenuItem Sets whether the menu item is enabled. setIcon(Drawable icon) abstract MenuItem Change the icon associated with this item. setIcon(int iconRes) abstract MenuItem Change the icon associated with this item. setIntent(Intent intent) abstract MenuItem Change the Intent associated with this item. setNumericShortcut(char numericChar) abstract MenuItem Change the numeric shortcut associated with this item. setOnMenuItemClickListener(MenuItem.OnMenuItemClickListener menuItemClickListener) Set a custom listener for invocation of this menu item.
abstract MenuItem
setShortcut(char numericChar, char alphaChar) abstract MenuItem Change both the numeric and alphabetic shortcut associated with this item. setTitle(int title) abstract MenuItem Change the title associated with this item. setTitle(CharSequence title) abstract MenuItem Change the title associated with this item. setTitleCondensed(CharSequence title) abstract MenuItem Change the condensed title associated with this item. setVisible(boolean visible) abstract MenuItem Sets the visibility of the menu item.
Public Methods
public abstract char getAlphabeticShortcut ()
Return the group identifier that this menu item is part of. The group identifier can not be changed after the menu is created.
Returns
Returns the icon for this item as a Drawable (getting it from resources if it hasn't been loaded before).
Returns
Return the Intent associated with this item. This returns a reference to the Intent which you can change as desired to modify what the Item is holding.
Returns
Return the identifier for this menu item. The identifier can not be changed after the menu is created.
Returns
Gets the extra information linked to this menu item. This extra information is set by the View that added this menu item to the menu.
Returns
The extra information linked to the View that added this menu item to the menu. This can be null.
Return the char for this menu item's numeric (12-key) shortcut.
Returns
Return the category and order within the category of this item. This item will be shown before all items (within its category) that have order greater than this value. An order integer contains the item's category (the upper bits of the integer; set by or/add the category with the order within the category) and the ordering of the item within that category (the lower bits). Example categories are CATEGORY_SYSTEM, CATEGORY_SECONDARY, CATEGORY_ALTERNATIVE, CATEGORY_CONTAINER. See Menu for a full list.
Returns
Get the sub-menu to be invoked when this item is selected, if it has one. See hasSubMenu().
Returns
The title.
Retrieve the current condensed title of the item. If a condensed title was never set, it will return the normal title.
Returns
Check whether this item has an associated sub-menu. I.e. it is a sub-menu of another menu.
Returns
Change the alphabetic shortcut associated with this item. The shortcut will be triggered when the key that generates the given character is pressed alone or along with with the alt key. Case is not significant and shortcut characters will be displayed in lower case. Note that menu items with the characters '\b' or '\n' as shortcuts will get triggered by the Delete key or Carriage Return key, respectively. See Menu for the menu types that support shortcuts.
Parameters alphaChar The alphabetic shortcut key. This is the shortcut when using a keyboard with alphabetic keys. Returns
Control whether this item can display a check mark. Setting this does not actually display a check mark (see setChecked(boolean) for that); rather, it ensures there is room in the item in which to display a check mark. See Menu for the menu types that support check marks.
Parameters checkable Set to true to allow a check mark, false to disallow. The default is false. Returns
Control whether this item is shown with a check mark. Note that you must first have enabled checking with setCheckable(boolean) or else the check mark will not appear. If this item is a member of a group that contains mutually-exclusive items (set via setGroupCheckable(int, boolean, boolean), the other items in the group will be unchecked.
Parameters checked Set to true to display a check mark, false to hide it. The default value is false. Returns
Sets whether the menu item is enabled. Disabling a menu item will not allow it to be invoked via its shortcut. The menu item will still be visible.
Parameters enabled If true then the item will be invokable; if false it is won't be invokable. Returns
Change the icon associated with this item. This icon will not always be shown, so the title should be sufficient in describing this item. See Menu for the menu types that support icons.
Parameters icon The new icon (as a Drawable) to be displayed. Returns
Change the icon associated with this item. This icon will not always be shown, so the title should be sufficient in describing this item. See Menu for the menu types that support icons. This method will set the resource ID of the icon which will be used to lazily get the Drawable when this item is being shown.
Parameters iconRes The new icon (as a resource ID) to be displayed. Returns
Change the Intent associated with this item. By default there is no Intent associated with a menu item. If you set one, and nothing else handles the item, then the default behavior will be to call startActivity(Intent) with the given Intent. Note that setIntent() can not be used with the versions of add(int) that take a Runnable, because run() does not return a value so there is no way to tell if it handled the item. In this case it is assumed that the Runnable always handles the item, and the intent will never be started.
Parameters intent The Intent to associated with the item. This Intent object is not copied, so be careful not to modify it later. Returns
Set a custom listener for invocation of this menu item. In most situations, it is more efficient and easier to use onOptionsItemSelected(MenuItem) or onContextItemSelected(MenuItem).
Parameters menuItemClickListener The object to receive invokations.
Returns
Change both the numeric and alphabetic shortcut associated with this item. Note that the shortcut will be triggered when the key that generates the given character is pressed alone or along with with the alt key. Also note that case is not significant and that alphabetic shortcut characters will be displayed in lower case.
Parameters numericChar The numeric shortcut key. This is the shortcut when using a numeric (e.g., 12-key) keyboard. alphaChar The alphabetic shortcut key. This is the shortcut when using a keyboard with alphabetic keys. Returns
Change the title associated with this item. Some menu types do not sufficient space to show the full title, and instead a condensed title is preferred. See Menu for more information.
Parameters title The resource id of the new text to be displayed. Returns
Change the condensed title associated with this item. The condensed title is used in situations where the normal title may be too long to be displayed.
Parameters title The new text to be displayed as the condensed title. Returns
Sets the visibility of the menu item. Even if a menu item is not visible, it may still be invoked via its shortcut (to completely disable an item, set it to invisible and disabled).
Parameters visible If true then the item will be visible; if false it is hidden. Returns
ContextMenu
implements Menu android.view.ContextMenu
Class Overview
Extension of Menu for context menus providing functionality to modify the header of the context menu. Context menus do not support item shortcuts and item icons. To show a context menu on long click, most clients will want to call registerForContextMenu(View) and override onCreateContextMenu(ContextMenu, View, ContextMenu.ContextMenuInfo).
Summary
Nested Classes interface ContextMenu.ContextMenuInfo Additional information regarding the creation of the context menu. [Expand]
Inherited Constants From interface android.view.Menu int CATEGORY_ALTERNATIVE Category code for the order integer for items/groups that are alternative actions on the data that is currently displayed -- or/add this with your base value. Category code for the order integer for items/groups that are part of a container -- or/add this with your base value. Category code for the order integer for items/groups that are user-supplied secondary (infrequently used) options -- or/add this with your base value. Category code for the order integer for items/groups that are provided by the system -- or/add this with your base value. First value for group and item identifier integers. Flag for performShortcut(int, KeyEvent, int): if set, always close the menu after executing the shortcut. Flag for addIntentOptions(int, int, int, ComponentName, Intent[], Intent, int, MenuItem[]): if set, do not automatically remove any existing menu items in the same group. Flag for performShortcut(int, KeyEvent, int): if set, do not close the menu after executing the shortcut. Value to use for group and item identifier integers when you don't care about them. Public Methods clearHeader() abstract void Clears the header of the context menu. setHeaderIcon(Drawable icon) abstract ContextMenu Sets the context menu header's icon to the icon given in icon Drawable. setHeaderIcon(int iconRes) abstract ContextMenu Sets the context menu header's icon to the icon given in iconRes resource id. setHeaderTitle(int titleRes) abstract ContextMenu Sets the context menu header's title to the title given in titleRes resource identifier. A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
int CATEGORY_CONTAINER
int CATEGORY_SECONDARY
int FLAG_APPEND_TO_GROUP
int FLAG_PERFORM_NO_CLOSE
int NONE
setHeaderTitle(CharSequence title) abstract ContextMenu Sets the context menu header's title to the title given in title. setHeaderView(View view) abstract ContextMenu Sets the header of the context menu to the View given in view. [Expand] Inherited Methods From interface android.view.Menu abstract add(int titleRes) MenuItem Add a new item to the menu. abstract add(int groupId, int itemId, int order, CharSequence title) MenuItem Add a new item to the menu. add(int groupId, int itemId, int order, int titleRes) abstract MenuItem Variation on add(int, int, int, CharSequence) that takes a string resource identifier instead of the string itself. abstract add(CharSequence title) MenuItem Add a new item to the menu. addIntentOptions(int groupId, int itemId, int order, ComponentName caller, Intent[] specifics, Intent intent, int flags, MenuItem[] outSpecificItems) Add a group of menu items corresponding to actions that can be performed for a particular Intent. abstract addSubMenu(int titleRes) SubMenu Add a new sub-menu to the menu. addSubMenu(int groupId, int itemId, int order, int titleRes) abstract SubMenu Variation on addSubMenu(int, int, int, CharSequence) that takes a string resource identifier for the title instead of the string itself. abstract addSubMenu(CharSequence title) SubMenu Add a new sub-menu to the menu. abstract addSubMenu(int groupId, int itemId, int order, CharSequence title) A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
abstract int
SubMenu Add a new sub-menu to the menu. abstract void abstract void clear() Remove all existing items from the menu, leaving it empty as if it had just been created. close() Closes the menu, if open.
abstract findItem(int id) MenuItem Return the menu item with a particular identifier. abstract getItem(int index) MenuItem Gets the menu item at the given index. abstract boolean abstract boolean abstract boolean abstract boolean abstract void abstract void abstract void abstract void hasVisibleItems() Return whether the menu currently has item items that are visible. isShortcutKey(int keyCode, KeyEvent event) Is a keypress one of the defined shortcut keys for this window. performIdentifierAction(int id, int flags) Execute the menu item action associated with the given menu identifier. performShortcut(int keyCode, KeyEvent event, int flags) Execute the menu item action associated with the given shortcut character. removeGroup(int groupId) Remove all items in the given group. removeItem(int id) Remove the item with the given identifier. setGroupCheckable(int group, boolean checkable, boolean exclusive) Control whether a particular group of items can show a check mark. setGroupEnabled(int group, boolean enabled) Enable or disable all menu items that are in the given group.
abstract void
setGroupVisible(int group, boolean visible) Show or hide all menu items that are in the given group. setQwertyMode(boolean isQwerty)
abstract void
Control whether the menu should be running in qwerty mode (alphabetic shortcuts) or 12-key mode (numeric shortcuts). size() Get the number of items in the menu.
abstract int
Public Methods
public abstract void clearHeader ()
Sets the context menu header's icon to the icon given in icon Drawable.
Parameters icon The Drawable used for the icon. Returns
Sets the context menu header's icon to the icon given in iconRes resource id.
Parameters iconRes The resource identifier used for the icon. Returns
Sets the context menu header's title to the title given in titleRes resource identifier.
Parameters titleRes The string resource identifier used for the title. Returns
Sets the context menu header's title to the title given in title.
Parameters title The character sequence used for the title. Returns
Sets the header of the context menu to the View given in view. This replaces the header title and icon (and those replace this).
Parameters view The View used for the header. Returns
SubMenu
implements Menu android.view.SubMenu
Class Overview
Subclass of Menu for sub menus. Sub menus do not support item icons, or nested sub menus.
Summary
[Expand]
Inherited Constants From interface android.view.Menu int CATEGORY_ALTERNATIVE Category code for the order integer for items/groups that are alternative actions on the data that is currently displayed -- or/add this with your base value. Category code for the order integer for items/groups that are part of a container -- or/add this with your base value. Category code for the order integer for items/groups that are user-supplied secondary (infrequently used) options -- or/add this with your base value. Category code for the order integer for items/groups that are provided by the system -- or/add this with your base value. First value for group and item identifier integers. Flag for performShortcut(int, KeyEvent, int): if set, always close the menu after executing the shortcut. Flag for addIntentOptions(int, int, int, ComponentName, Intent[], Intent, int, MenuItem[]): if set, do not automatically remove any existing menu items in the same group. Flag for performShortcut(int, KeyEvent, int): if set, do not close the menu after executing the shortcut. Value to use for group and item identifier integers when you don't care about them. Public Methods abstract void clearHeader() Clears the header of the submenu.
int CATEGORY_CONTAINER
int CATEGORY_SECONDARY
int FLAG_APPEND_TO_GROUP
int FLAG_PERFORM_NO_CLOSE
int NONE
abstract getItem() MenuItem Gets the MenuItem that represents this submenu in the parent menu. abstract setHeaderIcon(Drawable icon) SubMenu Sets the submenu header's icon to the icon given in icon Drawable. abstract setHeaderIcon(int iconRes) SubMenu Sets the submenu header's icon to the icon given in iconRes resource id. A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
abstract setHeaderTitle(int titleRes) SubMenu Sets the submenu header's title to the title given in titleRes resource identifier. abstract setHeaderTitle(CharSequence title) SubMenu Sets the submenu header's title to the title given in title. abstract setHeaderView(View view) SubMenu Sets the header of the submenu to the View given in view. abstract setIcon(Drawable icon) SubMenu Change the icon associated with this submenu's item in its parent menu. abstract setIcon(int iconRes) SubMenu Change the icon associated with this submenu's item in its parent menu. [Expand] Inherited Methods From interface android.view.Menu abstract add(int titleRes) MenuItem Add a new item to the menu. abstract add(int groupId, int itemId, int order, CharSequence title) MenuItem Add a new item to the menu. add(int groupId, int itemId, int order, int titleRes) abstract MenuItem Variation on add(int, int, int, CharSequence) that takes a string resource identifier instead of the string itself. abstract add(CharSequence title) MenuItem Add a new item to the menu. addIntentOptions(int groupId, int itemId, int order, ComponentName caller, Intent[] specifics, Intent intent, int flags, MenuItem[] outSpecificItems) Add a group of menu items corresponding to actions that can be performed for a particular Intent. abstract addSubMenu(int titleRes) SubMenu A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
abstract int
Add a new sub-menu to the menu. addSubMenu(int groupId, int itemId, int order, int titleRes) abstract SubMenu Variation on addSubMenu(int, int, int, CharSequence) that takes a string resource identifier for the title instead of the string itself. abstract addSubMenu(CharSequence title) SubMenu Add a new sub-menu to the menu. abstract addSubMenu(int groupId, int itemId, int order, CharSequence title) SubMenu Add a new sub-menu to the menu. abstract void abstract void clear() Remove all existing items from the menu, leaving it empty as if it had just been created. close() Closes the menu, if open.
abstract findItem(int id) MenuItem Return the menu item with a particular identifier. abstract getItem(int index) MenuItem Gets the menu item at the given index. abstract boolean abstract boolean abstract boolean abstract boolean abstract void hasVisibleItems() Return whether the menu currently has item items that are visible. isShortcutKey(int keyCode, KeyEvent event) Is a keypress one of the defined shortcut keys for this window. performIdentifierAction(int id, int flags) Execute the menu item action associated with the given menu identifier. performShortcut(int keyCode, KeyEvent event, int flags) Execute the menu item action associated with the given shortcut character. removeGroup(int groupId) Remove all items in the given group.
removeItem(int id) Remove the item with the given identifier. setGroupCheckable(int group, boolean checkable, boolean exclusive) Control whether a particular group of items can show a check mark. setGroupEnabled(int group, boolean enabled) Enable or disable all menu items that are in the given group. setGroupVisible(int group, boolean visible) Show or hide all menu items that are in the given group. setQwertyMode(boolean isQwerty)
abstract void
Control whether the menu should be running in qwerty mode (alphabetic shortcuts) or 12-key mode (numeric shortcuts). size() Get the number of items in the menu.
abstract int
Public Methods
public abstract void clearHeader ()
Gets the MenuItem that represents this submenu in the parent menu. Use this for setting additional item attributes.
Returns
Sets the submenu header's icon to the icon given in icon Drawable.
Parameters icon The Drawable used for the icon.
Returns
Sets the submenu header's icon to the icon given in iconRes resource id.
Parameters iconRes The resource identifier used for the icon. Returns
Sets the submenu header's title to the title given in titleRes resource identifier.
Parameters titleRes The string resource identifier used for the title. Returns
Sets the header of the submenu to the View given in view. This replaces the header title and icon (and those replace this).
Parameters view The View used for the header.
Returns
Change the icon associated with this submenu's item in its parent menu.
Parameters icon The new icon (as a Drawable) to be displayed. Returns
See Also
setIcon(Drawable)
Change the icon associated with this submenu's item in its parent menu.
Parameters iconRes The new icon (as a resource ID) to be displayed. Returns
See Also
setIcon(int)
Menu Resource
1. Creating Menus A menu resource defines an application menu (Options Menu, Context Menu, or Sub Menu) that can be inflated with MenuInflater. file location:
res/menu/filename.xml
The filename will be used as the resource ID. compiled resource datatype:
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
Resource pointer to a Menu (or subclass) resource. resource reference: In Java: R.menu.filename In XML: @[package:]menu.filename syntax:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@[+][package:]id/resource_name" android:menuCategory=["container" | "system" | "secondary" | "alternative"] android:orderInCategory="integer" android:title="string" android:titleCondensed="string" android:icon="@[package:]drawable/drawable_resource_name" android:alphabeticShortcut="string" android:numericShortcut="string" android:checkable=["true" | "false"] android:visible=["visible" | "invisible" | "gone"] android:enabled=["enabled" | "disabled"] /> <group android:id="@[+][package:]id/resource name" android:menuCategory=["container" | "system" | "secondary" | "alternative"] android:orderInCategory="integer" android:checkableBehavior=["none" | "all" | "single"] android:visible=["visible" | "invisible" | "gone"] android:enabled=["enabled" | "disabled"] > <item /> </group> <item > <menu> <item /> </menu> </item> </menu>
elements:
<menu>
Required. This must be the root node. Contains <item> and/or <group> elements. attributes:
xmlns:android
A menu group (to create a collection of items that share traits, such as whether they are visible, enabled, or checkable). Contains one or more <item> elements. Must be a child of a <menu> element. attributes:
android:id
Resource ID. A unique resource ID. To create a new resource ID for this item, use the form: "@+id/name". The plus symbol indicates that this should be created as a new ID.
android:menuCategory
Keyword. Value corresponding to Menu CATEGORY_* constants, which define the group's priority. Valid values: Value Description
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
container
For groups that are part of a container. system For groups that are provided by the system. secondary For groups that are user-supplied secondary (infrequently used) options. alternative For groups that are alternative actions on the data that is currently displayed.
android:orderInCategory
Keyword. The type of checkable behavior for the group. Valid values: Value Description none Not checkable all All items can be checked (use checkboxes) single Only one item can be checked (use radio buttons)
android:visible
A menu item. May contain a <menu> element (for a Sub Menu). Must be a child of a <menu> or <group> element. attributes:
android:id
Resource ID. A unique resource ID. To create a new resource ID for this item, use the form: "@+id/name". The plus symbol indicates that this should be created as a new ID.
android:menuCategory
Keyword. Value corresponding to Menu CATEGORY_* constants, which define the item's priority. Valid values: Value Description 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 (infrequently used) options. alternative For items that are alternative actions on the data that is currently displayed.
android:orderInCategory
String. A condensed title, for situations in which the normal title is too long.
android:icon
Boolean. "true" if the item is enabled by default. example: XML file saved at res/menu/example_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/item1" android:title="@string/item1" android:icon="@drawable/group_item1_icon" /> <group android:id="@+id/group"> <item android:id="@+id/group_item1" android:title="@string/group_item1" android:icon="@drawable/group_item1_icon" /> <item android:id="@+id/group_item2" android:title="G@string/group_item2" android:icon="@drawable/group_item2_icon" /> </group> <item android:id="@+id/submenu" android:title="@string/submenu_title" > <menu> <item android:id="@+id/submenu_item1" android:title="@string/submenu_item1" /> </menu> </item> </menu>
This application code will inflate the menu from the onCreateOptionsMenu(Menu) callback:
public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.example_menu, menu); return true; }
See also
1. Style and Theme Resources 2. R.style for Android styles and themes 3. R.attr for all style attributes A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
A style is a collection of properties that specify the look and format for a View or window. A style can specify properties such as height, padding, font color, font size, background color, and much more. A style is defined in an XML resource that is separate from the XML that specifies the layout. Styles in Android share a similar philosophy to cascading stylesheets in web designthey allow you to separate the design from the content. For example, by using a style, you can take this layout XML:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#00FF00" android:typeface="monospace" android:text="@string/hello" />
All of the attributes related to style have been removed from the layout XML and put into a style definition called CodeFont, which is then applied with the style attribute. You'll see the definition for this style in the following section. A theme is a style applied to an entire Activity or application, rather than an individual View (as in the example above). When a style is applied as a theme, every View in the Activity or application will apply each style property that it supports. For example, you can apply the same CodeFont style as a theme for an Activity and then all text inside that Activity will have green monospace font.
Defining Styles
To create a set of styles, save an XML file in the res/values/ directory of your project. The name of the XML file is arbitrary, but it must use the .xml extension and be saved in the res/values/ folder. The root node of the XML file must be <resources>. For each style you want to create, add a <style> element to the file with a name that uniquely identifies the style (this attribute is required). Then add an <item> element for each property of that style, with a name that declares the style property and a value to go with it (this attribute is required). The value for the <item> can be a keyword string, a hex color, a reference to another resource type, or other value depending on the style property. Here's an example file with a single style:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="CodeFont" parent="@android:style/TextAppearance.Medium"> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:textColor">#00FF00</item> <item name="android:typeface">monospace</item> </style> </resources>
Each child of the <resources> element is converted into an application resource object at compile-time, which can be referenced by the value in the <style> element's name attribute. This example style can be referenced from an XML layout as @style/CodeFont (as demonstrated in the introduction above). The parent attribute in the <style> element is optional and specifies the resource ID of another style from which this style should inherit properties. You can then override the inherited style properties if you want to. Remember, a style that you want to use as an Activity or application theme is defined in XML exactly the same as a style for a View. A style such as the one defined above can be applied as a style for a single View or as a theme for an entire Activity or application. How to apply a style for a single View or as an application theme is discussed later.
Inheritance
The parent attribute in the <style> element lets you specify a style from which your style should inherit properties. You can use this to inherit properties from an existing style and then define only the properties that you want to change or add. You can inherit from styles that you've created yourself or from styles that are built into the platform. (See Using Platform Styles and Themes, below, for information about inheriting from styles defined by the Android platform.) For example, you can inherit the Android platform's default text appearance and then modify it:
<style name="GreenText" parent="@android:style/TextAppearance"> <item name="android:textColor">#00FF00</item> </style>
If you want to inherit from styles that you've defined yourself, you do not have to use the parent attribute. Instead, just prefix the name of the style you want to inherit to the name of your new style, separated by a period. For example, to create a new style that inherits the CodeFont style defined above, but make the color red, you can author the new style like this:
<style name="CodeFont.Red"> <item name="android:textColor">#FF0000</item> </style>
Notice that there is no parent attribute in the <style> tag, but because the name attribute begins with the CodeFont style name (which is a style that you have created), this style inherits all style properties from that style. This style then overrides the android:textColor property to make the text red. You can reference this new style as @style/CodeFont.Red. You can continue inheriting like this as many times as you'd like, by chaining names with periods. For example, you can extend CodeFont.Red to be bigger, with:
<style name="CodeFont.Red.Big"> <item name="android:textSize">30sp</item> </style>
This inherits from both CodeFont and CodeFont.Red styles, then adds the android:textSize property. Note: This technique for inheritance by chaining together names only works for styles defined by your own resources. You can't inherit Android built-in styles this way. To reference a built-in style, such as TextAppearance, you must use the parent attribute.
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
Style Properties
Now that you understand how a style is defined, you need to learn what kind of style propertiesdefined by the <item> elementare available. You're probably familiar with some already, such as layout_width and textColor. Of course, there are many more style properties you can use. The best place to find properties that apply to a specific View is the corresponding class reference, which lists all of the supported XML attributes. For example, all of the attributes listed in the table of TextView XML attributes can be used in a style definition for a TextView element (or one of its subclasses). One of the attributes listed in the reference is android:inputType, so where you might normally place the android:inputType attribute in an <EditText> element, like this:
<EditText android:inputType="number" ... />
You can instead create a style for the EditText element that includes this property:
<style name="Numbers"> <item name="android:inputType">number</item> ... </style>
So your XML for the layout can now implement this style:
<EditText style="@style/Numbers" ... />
This simple example may look like more work, but when you add more style properties and factor-in the ability to re-use the style in various places, the pay-off can be huge. For a reference of all available style properties, see the R.attr reference. Keep in mind that all View objects don't accept all the same style attributes, so you should normally refer to the specific View class for supported style properties. However, if you apply a style to a View that does not support all of the style properties, the View will apply only those properties that are supported and simply ignore the others. Some style properties, however, are not supported by any View element and can only be applied as a theme. These style properties apply to the entire window and not to any type of View. For example, style properties for a theme can hide the application title, hide the status bar, or change the window's background. These kind of style properties do not belong to any View object. To discover these theme-only style properties, look at the R.attr reference for attributes that begin with window. For instance, windowNoTitle and windowBackground are style properties that are effective only when the style is applied as a theme to an Activity or application. See the next section for information about applying a style as a theme. Note: Don't forget to prefix the property names in each <item> element with the android: namespace. For example: <item name="android:inputType">.
To an individual View, by adding the style attribute to a View element in the XML for your layout. Or, to an entire Activity or application, by adding the android:theme attribute to the <activity> or <application> element in the Android manifest.
When you apply a style to a single View in the layout, the properties defined by the style are applied only to that View. If a style is applied to a ViewGroup, the child View elements will not inherit the style properties only the element to which you directly apply the style will apply its properties. However, you can apply a style so that it applies to all View elementsby applying the style as a theme. To apply a style definition as a theme, you must apply the style to an Activity or application in the Android manifest. When you do so, every View within the Activity or application will apply each property that it supports. For example, if you apply the CodeFont style from the previous examples to an Activity, then all View elements that support the text style properties will apply them. Any View that does not support the properties will ignore them. If a View supports only some of the properties, then it will apply only those properties.
Now this TextView will be styled as defined by the style named CodeFont. (See the sample above, in Defining Styles.) Note: The style attribute does not use the android: namespace prefix.
If you want a theme applied to just one Activity in your application, then add the android:theme attribute to the <activity> tag instead. Just as Android provides other built-in resources, there are many pre-defined themes that you can use, to avoid writing them yourself. For example, you can use the Dialog theme and make your Activity appear like a dialog box:
<activity android:theme="@android:style/Theme.Dialog">
If you like a theme, but want to tweak it, just add the theme as the parent of your custom theme. For example, you can modify the traditional dialog theme to use your own background image like this:
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi
These files will help you learn through example. For instance, in the Android themes source code, you'll find a declaration for <style name="Theme.Dialog">. In this definition, you'll see all of the properties that are used to style dialogs that are used by the Android framework. For more information about the syntax used to create styles in XML, see Available Resource Types: Style and Themes. For a reference of available style attributes that you can use to define a style or theme (e.g., "windowBackground" or "textAppearance"), see R.attr or the respective View class for which you are creating a style.