Menus

Intro

Android provides two types of Menus: the options menu and the context menu.  The options menu is displayed when the user clicks on the device's menu button, or (for device's running Android 3.0 or above) the menu icon in the ActionBar (the task bar at the top of the app).  The context menu is a floating menu displayed when the user long-clicks on a View that has been set to listen for long clicks.

Both menus are defined in XML by files in the res > menu folder using the following format

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/menu_show"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="Show"/>
    <item
        android:id="@+id/menu_forward"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="Forward"/>
    <item
        android:id="@+id/menu_back"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="Back"/>
</menu>

Options Menu

The Activity class's onCreateOptionsMenu method is  overridden to create the options menu:

@Override
public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);
    return true;
}

When an item in the menu is selected, the event is handled in the onOptionsItemSelected method:

@Override
public boolean onOptionsItemSelected(MenuItem item){
    // Handle item selection
    switch(item.getItemId()){
        case R.id.show:
            // do stuff here
            return true;
        case R.id.forward:
            // do stuff here
            return true;
        case R.id.back:
            // do stuff here
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

 Context Menu

The context menu is triggered when the user long-clicks on a View that has been registered.  The Activity class's onCreateContextMenu method is  overridden to create the context menu:

@Override
public void onCreateContextMenu(ContextMenu menu,View v,
                                ContextMenuInfo menuInfo){
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
}

Like the options menu, the context menu has a method that allows you to define behaviors:

@Override
public boolean onContextItemSelected(MenuItem item){
    AdapterContextMenuInfo info =(AdapterContextMenuInfo) item.getMenuInfo();
    switch(item.getItemId()){
        case R.id.add_url:
            editNote(info.id);
            return true;
        case R.id.delete_url:
            deleteNote(info.id);
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

Finally, you must register a View to trigger the context menu using the registerForContextMenu method:

Button contextButton = (Button) findViewById(R.id.button1);
registerForContextMenu(contextButton);

 

Download the working code from GitHub: https://github.com/jonipepin/MenusExample