Overview
A Popup Menu in Android provides a transient floating menu that displays a list of actions related to a specific UI element. It appears anchored to a view and is commonly used for contextual actions on lists, buttons, or custom views. Popup menus are lightweight alternatives to full menus or dialog lists and improve user experience by showing relevant options on demand.
This document explains how to create, configure, and handle popup menus in Android using modern APIs and best practices.
Popup Menu Basics
The PopupMenu class in Android allows you to:
- Inflate a menu layout from XML
- Anchor the menu to any view
- Handle item click events with clean callbacks
Popup menus are available through AndroidX and support backward compatibility.
Defining Popup Menu XML
Create a menu resource file to define popup menu items. For example:
res/menu/popup_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_edit"
android:title="Edit" />
<item
android:id="@+id/action_delete"
android:title="Delete" />
<item
android:id="@+id/action_share"
android:title="Share" />
</menu>
Each <item> defines a selectable action with a title and ID.
Inflating and Showing a Popup Menu
To display the popup menu from your activity or fragment:
Button button = findViewById(R.id.popupButton);
button.setOnClickListener(view -> {
PopupMenu popupMenu = new PopupMenu(this, view);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(item -> {
switch (item.getItemId()) {
case R.id.action_edit:
// Perform edit action
return true;
case R.id.action_delete:
// Perform delete action
return true;
case R.id.action_share:
// Perform share action
return true;
default:
return false;
}
});
popupMenu.show();
});
This code:
- Binds the popup to a
Buttonor any view - Inflates the menu XML
- Handles item selection through
setOnMenuItemClickListener
Handling Popup Menu Item Selection
When an item is selected, the callback provides the selected MenuItem. In the example above:
popupMenu.setOnMenuItemClickListener(item -> {
// Handle selected actions
});
Use item.getItemId() to determine which action was selected. This allows you to define specific behavior (e.g., edit, delete, share) for each option.
Showing Icons in Popup Menus (Optional)
Officially, PopupMenu does not support icons in menu items by default. However, you can apply reflection or use custom views if icon display is required.
Example (Reflection approach):
try {
Field field = popupMenu.getClass().getDeclaredField("mPopup");
field.setAccessible(true);
Object menuPopupHelper = field.get(popupMenu);
Method setForceIcons = menuPopupHelper.getClass()
.getDeclaredMethod("setForceShowIcon", boolean.class);
setForceIcons.invoke(menuPopupHelper, true);
} catch (Exception e) {
e.printStackTrace();
}
Use this with caution, as reflection may break on future platform changes. For icons, consider using Context Menu or custom dialog instead.
Styling Popup Menus
Popup menus respect the app’s theme by default. You can customize colors, text appearance, and item backgrounds via style resources:
<style name="AppPopupMenuStyle" parent="Widget.AppCompat.PopupMenu.Overflow">
<item name="android:textColor">@color/popup_text_color</item>
<item name="android:popupBackground">@drawable/popup_background</item>
</style>
Then, apply the style in your app theme:
<item name="popupMenuStyle">@style/AppPopupMenuStyle</item>
This ensures consistent styling across the application.
Use Cases for Popup Menus
Popup menus are appropriate when:
- You want to show contextual actions (e.g., item actions in a list)
- You need lightweight option lists without full screen overlays
- Actions are specific to a particular UI element
Common examples:
- Overflow options next to list items
- Secondary actions on item long-press
- Quick actions near a button or toolbar element
Performance and UX Considerations
- Keep popup menus simple and focused — avoid too many options.
- Avoid deep nested menus; use dialogs if multiple layers are needed.
- Ensure touch targets are large enough for accurate selection.
- Use consistent naming across menu items to reduce cognitive load.


