[Android] 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.

19 Aug 2008, Note: Due to changes to the Android SDK, the code in this example does not work in 0.9 beta.

The menu builder class provides a way to build a simple menu, similar to the one below, in Android.

Menu built with the menu builder class.

To do anything useful with the menu, you need to specify what to do when an item is selected. A class that implements the MenuItem.OnClickListener interface is required to perform these actions. Here is an example of a listener that popups a dialog with the selection:

1   public class AndroidMenuButton extends Activity
2   {
3       private View menuInstance = null;
4  
5       private class ReactToClick
            implements Menu.OnClickListener
6       {
7           public ReactToClick()
8           {
9               // Empty constructor. Do nothing.
10          }
11          
12          /**
13           * React to an item being clicked.
14           */
15          public boolean onClick(Item item) 
16          {
17              // Display a that "echoes" the item that was
                // selected.

18              AlertDialog.Builder dialogBuilder = new 
19                  AlertDialog.Builder(AndroidMenuButton.this);
20              
21              dialogBuilder.setMessage(" You selected " +
22                      item.getTitle());
23              dialogBuilder.setCancelable(true);
24              dialogBuilder.create().show();
25  
26              // Click has been processed, so consume it.
27              return true;
28          }
29      }
30  

A layout can be setup to place the view on. For this example, I’ve set up a vertical linear view using XML and I grab the layout with the findViewById method:

31      /** 
32       * Called when the activity is first created. 
33       */
34      @Override
35      public void onCreate(Bundle icicle) 
36      {
37          super.onCreate(icicle);
38          setContentView(R.layout.capbutton);
39  
40          // Grab the view’s layout.
41          LinearLayout backView = (LinearLayout) 
42              findViewById(R.id.backgroundView);
43          

Next, create an instance of a MenuBuilder object and use the add method to place the menu items. Images for the menu items should be placed as a resource in res/drawables.

44          // Create the menu builder.
45          MenuBuilder builder = new MenuBuilder(this);
46          builder.setHeader("Menu Demo");
47          Item newItem = null;
48          ReactToClick action = new ReactToClick();
49          
50          // Tell the menu builder how to build the menu.
51          newItem = builder.add(0, 1, "Photos",
                R.drawable.item1);

The class, R, should be automatically be generated for you by the Android Eclipse plugin. R should contain references to all of your resources. The add method will return an Item object, to which you add your listeners to.

52          newItem.setClickListener(action);
53          newItem = builder.add(0, 2, "Painting",
                R.drawable.item2);
54          newItem.setClickListener(action);
55          newItem = builder.add(0, 3, "Science",
                R.drawable.item3);
56          newItem.setClickListener(action);
57          

Once all of the menu items have been added, the menu view can be constructed by the viewer and added to your own view.

58          // Get the menu instance and add it the view.
59          menuInstance = builder.getMenuView
60              (MenuBuilder.TYPE_ICON, backView, true);
61          menuInstance.setBackgroundColor(Color.WHITE);
62  
63          backView.addView(menuInstance);
64      }
65  }

It is also possible to have Android create the menu in different formats, such as the icon view (pictured in the first screenshot) or the context menu (pictured in the screenshot below).

Example of a context type menu.

The type of menu displayed is controlled by the first parameter to the MenuBuilder’s getMenuView method. The other two types of menus that have not been mentioned are the expanded menu and submenus. The MenuBuilder documentation details which constants the parameter needs to be set to in order to obtain the different types of menus (see the Constants section).

Advertisements

7 Responses to [Android] Building a simple menu in Android

  1. Shak says:

    Nice tutorial,

    Would it be possible to make the similar one for the last version of SDK (1.0)? Or are there any similar ones to this?

    Would be much appreciated.

  2. kahgoh says:

    It should definitely be possible. I’ll have to have a look at how to do it first, but I reckon there is another tutorial out there somewhere for v1.0

  3. Diya says:

    Hi,
    i just wan to know what is MenuBuilder?
    i m not getting it

  4. kahgoh says:

    Menu builder used to be something you could use to build a simple menu (like the one in the screenshot). It was removed since version 1 though.

  5. meojunior says:

    Nice tuts, really helpful for beginer like me. Can you post xml fil e ?
    Thanks again !

  6. kahgoh says:

    Hi meojunior! There is a more up-to-date tutorial for building menu is here: https://kahdev.wordpress.com/2008/11/25/building-a-menu-for-your-android-v10-r1-app/ . I can look for the XML specific to this tutorial if you really need it, but I suggest looking at the updated tutorial first.

  7. Android News says:

    Fastidious answers in return of this issue with genuine arguments
    and telling the whole thing about that.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: