Android APIs
public class

PopupMenu

extends Object
implements MenuBuilder.Callback MenuPresenter.Callback
java.lang.Object
   ↳ android.widget.PopupMenu

Class Overview

A PopupMenu displays a Menu in a modal popup window anchored to a View. The popup will appear below the anchor view if there is room, or above it if there is not. If the IME is visible the popup will not overlap it until it is touched. Touching outside of the popup will dismiss it.

Summary

Nested Classes
interface PopupMenu.OnDismissListener Callback interface used to notify the application that the menu has closed. 
interface PopupMenu.OnMenuItemClickListener Interface responsible for receiving menu item click events if the items themselves do not have individual item click listeners. 
Public Constructors
PopupMenu(Context context, View anchor)
Constructor to create a new popup menu with an anchor view.
PopupMenu(Context context, View anchor, int gravity)
Constructor to create a new popup menu with an anchor view and alignment gravity.
PopupMenu(Context context, View anchor, int gravity, int popupStyleAttr, int popupStyleRes)
Constructor a create a new popup menu with a specific style.
Public Methods
void dismiss()
Dismiss the menu popup.
View.OnTouchListener getDragToOpenListener()
Returns an View.OnTouchListener that can be added to the anchor view to implement drag-to-open behavior.
int getGravity()
Menu getMenu()
MenuInflater getMenuInflater()
void inflate(int menuRes)
Inflate a menu resource into this PopupMenu.
void setGravity(int gravity)
Sets the gravity used to align the popup window to its anchor view.
void setOnDismissListener(PopupMenu.OnDismissListener listener)
Set a listener that will be notified when this menu is dismissed.
void setOnMenuItemClickListener(PopupMenu.OnMenuItemClickListener listener)
Set a listener that will be notified when the user selects an item from the menu.
void show()
Show the menu popup anchored to the view specified during construction.
[Expand]
Inherited Methods
From class java.lang.Object
From interface com.android.internal.view.menu.MenuBuilder.Callback
From interface com.android.internal.view.menu.MenuPresenter.Callback

Public Constructors

public PopupMenu (Context context, View anchor)

Added in API level 11

Constructor to create a new popup menu with an anchor view.

Parameters
context Context: Context the popup menu is running in, through which it can access the current theme, resources, etc.
anchor View: Anchor view for this popup. The popup will appear below the anchor if there is room, or above it if there is not.

public PopupMenu (Context context, View anchor, int gravity)

Added in API level 19

Constructor to create a new popup menu with an anchor view and alignment gravity.

Parameters
context Context: Context the popup menu is running in, through which it can access the current theme, resources, etc.
anchor View: Anchor view for this popup. The popup will appear below the anchor if there is room, or above it if there is not.
gravity int: The Gravity value for aligning the popup with its anchor.

public PopupMenu (Context context, View anchor, int gravity, int popupStyleAttr, int popupStyleRes)

Added in API level 22

Constructor a create a new popup menu with a specific style.

Parameters
context Context: Context the popup menu is running in, through which it can access the current theme, resources, etc.
anchor View: Anchor view for this popup. The popup will appear below the anchor if there is room, or above it if there is not.
gravity int: The Gravity value for aligning the popup with its anchor.
popupStyleAttr int: An attribute in the current theme that contains a reference to a style resource that supplies default values for the popup window. Can be 0 to not look for defaults.
popupStyleRes int: A resource identifier of a style resource that supplies default values for the popup window, used only if popupStyleAttr is 0 or can not be found in the theme. Can be 0 to not look for defaults.

Public Methods

public void dismiss ()

Added in API level 11

Dismiss the menu popup.

See also:

public View.OnTouchListener getDragToOpenListener ()

Added in API level 19

Returns an View.OnTouchListener that can be added to the anchor view to implement drag-to-open behavior.

When the listener is set on a view, touching that view and dragging outside of its bounds will open the popup window. Lifting will select the currently touched list item.

Example usage:

 PopupMenu myPopup = new PopupMenu(context, myAnchor);
 myAnchor.setOnTouchListener(myPopup.getDragToOpenListener());
 

Returns
View.OnTouchListener a touch listener that controls drag-to-open behavior

public int getGravity ()

Added in API level 23

Returns
int the gravity used to align the popup window to its anchor view

See also:

public Menu getMenu ()

Added in API level 11

Returns
Menu the Menu associated with this popup. Populate the returned Menu with items before calling show().

public MenuInflater getMenuInflater ()

Added in API level 11

Returns
MenuInflater a MenuInflater that can be used to inflate menu items from XML into the menu returned by getMenu().

See also:

public void inflate (int menuRes)

Added in API level 14

Inflate a menu resource into this PopupMenu. This is equivalent to calling popupMenu.getMenuInflater().inflate(menuRes, popupMenu.getMenu()).

Parameters
menuRes int: Menu resource to inflate

public void setGravity (int gravity)

Added in API level 23

Sets the gravity used to align the popup window to its anchor view.

If the popup is showing, calling this method will take effect only the next time the popup is shown.

Parameters
gravity int: the gravity used to align the popup window

See also:

public void setOnDismissListener (PopupMenu.OnDismissListener listener)

Added in API level 14

Set a listener that will be notified when this menu is dismissed.

Parameters
listener PopupMenu.OnDismissListener: Listener to notify

public void setOnMenuItemClickListener (PopupMenu.OnMenuItemClickListener listener)

Added in API level 11

Set a listener that will be notified when the user selects an item from the menu.

Parameters
listener PopupMenu.OnMenuItemClickListener: Listener to notify

public void show ()

Added in API level 11

Show the menu popup anchored to the view specified during construction.

See also: