Pin Me

How to create Advanced Menus Via Our Google Android Applications

written by: Jbeerdev•edited by: Simon Hill•updated: 7/1/2010

In this article we will see how to create a more advanced menu within our applications. To do this we will learn how to create sub-menus and context menus.

  • slide 1 of 3

    Context Menu

    Context menus are similar to right click menus on desktop operating systems. These menus are related with a view in the layout-tree. So imagine that you have an image on the screen (an image with ImageView), you then long press the image (long-press → about 2 or 3 seconds) and then a menu appears with different options: “Edit”, “Delete”, “Change Name”... basically whatever you want to output, of course this is just an example so lets develop the idea a bit further.

    Let's start by creating a View in our onCreate() Activity method.

    ImageView mImage = new ImageView(this);

    We can create a view in this way, or we can create it using this:

    mImage = (ImageView) findViewById(R.id.imageName);

    (Once we have set the ContentView to the Activity with setContentView(R.layout.mainView); )

    Now, we are going to add a content menu to this ImageView, for that we use the following method.

    registerForContextMenu(mImage);

    or we can directly call the view using:

    registerForContextMenu(findViewById(R.id.imageName));

    Luckily If we use this last line, we don`t need to create an ImageView object.

    Once we have done this. We need to override a pair of methods in the Activity.

    One of them is:

    onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){

    super.onCreateContextMenu(menu, v, menuInfo);

    menu.add(0, EDIT_IMAGE, 0, "Edit Image");

    menu.add(0, CHANGE_NAME, 0, "Change name");

    }

    With the .add method, we add options to the menu. In this case, we only have a context menu, but if we would like to add more of these menus to others views, we have to be careful with the onCreateContextMenu method, because we will need to filter by view:

    An example in pseudo-code:

    if(view) is "TextView":

    do A

    else if (view) is "ImageView"

    do B

    ....

    Remember that we can identify every view with an unique ID.

    Now we have to add functionality to every menu option we have created. This is done with the "onContextItemSelected" method.

    public boolean onContextItemSelected (MenuItem item) {

    switch (item.getItemId()){

    case EDIT_IMAGE:

    editImage();

    return true;

    case CHANGE_NAME:

    changeName();

    return true;

    default:

    return super.onContextItemSelected(Item);

    }

    }

  • slide 2 of 3

    Sub-Menus

    Sometimes we have too much to add in the menus or we just want to order our options-menus in topics. To do this, we just need to add the following structure in the onCreateOptionsMenu method.

    SubMenu toolsMenu = menu.addSubMenu("Tools");

    SubMenu helpMenu = menu.addSubMenu("Help");

    toolsMenu.add("languaje");

    toolsMenu.add("Settings");

    toolsMenu.add("Format");

    toolsMenu.add("Table");

    toolsMenu.add("Font");

    helpMenu.add("Help");

    helpMenu.add("Update");

    helpMenu.add("Update");

    Creating sub-menus in XML is very simple too, just we have to add the tag “menu” inside of the “item” tag we want to put as submenu as its shown in this piece of code.

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

    <item android:title:"Option 1" />

    <item android:id = "@+id/submenu1" android:title="SubMenu 1">

    <menu>

    <item android:id="@+id/suboption1" android:title="SubOption 1" />

    <item android:id="@+id/suboption2" android:title="SubOption 2" />

    </menu>

    </item>

    <item android:title:"Option 2" />

    </menu>

  • slide 3 of 3

    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