How to create Android Menus Tutorial

Options Menu

Options menu is created when the user presses the menu button while the Activity is active.
Step 1 – Creating the menu XML

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:id="@+id/new_game"
         android:icon="@drawable/ic_new_game"
         android:title="@string/new_game" />
   <item android:id="@+id/quit"
         android:icon="@drawable/ic_quit"
         android:title="@string/quit" />
</menu>

Step 2 – Showing the menu (implemented in Activity class)

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

Step 3 – Listening to user choice (implemented in Activity class)

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   // Handle item selection
   switch (item.getItemId()) {
   case R.id.new_game:
       newGame();
       return true;
   case R.id.quit:
       quit();
       return true;
   default:
       return super.onOptionsItemSelected(item);
   }
}

Step 4 – optional: updating the menu items dynamically.

@Override
public boolean onPrepareOptionsMenu(int featureId, Menu menu) {
          updateMenuItems(); //update icons or labels if needed
}

android option menu

Opening the options menu by code
Options menu is usually triggered by the user pressing the options button, but you have the ability to trigger it also from the activity.

OnClickListener ocl =  new OnClickListener(){
     @Override
     public void onClick (View v){
          myActivity.openOptionsMenu();
     }
}

 

Context Menu

A floating list of menu items that appears when the user performs a long-press on a View.
Step 1 – Creating a menu XML

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:id="@+id/new_game"
         android:icon="@drawable/ic_new_game"
         android:title="@string/new_game" />
   <item android:id="@+id/quit"
         android:icon="@drawable/ic_quit"
         android:title="@string/quit" />
</menu>

Step 2 – Showing the menu (implemented in Activity class)

@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);
}

Step 3 – Listening to user choice (implemented in Activity class)

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

Step 4 – Attaching the context menu to a view (implemented in Activity class)

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

android option menu

Submenu

Submenu is a floating list of menu items that the user opens by pressing a menu item in the Options Menu or a context menu.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:id="@+id/file"
         android:icon="@drawable/file"
         android:title="@string/file" >
       <!-- "file" submenu -->
       <menu">
           <item android:id="@+id/new"
                 android:title="@string/new" />
           <item android:id="@+id/open"
                 android:title="@string/open" />
       </menu>
   </item>
</menu>

 

 

About jkoder