Like the article? You may like us on Facebook or follow us on Twitter.

Android Menu Example

This tutorial explains Android Menu 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.

For more information on API refer to Android Developer Portal

1. Declare Menu Items in XML

Android supports menu to be craeted in two ways. One by using the code or by using the Google Recommended Declarative manner using XML layouts. In this tutorial we are crating the menu items using the declarative approach from xml.

Android project has some special sub-folders used to manage its resources. One is resmenu and its role is to manage xml files that describe Activity’s menus.

If you want to add a new menu_sample.xml file in the project resmenu subfolder, you can create the menu sub-folder (the generated Android project doesn’t have one). Refer image for the folder structure

File: menu_sample.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!--?xml version="1.0" encoding="utf-8"?-->
<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="Favourite"/>
<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"/>

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!

2. Declaring Android Activity

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

File: MenuActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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_sample, menu);
return true;
 
}
 
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(getApplicationContext(), item.getTitle() + " menu 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;
}
 
}

 

3. Demo

Run the application. and it results screen with menu items.

About Nilanchala

A blogger, bit of a tech freak and a software developer. Follow him on Twitter, or like our official Facebook page or Google Plus.

  • jancuk

    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?

    • http://javatechig.com/ javatechig

      You must declare menu items in your xml.

  • Hanna Harper

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

  • mike

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

  • Enrim

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

  • Ashwath

    Nice and simple example to learn menu items