android.view.MenuItem |
Interface for direct access to a previously created menu item.
An Item is returned by calling one of the
add(int)
methods.
For a feature set of specific menu types, see
Menu
.
For information about creating menus, read the Menus developer guide.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
MenuItem.OnActionExpandListener |
Interface definition for a callback to be invoked when a menu item
marked with
SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
is
expanded or collapsed.
|
|||||||||
|
MenuItem.OnMenuItemClickListener | Interface definition for a callback to be invoked when a menu item is clicked. |
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | SHOW_AS_ACTION_ALWAYS | Always show this item as a button in an Action Bar. | |||||||||
int | SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | This item's action view collapses to a normal menu item. | |||||||||
int | SHOW_AS_ACTION_IF_ROOM | Show this item as a button in an Action Bar if the system decides there is room for it. | |||||||||
int | SHOW_AS_ACTION_NEVER | Never show this item as a button in an Action Bar. | |||||||||
int | SHOW_AS_ACTION_WITH_TEXT | When this item is in the action bar, always show it with a text label even if it also has an icon specified. |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
Collapse the action view associated with this menu item.
|
||||||||||
|
Expand the action view associated with this menu item.
|
||||||||||
|
Gets the
ActionProvider
.
|
||||||||||
|
Returns the currently set action view for this menu item.
|
||||||||||
|
Return the char for this menu item's alphabetic shortcut.
|
||||||||||
|
Return the group identifier that this menu item is part of.
|
||||||||||
|
Returns the icon for this item as a Drawable (getting it from resources if it hasn't been
loaded before).
|
||||||||||
|
Return the Intent associated with this item.
|
||||||||||
|
Return the identifier for this menu item.
|
||||||||||
|
Gets the extra information linked to this menu item.
|
||||||||||
|
Return the char for this menu item's numeric (12-key) shortcut.
|
||||||||||
|
Return the category and order within the category of this item.
|
||||||||||
|
Get the sub-menu to be invoked when this item is selected, if it has
one.
|
||||||||||
|
Retrieve the current title of the item.
|
||||||||||
|
Retrieve the current condensed title of the item.
|
||||||||||
|
Check whether this item has an associated sub-menu.
|
||||||||||
|
Returns true if this menu item's action view has been expanded.
|
||||||||||
|
Return whether the item can currently display a check mark.
|
||||||||||
|
Return whether the item is currently displaying a check mark.
|
||||||||||
|
Return the enabled state of the menu item.
|
||||||||||
|
Return the visibility of the menu item.
|
||||||||||
|
Sets the
ActionProvider
responsible for creating an action view if
the item is placed on the action bar.
|
||||||||||
|
Set an action view for this menu item.
|
||||||||||
|
Set an action view for this menu item.
|
||||||||||
|
Change the alphabetic shortcut associated with this item.
|
||||||||||
|
Control whether this item can display a check mark.
|
||||||||||
|
Control whether this item is shown with a check mark.
|
||||||||||
|
Sets whether the menu item is enabled.
|
||||||||||
|
Change the icon associated with this item.
|
||||||||||
|
Change the icon associated with this item.
|
||||||||||
|
Change the Intent associated with this item.
|
||||||||||
|
Change the numeric shortcut associated with this item.
|
||||||||||
|
Set an
MenuItem.OnActionExpandListener
on this menu item to be notified when
the associated action view is expanded or collapsed.
|
||||||||||
|
Set a custom listener for invocation of this menu item.
|
||||||||||
|
Change both the numeric and alphabetic shortcut associated with this
item.
|
||||||||||
|
Sets how this item should display in the presence of an Action Bar.
|
||||||||||
|
Sets how this item should display in the presence of an Action Bar.
|
||||||||||
|
Change the title associated with this item.
|
||||||||||
|
Change the title associated with this item.
|
||||||||||
|
Change the condensed title associated with this item.
|
||||||||||
|
Sets the visibility of the menu item.
|
Always show this item as a button in an Action Bar. Use sparingly! If too many items are set to always show in the Action Bar it can crowd the Action Bar and degrade the user experience on devices with smaller screens. A good rule of thumb is to have no more than 2 items set to always show at a time.
This item's action view collapses to a normal menu item. When expanded, the action view temporarily takes over a larger segment of its container.
Show this item as a button in an Action Bar if the system decides there is room for it.
Never show this item as a button in an Action Bar.
When this item is in the action bar, always show it with a text label even if it also has an icon specified.
Collapse the action view associated with this menu item.
The menu item must have an action view set, as well as the showAsAction flag
SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
. If a listener has been set using
setOnActionExpandListener(OnActionExpandListener)
it will have its
onMenuItemActionCollapse(MenuItem)
method invoked.
The listener may return false from this method to prevent collapsing the action view.
Expand the action view associated with this menu item.
The menu item must have an action view set, as well as
the showAsAction flag
SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
.
If a listener has been set using
setOnActionExpandListener(OnActionExpandListener)
it will have its
onMenuItemActionExpand(MenuItem)
method invoked. The listener may return false from this method to prevent expanding
the action view.
Gets the
ActionProvider
.
Returns the currently set action view for this menu item.
Return the char for this menu item's alphabetic shortcut.
Return the group identifier that this menu item is part of. The group identifier can not be changed after the menu is created.
Returns the icon for this item as a Drawable (getting it from resources if it hasn't been loaded before).
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.
setIntent(Intent)
, or
null.
Return the identifier for this menu item. The identifier can not be changed after the menu is created.
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.
Return the char for this menu item's numeric (12-key) shortcut.
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.
Get the sub-menu to be invoked when this item is selected, if it has
one. See
hasSubMenu()
.
Retrieve the current title of the item.
Retrieve the current condensed title of the item. If a condensed title was never set, it will return the normal title.
Check whether this item has an associated sub-menu. I.e. it is a sub-menu of another menu.
Returns true if this menu item's action view has been expanded.
Return whether the item can currently display a check mark.
Return whether the item is currently displaying a check mark.
Return the enabled state of the menu item.
Return the visibility of the menu item.
Sets the
ActionProvider
responsible for creating an action view if
the item is placed on the action bar. The provider also provides a default
action invoked if the item is placed in the overflow menu.
Note:
Setting an action provider overrides the action view
set via
setActionView(int)
or
setActionView(View)
.
actionProvider | The action provider. |
---|
Set an action view for this menu item. An action view will be displayed in place of an automatically generated menu item element in the UI when this item is shown as an action within a parent.
Note:
Setting an action view overrides the action provider
set via
setActionProvider(ActionProvider)
.
resId | Layout resource to use for presenting this item to the user. |
---|
Set an action view for this menu item. An action view will be displayed in place of an automatically generated menu item element in the UI when this item is shown as an action within a parent.
Note:
Setting an action view overrides the action provider
set via
setActionProvider(ActionProvider)
.
view | View to use for presenting this item to the user. |
---|
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.
alphaChar | The alphabetic shortcut key. This is the shortcut when using a keyboard with alphabetic keys. |
---|
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.
checkable | Set to true to allow a check mark, false to disallow. The default is false. |
---|
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.
See
Menu
for the menu types that support check marks.
checked | Set to true to display a check mark, false to hide it. The default value is false. |
---|
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.
enabled | If true then the item will be invokable; if false it is won't be invokable. |
---|
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.
icon | The new icon (as a Drawable) to be displayed. |
---|
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.
iconRes | The new icon (as a resource ID) to be displayed. |
---|
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.
intent | The Intent to associated with the item. This Intent object is not copied, so be careful not to modify it later. |
---|
Change the numeric shortcut associated with this item.
See
Menu
for the menu types that support shortcuts.
numericChar | The numeric shortcut key. This is the shortcut when using a 12-key (numeric) keyboard. |
---|
Set an
MenuItem.OnActionExpandListener
on this menu item to be notified when
the associated action view is expanded or collapsed. The menu item must
be configured to expand or collapse its action view using the flag
SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
.
listener | Listener that will respond to expand/collapse events |
---|
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)
.
menuItemClickListener | The object to receive invokations. |
---|
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.
See
Menu
for the menu types that support shortcuts.
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. |
Sets how this item should display in the presence of an Action Bar.
The parameter actionEnum is a flag set. One of
SHOW_AS_ACTION_ALWAYS
,
SHOW_AS_ACTION_IF_ROOM
, or
SHOW_AS_ACTION_NEVER
should
be used, and you may optionally OR the value with
SHOW_AS_ACTION_WITH_TEXT
.
SHOW_AS_ACTION_WITH_TEXT requests that when the item is shown as an action,
it should be shown with a text label.
actionEnum |
How the item should display. One of
SHOW_AS_ACTION_ALWAYS
,
SHOW_AS_ACTION_IF_ROOM
, or
SHOW_AS_ACTION_NEVER
. SHOW_AS_ACTION_NEVER is the default.
|
---|
Sets how this item should display in the presence of an Action Bar.
The parameter actionEnum is a flag set. One of
SHOW_AS_ACTION_ALWAYS
,
SHOW_AS_ACTION_IF_ROOM
, or
SHOW_AS_ACTION_NEVER
should
be used, and you may optionally OR the value with
SHOW_AS_ACTION_WITH_TEXT
.
SHOW_AS_ACTION_WITH_TEXT requests that when the item is shown as an action,
it should be shown with a text label.
Note: This method differs from
setShowAsAction(int)
only in that it
returns the current MenuItem instance for call chaining.
actionEnum |
How the item should display. One of
SHOW_AS_ACTION_ALWAYS
,
SHOW_AS_ACTION_IF_ROOM
, or
SHOW_AS_ACTION_NEVER
. SHOW_AS_ACTION_NEVER is the default.
|
---|
Change the title associated with this item.
title | The new text to be displayed. |
---|
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.
title | The resource id of the new text to be displayed. |
---|
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.
title | The new text to be displayed as the condensed title. |
---|
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
).
visible | If true then the item will be visible; if false it is hidden. |
---|