Kah – The Developer

Building a Menu for your Android (V1.0 R1) App

Posted by: kahgoh on: 25 November, 2008

In Building a simple menu in Android, a menu was built using the MenuBuilder. Since the release of version 1.0 (release 1), MenuBuilder has been removed. In this tutorial, we’ll set up a custom menu that gets displayed when the menu button is pressed.

To add the menu for your application, your activity needs to override the onCreateOptionsMenu method. The method will be given the instance of the menu to popuplate. Our overridden method will add the items to the instance that is given. To control what happens when an item on the menu is selected, override the onOptionsItemSelected method. Here is the first part of the code that sets up the activity and adds a couple of items to our menu:


 1 package com.android.menu;
 2
 3 import android.app.Activity;
 4 import android.app.AlertDialog;
 5 import android.os.Bundle;
 6 import android.view.Menu;
 7 import android.view.MenuInflater;
 8 import android.view.MenuItem;
 9 import android.view.SubMenu;
10
11 public class MenuDemo extends Activity
12 {
13   /** 
14    * Called when the activity is first created. 
15    */
16   @Override
17   public void onCreate(Bundle savedInstanceState) 
18   {
19     super.onCreate(savedInstanceState);
20     setContentView(R.layout.main);      
21   }
22     
23   /**
24    * {@inheritDoc}
25    */
26   @Override
27   public boolean onCreateOptionsMenu(Menu menu) 
28   {
29     super.onCreateOptionsMenu(menu);
30     
31     MenuItem item = menu.add("Painting");
32     item.setIcon(R.drawable.paint);
33     
34     item = menu.add("Photos");
35     item.setIcon(R.drawable.photos);
36     

The call to to the setIcon method attachs an image to the menu item. In the example above, the images have been placed in the appropriate resources directory and is being referenced from there.

The items added so far are top level items. Now let’s say we want to attach a submenu. This is as easy as calling the addSubMenu method.


37     SubMenu subScience = menu.addSubMenu(
38         R.string.scienceMenuName);
39     subScience.setIcon(R.drawable.science);
40

To add items to our submenu, we can create the menu items programatically (like we did with the top level menu). However, just to show you another way of setting up the menu, we’ll use load up a menu specified by a menu XML file here instead. You can use the Android Menu Editor to edit the XML if you want (in Eclipse, create an XML file and then right click on the file Open With -> Android Menu Editor) or you can use the text editor to manually create the XML. Below is the contents of the XML file that I used:


1 <menu xmlns:android="http://schemas.android.com/apk/res/android">
2   <item android:title="Physics" />
3   <item android:title="Chemistry" />
4   <item android:title="Biology" />
5 </menu>
6

It should be noted that we could have also created our top level menu the same way! The loading of the menu from an XML file is done through the use of a MenuInflater.


41     // Now, inflate our submenu.
42     MenuInflater inflater = new MenuInflater(this);
43     inflater.inflate(R.menu.menu, subScience);
44

Of course, once the menu has been loaded from an XML file, we can still add items to the submenu.


45     // Programatically, add one item to the submenu.
46     subScience.add("Meteorology");
47

To make the menu displayable when the menu button is pressed, the method needs to return true.


48     // Return true so that the menu gets displayed.
49     return true;
50   }
51

With the menu setup, we now override the onOptionsItemSelected method to handle menu selections. The selected menu item is given back as the method parameter. In this example, the an alert dialog simply pops up, showing what item was selected.


52   /**
53    * {@inheritDoc}
54    */
55   public boolean onOptionsItemSelected(MenuItem item)
56   {
57
58     if (item.hasSubMenu() == false)
59     {
60       // For this demo, lets just give back what
61       // we found.
62       AlertDialog.Builder dialogBuilder = new 
63         AlertDialog.Builder(this);
64   
65       dialogBuilder.setMessage(" You selected " +
66           item.getTitle());
67   
68       dialogBuilder.setCancelable(true);
69       dialogBuilder.create().show();
70     }
71     
72     // Consume the selection event.
73     return true;
74   }
75 }
76

Tags:

6 Responses to "Building a Menu for your Android (V1.0 R1) App"

[...] Building a simple menu in Android 25 Nov 2008, This tutorial has been superceded by Building a Menu for your Android (V1.0 R1) App. [...]

Excellent little tutorial, mate. Android really has a neat SDK!

[...] Defining a Context Menu for your View In Building a Menu for your Android (V1.0 R1) App, a menu was created for an application. The context menu can create menus based on the current view [...]

Hi,
I tried to create an Menu bar in Android Project but in code I am facing some queries like “How do design the class and xml file for this”. Please help for this.
Thanks a lot…
Regards,
Ravisankar S
+91 988 477 9432

I don’t really understand what you are asking or what you are after. Once you have figured out how you want your menu to look, you should be able to easily translate it to code or the XML file. Also, note that the code in this little tutorial may also be out of date, as they have release later versions of the SDK.

[...] to build menus in Android. These articles also helped me to learn some more about the Android SDK. Simple Menus September 25th, 2009 in [...]

Leave a Reply