# PopupMenu

Artifact: [androidx.appcompat:appcompat](https://developer.android.com/jetpack/androidx/releases/appcompat) [View Source](https://cs.android.com/search?q=file:androidx/appcompat/widget/PopupMenu.java+class:androidx.appcompat.widget.PopupMenu) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

*** ** * ** ***


```
class PopupMenu
```

<br />

*** ** * ** ***

Static library support version of the framework's `https://developer.android.com/reference/android/widget/PopupMenu.html`. Used to write apps that run on platforms prior to Android 3.0. When running on Android 3.0 or above, this implementation is still used; it does not try to switch to the framework's implementation. See the framework SDK documentation for a class overview.

## Summary

| ### Nested types |
|---|
| `interface https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu.OnDismissListener` Callback interface used to notify the application that the menu has closed. |
| `interface https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu.OnMenuItemClickListener` Interface responsible for receiving menu item click events if the items themselves do not have individual item click listeners. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#PopupMenu(android.content.Context,android.view.View)(context: https://developer.android.com/reference/android/content/Context.html, anchor: https://developer.android.com/reference/android/view/View.html)` Constructor to create a new popup menu with an anchor view. |
| `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#PopupMenu(android.content.Context,android.view.View,int)(context: https://developer.android.com/reference/android/content/Context.html, anchor: https://developer.android.com/reference/android/view/View.html, gravity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Constructor to create a new popup menu with an anchor view and alignment gravity. |
| `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#PopupMenu(android.content.Context,android.view.View,int,int,int)( context: https://developer.android.com/reference/android/content/Context.html, anchor: https://developer.android.com/reference/android/view/View.html, gravity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, popupStyleAttr: @https://developer.android.com/reference/kotlin/androidx/annotation/AttrRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, popupStyleRes: @https://developer.android.com/reference/kotlin/androidx/annotation/StyleRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` Constructor a create a new popup menu with a specific style. |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#dismiss()()` Dismiss the menu popup. |
| `https://developer.android.com/reference/android/view/View.OnTouchListener.html` | `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#getDragToOpenListener()()` Returns an `https://developer.android.com/reference/android/view/View.OnTouchListener.html` that can be added to the anchor view to implement drag-to-open behavior. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#getGravity()()` |
| `https://developer.android.com/reference/android/view/Menu.html` | `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#getMenu()()` Returns the `https://developer.android.com/reference/android/view/Menu.html` associated with this popup. |
| `https://developer.android.com/reference/android/view/MenuInflater.html` | `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#getMenuInflater()()` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#inflate(int)(menuRes: @https://developer.android.com/reference/kotlin/androidx/annotation/MenuRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Inflate a menu resource into this PopupMenu. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#setForceShowIcon(boolean)(forceShowIcon: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` Sets whether the popup menu's adapter is forced to show icons in the menu item views. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#setGravity(int)(gravity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Sets the gravity used to align the popup window to its anchor view. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#setOnDismissListener(androidx.appcompat.widget.PopupMenu.OnDismissListener)(listener: https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu.OnDismissListener?)` Sets a listener that will be notified when this menu is dismissed. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#setOnMenuItemClickListener(androidx.appcompat.widget.PopupMenu.OnMenuItemClickListener)(listener: https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu.OnMenuItemClickListener?)` Sets a listener that will be notified when the user selects an item from the menu. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#show()()` Show the menu popup anchored to the view specified during construction. |

## Public constructors

### PopupMenu

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

```
PopupMenu(context: Context, anchor: View)
```

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

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | Context the popup menu is running in, through which it can access the current theme, resources, etc. |
| `anchor: https://developer.android.com/reference/android/view/View.html` | 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 [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

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

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

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | Context the popup menu is running in, through which it can access the current theme, resources, etc. |
| `anchor: https://developer.android.com/reference/android/view/View.html` | Anchor view for this popup. The popup will appear below the anchor if there is room, or above it if there is not. |
| `gravity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The `https://developer.android.com/reference/android/view/Gravity.html` value for aligning the popup with its anchor. |

### PopupMenu

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

```
PopupMenu(
    context: Context,
    anchor: View,
    gravity: Int,
    popupStyleAttr: @AttrRes Int,
    popupStyleRes: @StyleRes Int
)
```

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

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | Context the popup menu is running in, through which it can access the current theme, resources, etc. |
| `anchor: https://developer.android.com/reference/android/view/View.html` | Anchor view for this popup. The popup will appear below the anchor if there is room, or above it if there is not. |
| `gravity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The `https://developer.android.com/reference/android/view/Gravity.html` value for aligning the popup with its anchor. |
| `popupStyleAttr: @https://developer.android.com/reference/kotlin/androidx/annotation/AttrRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | 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: @https://developer.android.com/reference/kotlin/androidx/annotation/StyleRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | 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 functions

### dismiss

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

```
fun dismiss(): Unit
```

Dismiss the menu popup.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#show()` |   |

### getDragToOpenListener

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

```
fun getDragToOpenListener(): View.OnTouchListener
```

Returns an `https://developer.android.com/reference/android/view/View.OnTouchListener.html` 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:

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

| Returns |
|---|---|
| `https://developer.android.com/reference/android/view/View.OnTouchListener.html` | a touch listener that controls drag-to-open behavior |

### getGravity

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

```
fun getGravity(): Int
```

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | the gravity used to align the popup window to its anchor view |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#setGravity(int)` |   |

### getMenu

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

```
fun getMenu(): Menu
```

Returns the `https://developer.android.com/reference/android/view/Menu.html` associated with this popup. Populate the returned Menu with items before calling `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#show()`.

| Returns |
|---|---|
| `https://developer.android.com/reference/android/view/Menu.html` | the `https://developer.android.com/reference/android/view/Menu.html` associated with this popup |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#show()` |   |
| `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#getMenuInflater()` |   |

### getMenuInflater

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

```
fun getMenuInflater(): MenuInflater
```

| Returns |
|---|---|
| `https://developer.android.com/reference/android/view/MenuInflater.html` | a `https://developer.android.com/reference/android/view/MenuInflater.html` that can be used to inflate menu items from XML into the menu returned by `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#getMenu()` |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#getMenu()` |   |

### inflate

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

```
fun inflate(menuRes: @MenuRes Int): Unit
```

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

| Parameters |
|---|---|
| `menuRes: @https://developer.android.com/reference/kotlin/androidx/annotation/MenuRes https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Menu resource to inflate |

### setForceShowIcon

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.4.0)

```
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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `true` to force icons to be shown, or `false` for icons to be optionally shown |

### setGravity

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

```
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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | the gravity used to align the popup window |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#getGravity()` |   |

### setOnDismissListener

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

```
fun setOnDismissListener(listener: PopupMenu.OnDismissListener?): Unit
```

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

| Parameters |
|---|---|
| `listener: https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu.OnDismissListener?` | the listener to notify |

### setOnMenuItemClickListener

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

```
fun setOnMenuItemClickListener(listener: PopupMenu.OnMenuItemClickListener?): Unit
```

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

| Parameters |
|---|---|
| `listener: https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu.OnMenuItemClickListener?` | the listener to notify |

### show

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

```
fun show(): Unit
```

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

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/PopupMenu#dismiss()` |   |