Android Menu Tutorial

Menus are a common UI component in many types of applications. Menus are useful for displaying additional options that are not directly visible on the main UI of an application. In this tutorial we will learn about different types of android menus & their implementation.
Android offers three types of menus :
  • Options Menu : An Options menu is presented when the user presses the MENU button while the Activity is active.
  • Context Menu : A context menu is a floating list of menu items that appears when the user performs a long-press on a View.
  • Sub Menu : A 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.
  •  
Options Menu Example


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/about”
           android:icon=”@drawable/ic_about”     
           android:title=”@string/about” />   
  <item android:id=”@+id/quit”     
           android:icon=”@drawable/ic_quit”    
           android:title=”@string/quit” />
 </menu>

Save it as options_menu.xml under <project_path>/res/menu  folder.

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();    
        inflater.inflate(R.menu.options_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.about:          
        //code for menu option about      
        return true;          
    case R.id.quit:         
        //code for menu option quit   
        return true;         
    default:             
        return super.onOptionsItemSelected(item);   
    }
}



Context Menu Example

  
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/about”     
        android:icon=”@drawable/ic_about”     
        android:title=”@string/about” />
  <item android:id=”@+id/quit”    
        android:icon=”@drawable/ic_quit”      
        android:title=”@string/quit” />
</menu>

Save it as context_menu.xml under <project_path>/res/menu  folder.

Step 2 – Displaying 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.about:        
        //code for menu option about 
        return true;       
    case R.id.quit:      
        //code for menu option quit      
        return true;     
    default:        
        return super.onContextItemSelected(item); 
    }
}

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

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



SubMenu Example


Adding SubMenu to Options Menu

public boolean onCreateOptionsMenu(Menu menu) {  
    SubMenu subMenu = menu.addSubMenu("Contact");    
    subMenu.add(0,MENU_SMS,0,"SMS");       
    subMenu.add(0,MENU_EMAIL,0,"EMail");        
    return true;
}


Adding SubMenu to Context Menu

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {  
    super.onCreateContextMenu(menu, v, menuInfo);      
    menu.add(0, MENU_SMS, 0, "SMS");       
    menu.add(0, MENU_EMAIL, 0, "Email");
}





Be the first to comment

Post a Comment

Recent Posts

Site Stats

Our Apps

Get it on Google Play

©2012-2014 Android Aspect - All rights reserved | Powered by Blogger

Creative Commons License