Menu in Android
Menu in Android
Menu in Android provides options for user actions and navigation in an app. Menus can appear as options menus (accessed via the app bar), context menus (triggered by long-press), or popup menus (small floating lists). They are typically defined in XML using <menu>
and <item>
tags and inflated using MenuInflater
in activities or fragments. Developers handle menu item clicks using onOptionsItemSelected()
or similar methods. Menus help improve usability by grouping actions in a consistent interface. Common use cases include settings, search, and sharing options, offering a clean and accessible way to enhance app functionality.
- Menus provide a way to present actions or options to the user.
- You can find menu in res folder where you create menu.
Types of Menu
1. Options Menu
- Displayed in the app bar, it’s used for global actions that impact to the entire app or current screen.
- onCreateOptionsMenu(Menu menu) method is used to inflate the Options Menu.
- onOptionsItemSelected(MenuItem item) method is used to handle clicks in menu item based on item Id.
Create Menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@mipmap/ic_launcher"
android:title="Search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />
<item
android:id="@+id/action_settings"
android:title="Settings"
app:showAsAction="never"
/>
</menu>
Inflate the Options Menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
Handle Click
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
Toast.makeText(this, "Search Clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_settings:
Toast.makeText(this, "Settings Clicked", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
2. Context Menu
- It appears when a user performs a long click on a view.
- Shows actions related to a specific UI component.
Create Context Menu
<?xml version="1.0" encoding="utf-8"?>
<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" />
</menu>
Inflate the 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);
}
Handle Click
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_edit:
Toast.makeText(this, "Edit Context Clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_delete:
Toast.makeText(this, "Delete Context Clicked", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onContextItemSelected(item);
}
}
3. PopUp Menu
- It appears from a view and displays a list of items.
- Similar to context menu but is more flexible in terms of positioning.
- It is mostly used for the actions related to specific UI element or content.
- Displayed in a modal popup window anchored to a view.
Create PopUp Menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_edit_pop"
android:title="Edit Pop" />
<item
android:id="@+id/action_delete_pop"
android:title="Delete Pop" />
</menu>
Inflate PopUp Menu
PopupMenu popupMenu = new PopupMenu(this, v);
popupMenu.inflate(R.menu.popup_menu);
popupMenu.show();
Handle Click
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_edit_pop:
Toast.makeText(MainActivity.this, "Edit Pop Clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_delete_pop:
Toast.makeText(MainActivity.this, "Delete Pop Clicked", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
});
Conclusion
Menus in Android offer a consistent and user-friendly way to present actions and navigation options within an app. By using options menus for global actions, context menus for specific UI elements, and popup menus for flexible interaction, developers can enhance the overall usability and functionality of their applications. Proper implementation of menus not only organizes actions effectively but also improves the user experience by making key features easily accessible.
Let’s get connected
We can be friends. Find on Facebook, Linkedin, Github, YouTube,
BuyMeACoffee, and Instagram.
Contribute: BuyMeACoffee
Contact: Contact Us