Pin Me

Guide to Creating Android App Menus

written by: Jbeerdev•edited by: Simon Hill•updated: 5/4/2011

If you have played a bit with an Android device (or its Emulator), you have noticed the Menu button function. Pressing this displays options in the screen, usually related to the application we are using. Let's learn how to create an Options Menu.

  • slide 1 of 4

    We have two ways to create a Menu in an Android application. We can do it in the Java code or we can create it using XML. First of all, lets do it in the Java code, and next in XML. This way we will see the differences and we can adopt the better way according to our programming style.

  • slide 2 of 4

    Menu in Java Code

    We need to add the following code to our Activity, to create the Menu:

    public boolean onCreateOptionsMenu(Menu menu){

    menu.add(0,EDIT_CONTACT,0,"Edit Contact");

    menu.add(0,DELETE_CONTACT,0,"Delete Contact");

    menu.add(0,EXIT,0,"Exit");

    return true

    }

    In the method add() we have four arguments.

    groupId → with this parameter we can associate this item with a group of others items.

    itemId → the ID unique item identifier.

    order → this number allows us to order the elements in the menu

    title → this is the string that its going to appear in the Menu Screen.

    public boolean onOptionsItemSelected (MenuItem item){

    switch (item.getItemId()){

    case EDIT_CONTACT:

    /* Actions in case that Edid Contacts is pressed */

    return true

    case DELETE_CONTACT :

    /* Actions in case that Delete Contact is pressed */

    return true

    }

    .......

    return false

    }

    With the onOptionsItemSelected method we control the actions of every item of the menu.

    In addition, we can put a picture to each item in the Menu, to do this, we just need to add the following method to the menu.add() line:

    menu.add(0,EDIT_CONTACT, 0, "Edit"). setIcon(R.drawable.edit_icon);

    We get the icon from a drawable resource.

    If our Android device has physical keyboard, maybe we would like to add shortcuts to our menu. We can add an Alphabetic shorcut (with setAlphabeticShorcut), a numeric shorcut (with setNumericShortcut) or we can add both with setShortcut. Lets see an example:

    menu.add(0,EDIT_CONTACT,0,"edit"). setAlphabeticShortcut('e');

    When "e" is pressed, the EDIT_CONTACT option will be displayed.

  • slide 3 of 4

    Menu in XML code

    Creating a menu using XML code is even easier. We need to add a new folder inside the "res" folder. This folder will be called "menu" and inside of it we can add a "menu.xml" file with the following content:

    <menu xmlns:android="https://schemas.android.com/apk/res/android">

    <item android:id="@+id/edit_contact" android:title="Edit Contact" />

    <item android:id="@+id/delete_contact" android:title="Delete Contact" />

    <item android:id="@+id/exit" android:title="Exit" />

    </menu>

    We create a <menu> </menu> element in XML, and inside of it, we add <item/> according to the number of items we want inside of our menu. We have basic parameters in the <item> tags:

    android:id -> unique identifier of the item.

    android:title-> the String that its going to be displayed in the item menu.

    To finish creating our menu in XML, we need to add the following lines in the Activity:

    public boolean onCreateOptionsMenu(Menu menu){

    MenuInflater inflater = getMenuInflater();

    inflater.inflate(R.menu.menu, menu);

    return true

    }

    With the inflater we load the menu from the XML code, calling to its resource in "R.menu.menu".

    Creating a menu is that easy! Using this base we can create submenus and check options menu. But this will be explained in other article.

  • slide 4 of 4

    Follow up

    If you want to know when new articles are released, subscribe yourself to the Google Android RSS Otherwise, you can follow my research, articles and work in my professional twitter: jbeerdev