Skip to content

Most visited

Recently visited

navigation

MenuItemCompat

public final class MenuItemCompat
extends Object

java.lang.Object
   ↳ android.support.v4.view.MenuItemCompat


Helper for accessing features in MenuItem introduced after API level 4 in a backwards compatible fashion.

Note: You cannot get an instance of this class. Instead, it provides static methods that correspond to the methods in MenuItem, but take a MenuItem object as an additional argument.

Summary

Nested classes

interface MenuItemCompat.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. 

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

static boolean collapseActionView(MenuItem item)

Collapse the action view associated with this menu item.

static boolean expandActionView(MenuItem item)

Expand the action view associated with this menu item.

static ActionProvider getActionProvider(MenuItem item)

Gets the ActionProvider.

static View getActionView(MenuItem item)

Returns the currently set action view for this menu item.

static boolean isActionViewExpanded(MenuItem item)

Returns true if this menu item's action view has been expanded.

static MenuItem setActionProvider(MenuItem item, ActionProvider provider)

Sets the ActionProvider responsible for creating an action view if the item is placed on the action bar.

static MenuItem setActionView(MenuItem item, int resId)

Set an action view for this menu item.

static MenuItem setActionView(MenuItem item, View view)

Set an action view for this menu item.

static MenuItem setOnActionExpandListener(MenuItem item, MenuItemCompat.OnActionExpandListener listener)

Set an MenuItemCompat.OnActionExpandListener on this menu item to be notified when the associated action view is expanded or collapsed.

static void setShowAsAction(MenuItem item, int actionEnum)

Sets how this item should display in the presence of a compatible Action Bar.

Inherited methods

From class java.lang.Object

Constants

SHOW_AS_ACTION_ALWAYS

int SHOW_AS_ACTION_ALWAYS

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.

Constant Value: 2 (0x00000002)

SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW

int SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW

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.

Constant Value: 8 (0x00000008)

SHOW_AS_ACTION_IF_ROOM

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.

Constant Value: 1 (0x00000001)

SHOW_AS_ACTION_NEVER

int SHOW_AS_ACTION_NEVER

Never show this item as a button in an Action Bar.

Constant Value: 0 (0x00000000)

SHOW_AS_ACTION_WITH_TEXT

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.

Constant Value: 4 (0x00000004)

Public methods

collapseActionView

boolean collapseActionView (MenuItem item)

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(MenuItem, android.support.v4.view.MenuItemCompat.OnActionExpandListener) it will have its onMenuItemActionCollapse(MenuItem) method invoked. The listener may return false from this method to prevent collapsing the action view.

Parameters
item MenuItem
Returns
boolean true if the action view was collapsed, false otherwise.

expandActionView

boolean expandActionView (MenuItem item)

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(MenuItem, OnActionExpandListener) it will have its onMenuItemActionExpand(MenuItem) method invoked. The listener may return false from this method to prevent expanding the action view.

Parameters
item MenuItem
Returns
boolean true if the action view was expanded, false otherwise.

getActionProvider

ActionProvider getActionProvider (MenuItem item)

Gets the ActionProvider.

Parameters
item MenuItem
Returns
ActionProvider The action provider.

See also:

getActionView

View getActionView (MenuItem item)

Returns the currently set action view for this menu item.

Parameters
item MenuItem: the item to query
Returns
View This item's action view

isActionViewExpanded

boolean isActionViewExpanded (MenuItem item)

Returns true if this menu item's action view has been expanded.

Parameters
item MenuItem
Returns
boolean true if the item's action view is expanded, false otherwise.

See also:

setActionProvider

MenuItem setActionProvider (MenuItem item, 
                ActionProvider provider)

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(MenuItem, View).

Parameters
item MenuItem: item to change
provider ActionProvider: The action provider.
Returns
MenuItem This Item so additional setters can be called.

See also:

setActionView

MenuItem setActionView (MenuItem item, 
                int resId)

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(MenuItem, ActionProvider).

Parameters
item MenuItem: the item to change
resId int: Layout resource to use for presenting this item to the user.
Returns
MenuItem This Item so additional setters can be called.

See also:

setActionView

MenuItem setActionView (MenuItem item, 
                View view)

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.

Parameters
item MenuItem: the item to change
view View: View to use for presenting this item to the user.
Returns
MenuItem This Item so additional setters can be called.

See also:

setOnActionExpandListener

MenuItem setOnActionExpandListener (MenuItem item, 
                MenuItemCompat.OnActionExpandListener listener)

Set an MenuItemCompat.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.

Parameters
item MenuItem
listener MenuItemCompat.OnActionExpandListener: Listener that will respond to expand/collapse events
Returns
MenuItem This menu item instance for call chaining

setShowAsAction

void setShowAsAction (MenuItem item, 
                int actionEnum)

Sets how this item should display in the presence of a compatible Action Bar. If the given item is compatible, this will call the item's supported implementation of setShowAsAction(int).

Parameters
item MenuItem: - the item to change
actionEnum int: - How the item should display.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.