Added in API level 11

PopupMenu

open class PopupMenu
kotlin.Any
   ↳ android.widget.PopupMenu

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
abstract

Callback interface used to notify the application that the menu has closed.

abstract

Interface responsible for receiving menu item click events if the items themselves do not have individual item click listeners.

Public constructors
PopupMenu(context: Context!, anchor: View!)

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

PopupMenu(context: Context!, anchor: View!, gravity: Int)

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

PopupMenu(context: Context!, anchor: View!, gravity: Int, popupStyleAttr: Int, popupStyleRes: Int)

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

Public methods
open Unit

Dismiss the menu popup.

open View.OnTouchListener!

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

open Int

open Menu!

Returns the Menu associated with this popup.

open MenuInflater!

open Unit
inflate(menuRes: Int)

Inflate a menu resource into this PopupMenu.

open Unit
setForceShowIcon(forceShowIcon: Boolean)

Sets whether the popup menu's adapter is forced to show icons in the menu item views.

open Unit
setGravity(gravity: Int)

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

open Unit

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

open Unit

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

open Unit

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

Public constructors

PopupMenu

Added in API level 11
PopupMenu(
    context: Context!,
    anchor: View!)

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.

PopupMenu

Added in API level 11
PopupMenu(
    context: Context!,
    anchor: View!,
    gravity: Int)

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.

PopupMenu

Added in API level 11
PopupMenu(
    context: Context!,
    anchor: View!,
    gravity: Int,
    popupStyleAttr: Int,
    popupStyleRes: Int)

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

dismiss

Added in API level 11
open fun dismiss(): Unit

Dismiss the menu popup.

See Also

getDragToOpenListener

Added in API level 19
open fun getDragToOpenListener(): View.OnTouchListener!

Returns an 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());
  
Return
View.OnTouchListener! a touch listener that controls drag-to-open behavior

getGravity

Added in API level 23
open fun getGravity(): Int
Return
Int the gravity used to align the popup window to its anchor view

See Also

getMenu

Added in API level 11
open fun getMenu(): Menu!

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

Return
Menu! the Menu associated with this popup

getMenuInflater

Added in API level 11
open fun getMenuInflater(): MenuInflater!
Return
MenuInflater! a MenuInflater that can be used to inflate menu items from XML into the menu returned by getMenu()

See Also

inflate

Added in API level 14
open fun inflate(menuRes: Int): Unit

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

setForceShowIcon

Added in API level 29
open fun setForceShowIcon(forceShowIcon: Boolean): Unit

Sets whether the popup menu's adapter is forced to show icons in the menu item views.

Changes take effect on the next call to show().

Parameters
forceShowIcon Boolean: true to force icons to be shown, or false for icons to be optionally shown

setGravity

Added in API level 23
open fun setGravity(gravity: Int): Unit

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

setOnDismissListener

Added in API level 14
open fun setOnDismissListener(listener: PopupMenu.OnDismissListener!): Unit

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

Parameters
listener PopupMenu.OnDismissListener!: the listener to notify

setOnMenuItemClickListener

Added in API level 11
open fun setOnMenuItemClickListener(listener: PopupMenu.OnMenuItemClickListener!): Unit

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

Parameters
listener PopupMenu.OnMenuItemClickListener!: the listener to notify

show

Added in API level 11
open fun show(): Unit

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

See Also