StackTips

Android Menu Example

nilan avtar

Written by

Nilanchala,  6 min read,  2.61K views, updated on Sept. 17, 2023

This tutorial explains how to create menu in android with example.

Menus are a common user interface component in many types of applications. To provide a familiar and consistent user experience, you should use the Menu APIs to present user actions and other options in your activities.

Declare Menu Items in XML

You can create android menus in two ways. One by using the code or using XML declaration. In this tutorial we are crating the menu items using the declarative approach from xml.

  1. Create a new folder named menu inside your project resources directory. This folder will contain all the menu declarative XML files.
  2. Now let us add a new xml file named menu_myactivity.xml file, into menu resource directory and paste the following code snippets.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">
    <item
        android:id="@+id/mapMenu"
        android:icon="@drawable/menu_map"
        android:title="Map" />
    <item
        android:id="@+id/favMenu"
        android:icon="@drawable/menu_favourite"
        android:title="Favorite" />
    <item
        android:id="@+id/listMenu"
        android:icon="@drawable/menu_list"
        android:title="List" />
    <item
        android:id="@+id/settingsMenu"
        android:icon="@drawable/menu_settings"
        android:title="Settings" />
</menu>

Notice that in the menu declaration xml, we are using some of the drawable images. Here you need to add your own images to drawable directory.

Note: I have used few images for menu items. Copy your images under drawable folder of project. For more details on icon size, Refer Guides for App Designers!

Using Menu from Android Activity

Implement the onCreateContextMenu() method from your android Activity class and inflate the menu items created in the menu_myactivity.xml. We can also override OnOptionsItemSelected method to handle the events while user presses the menu option in android.

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class MenuActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		TextView text = new TextView(this);
		text.setText("Press the menu button to get list of menus.");
		addContentView(text, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_myactivity, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Toast.makeText(getApplicationContext(),	item.getTitle() + " selected", Toast.LENGTH_SHORT).show();

		switch (item.getItemId()) {
		case R.id.mapMenu:
			// do something
			break;
		case R.id.favMenu:
			// do something
			break;
		case R.id.listMenu:
			// do something
			break;
		case R.id.settingsMenu:
			// do something
			break;
		}
		return true;
	}

}

Demo

Run the application, you will notice the result as shown in the screenshot below.