How to Create Menus in Android Applications using Java or XML

How to Create Menus in Android Applications using Java or XML
Page content

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.

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.

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:

We create a

element in XML, and inside of it, we add according to the number of items we want inside of our menu. We have basic parameters in the 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.

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

This post is part of the series: How to- Develop Applications on Android: More Functionality

This time we will learn more advanced functionality to add to our Android Applications.

  1. Guide to Creating Android App Menus
  2. How to create Advanced Menus Via Our Google Android Applications
  3. Guide to Using Basic Content Providers in Android Development
  4. Dev Guide to Advanced Content Providers on Android
  5. How to interact with Android Events