Added in [API level 11](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Nested Classes](https://developer.android.com/reference/android/widget/PopupMenu#nestedclasses) \| [Ctors](https://developer.android.com/reference/android/widget/PopupMenu#pubctors) \| [Methods](https://developer.android.com/reference/android/widget/PopupMenu#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/widget/PopupMenu#inhmethods)

# PopupMenu

*** ** * ** ***

` public class PopupMenu `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.widget.PopupMenu |

<br />

*** ** * ** ***

A PopupMenu displays a `https://developer.android.com/reference/android/view/Menu` in a modal popup window anchored to a `https://developer.android.com/reference/android/view/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` | `https://developer.android.com/reference/android/widget/PopupMenu.OnDismissListener` Callback interface used to notify the application that the menu has closed. |
| ` interface` | `https://developer.android.com/reference/android/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/android/widget/PopupMenu#PopupMenu(android.content.Context,%20android.view.View)(https://developer.android.com/reference/android/content/Context context, https://developer.android.com/reference/android/view/View anchor) ` Constructor to create a new popup menu with an anchor view. |
| ` https://developer.android.com/reference/android/widget/PopupMenu#PopupMenu(android.content.Context,%20android.view.View,%20int)(https://developer.android.com/reference/android/content/Context context, https://developer.android.com/reference/android/view/View anchor, int gravity) ` Constructor to create a new popup menu with an anchor view and alignment gravity. |
| ` https://developer.android.com/reference/android/widget/PopupMenu#PopupMenu(android.content.Context,%20android.view.View,%20int,%20int,%20int)(https://developer.android.com/reference/android/content/Context context, https://developer.android.com/reference/android/view/View anchor, int gravity, int popupStyleAttr, int popupStyleRes) ` Constructor a create a new popup menu with a specific style. |

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

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Public constructors

### PopupMenu

Added in [API level 11](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

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

<br />

| Parameters ||
|---|---|
| `context` | `Context`: Context the popup menu is running in, through which it can access the current theme, resources, etc. <br /> |
| `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. <br /> |

### PopupMenu

Added in [API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

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

<br />

| Parameters ||
|---|---|
| `context` | `Context`: Context the popup menu is running in, through which it can access the current theme, resources, etc. <br /> |
| `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. <br /> |
| `gravity` | `int`: The `https://developer.android.com/reference/android/view/Gravity` value for aligning the popup with its anchor. <br /> |

### PopupMenu

Added in [API level 22](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

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

<br />

| Parameters ||
|---|---|
| `context` | `Context`: Context the popup menu is running in, through which it can access the current theme, resources, etc. <br /> |
| `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. <br /> |
| `gravity` | `int`: The `https://developer.android.com/reference/android/view/Gravity` value for aligning the popup with its anchor. <br /> |
| `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. <br /> |
| `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. <br /> |

## Public methods

### dismiss

Added in [API level 11](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void dismiss ()
```

Dismiss the menu popup.

<br />

**See also:**

- `https://developer.android.com/reference/android/widget/PopupMenu#show()`

### getDragToOpenListener

Added in [API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public View.OnTouchListener getDragToOpenListener ()
```

Returns an `https://developer.android.com/reference/android/view/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());
```

<br />

<br />

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

### getGravity

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getGravity ()
```

<br />

<br />

| Returns ||
|---|---|
| `int` | the gravity used to align the popup window to its anchor view <br /> |

**See also:**

- `https://developer.android.com/reference/android/widget/PopupMenu#setGravity(int)`

### getMenu

Added in [API level 11](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Menu getMenu ()
```

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

<br />

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

**See also:**

- `https://developer.android.com/reference/android/widget/PopupMenu#show()`
- `https://developer.android.com/reference/android/widget/PopupMenu#getMenuInflater()`

### getMenuInflater

Added in [API level 11](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public MenuInflater getMenuInflater ()
```

<br />

<br />

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

**See also:**

- `https://developer.android.com/reference/android/widget/PopupMenu#getMenu()`

### inflate

Added in [API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void inflate (int menuRes)
```

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

<br />

| Parameters ||
|---|---|
| `menuRes` | `int`: Menu resource to inflate <br /> |

### setForceShowIcon

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setForceShowIcon (boolean forceShowIcon)
```

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().

<br />

| Parameters ||
|---|---|
| `forceShowIcon` | `boolean`: `true` to force icons to be shown, or `false` for icons to be optionally shown <br /> |

### setGravity

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setGravity (int gravity)
```

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.

<br />

| Parameters ||
|---|---|
| `gravity` | `int`: the gravity used to align the popup window <br /> |

**See also:**

- `https://developer.android.com/reference/android/widget/PopupMenu#getGravity()`

### setOnDismissListener

Added in [API level 14](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setOnDismissListener (PopupMenu.OnDismissListener listener)
```

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

<br />

| Parameters ||
|---|---|
| `listener` | `PopupMenu.OnDismissListener`: the listener to notify <br /> |

### setOnMenuItemClickListener

Added in [API level 11](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setOnMenuItemClickListener (PopupMenu.OnMenuItemClickListener listener)
```

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

<br />

| Parameters ||
|---|---|
| `listener` | `PopupMenu.OnMenuItemClickListener`: the listener to notify <br /> |

### show

Added in [API level 11](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void show ()
```

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

<br />

**See also:**

- `https://developer.android.com/reference/android/widget/PopupMenu#dismiss()`