Tutorials Android Android Menu Example

Android Menu Example

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.

Total
0
Shares
guest
9 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ashwath
Ashwath
10 years ago

Nice and simple example to learn menu items

Enrim
Enrim
9 years ago

Can you pls provide a action bar menu style design??

mike
mike
9 years ago

Hi, i have reading out and i will definitely bookmarrk your site, just wanted to say i liked this article.

Hanna Harper
Hanna Harper
9 years ago

very good example.. do you have any sample on android action bar menu items?

jancuk
jancuk
9 years ago

i have error message in :

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;
}

the error message is:
– mapMenu cannot be resolved
– favMenucannot be resolved
– listMenu cannot be resolved
all of R.id cannot be resolved

any solution?

javatechig
javatechig
9 years ago
Reply to  jancuk

You must declare menu items in your xml.

RK-Muscles God
RK-Muscles God
7 years ago

can you tell me how i can add webview in this ?

Nilanchala Panigrahy
Nilanchala Panigrahy
7 years ago
Reply to  RK-Muscles God

Checkout WebView tutorial

Android WebView Example

Nilanchala Panigrahy
Nilanchala Panigrahy
7 years ago

Create an super class activity and abstract onCreateOptionsMenu and onOptionsItemSelected method inside it.

Let all other activities extend your super class activity.

Previous Post
PhoneGap Tutorials

Sending SMS from JavaScript using Phonegap SMS plugin

Next Post
Android Tutorials

Android Radio Button Example

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
9
0
Would love your thoughts, please comment.x
()
x