Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)
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)
Construct a new PopupMenu.
PopupMenu ( Context context, View anchor, int gravity)
Construct a new PopupMenu.
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.
Menu getMenu ()
MenuInflater getMenuInflater ()
void inflate (int menuRes)
Inflate a menu resource into this PopupMenu.
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)

Construct a new PopupMenu.

Parameters
context Context for the PopupMenu.
anchor 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)

Construct a new PopupMenu.

Parameters
context Context for the PopupMenu.
anchor Anchor view for this popup. The popup will appear below the anchor if there is room, or above it if there is not.
gravity The Gravity value for aligning the popup with its anchor

Public Methods

public void dismiss ()

Dismiss the menu popup.

See Also

public View.OnTouchListener getDragToOpenListener ()

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
  • a touch listener that controls drag-to-open behavior

public Menu getMenu ()

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

public MenuInflater getMenuInflater ()

Returns
See Also

public void inflate (int menuRes)

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

Parameters
menuRes Menu resource to inflate

public void setOnDismissListener ( PopupMenu.OnDismissListener listener)

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

Parameters
listener Listener to notify

public void setOnMenuItemClickListener ( PopupMenu.OnMenuItemClickListener listener)

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

Parameters
listener Listener to notify

public void show ()

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

See Also