Menus are fundamental UI components in Android applications. They help users navigate features, execute actions, and access contextual options. Android provides several types of menus:
- Options Menu – Appears in the app bar (Toolbar)
- Context Menu – Appears on long-press of a View
- Popup Menu – Appears anchored to a specific UI element
This guide explains each menu type with clear examples and best practices for production-ready Android apps.
What Is an Android Menu?
An Android menu presents a set of action items or options. Menus improve usability by offering contextual or global operations without cluttering the interface.
Options Menu (App Bar / Toolbar)
The Options Menu is typically shown in the app bar, either as text or icon items. With AppCompat and Toolbar, defining menu items is straightforward.
Step 1 — Define Menu XML
Create a menu resource file under res/menu/ (e.g., main_menu.xml):
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_search"
android:title="Search"
android:icon="@drawable/ic_search"
android:showAsAction="ifRoom"/>
<item
android:id="@+id/action_settings"
android:title="Settings"
android:showAsAction="never"/>
</menu>
showAsAction="ifRoom"attempts to display the item in the app barandroid:icondefines an icon for visual context
Step 2 — Inflate Menu in Activity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
Step 3 — Handle Menu Selections
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
// Perform search action
return true;
case R.id.action_settings:
// Navigate to settings
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This pattern displays the menu in the app bar and handles user taps.
Context Menu (Long-Press Actions)
Context menus provide actions related to a specific view (e.g., list item).
Step 1 — Register View for Context Menu
TextView textView = findViewById(R.id.textView);
registerForContextMenu(textView);
Step 2 — Define Context Menu
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.context_menu, menu);
}
Step 3 — Handle Context Item Selection
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.edit:
// Edit action
return true;
case R.id.delete:
// Delete action
return true;
default:
return super.onContextItemSelected(item);
}
}
This provides a long-press menu for contextual actions.
Popup Menu (Anchored to View)
Popup menus show a small menu anchored near a specific View (e.g., a button). Use this for inline option selections.
Example: Show Popup on Button Click
Button btnOptions = findViewById(R.id.btnOptions);
btnOptions.setOnClickListener(v -> {
PopupMenu popup = new PopupMenu(this, v);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(item -> {
switch(item.getItemId()) {
case R.id.share:
// Handle share
return true;
case R.id.delete:
// Handle delete
return true;
default:
return false;
}
});
popup.show();
});
Popup menus are lightweight and context-specific.
Using AndroidX and Toolbars
Modern Android apps use Toolbar from Material or AppCompat as the app bar.
Setup Toolbar
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Then proceed with options menu inflation as shown earlier. This centralizes menu handling in a single app bar.
Dynamic Menu Items
You can modify menu items at runtime:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem searchItem = menu.findItem(R.id.action_search);
searchItem.setVisible(isSearchEnabled);
return super.onPrepareOptionsMenu(menu);
}
Call invalidateOptionsMenu() to refresh the menu.
Icons and Accessibility
- Always provide icons with meaningful titles
- Use
contentDescriptionfor accessibility - Use vector icons (
VectorDrawable) to minimize app size
Example:
android:contentDescription="@string/search_desc"
This improves screen reader support.
Best Practices (Senior Engineering Insight)
From real production experience:
- Use Toolbar + AppCompat for consistent menu support across devices
- Keep global actions in the app bar and contextual actions in context menus
- Avoid too many items in an options menu — group using
groupIdor submenus - Use popup menus for inline, transient actions
- Use IDs and constants for menu handling to avoid hardcoded strings
These practices improve usability and maintainability.
Common Issues and Fixes
Menu doesn’t appear:
✔ Ensure onCreateOptionsMenu() returns true
✔ Check that the menu resource file exists under res/menu
Click events not triggering:
✔ Ensure onOptionsItemSelected() receives the correct item.getItemId()
Context menu not showing on long-press:
✔ Verify registerForContextMenu(view) is called
Summary
This guide showed how to implement menus in Android using:
- Options Menu for global app bar actions
- Context Menu for view-specific long-press options
- Popup Menu for inline actions
- Toolbar integration for a modern app bar
Menus enhance navigation, clarity, and user control in your Android apps when used appropriately with clean code structure.


