Android Context Menu Tutorial

January 31st, 2013 Leave a comment
Like the article?
Android Context Menu Tutorial

The context menu in Android is vital to many applications – the best way to think about it is to equate it with a right-click on a normal desktop computer. If you long-press a view in an app, it expands a similar list and you can take certain actions with them that you would not be able to do with regular clicks alone; while many different customizations and actions can be performed with single presses or hard menu buttons, often the quickest and most user-friendly way to implement an action is through the use of these “right-click” or “long-press” menus, known also as context menus. Let’s take a look at how to make them!

Step 1: Creating Your XML

In a new Android project, create a menu.xml in the /res/menu folder of your project. Add in the following to your menu.xml:

<menu  xmlns:android="http://schemas.android.com/apk/res/android">
     <group android:checkableBehavior="single">
       <item android:id="@+id/testitem1" android:title="Test Item 1"></item>
          <item android:id="@+id/testitem2" android:title="Test Item 2"></item>
       <item android:id="@+id/testitem3" android:title="Test Item 3"></item>
   </group>
</menu>

Step 2: Bringing The XML Into The Activity

Here’s where we’re going to inflate the XML into the activity. Add this into the activity:

@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo contextinfo) {
  MenuInflater inflater = getMenuInflater();   
  inflater.inflate(R.menu.menu, menu);         
  super.onCreateContextMenu(menu, view, contextinfo);
  return true; 
}

Step 3: Implementing the Menu Event Handling

Now that you have your buttons, you need to do something with them! Here’s the code that handles the buttons when they’re clicked and tells your app to do something.

@Override
public boolean onContextItemSelected(MenuItem item) {
  switch (item.getItemId()) {                              
  	case R.id.testitem1:
	    Toast.makeText(this, "You Pressed Test Item 1!",Toast.LENGTH_SHORT).show(); 
	  break;
	case R.id.testitem2:
	    Toast.makeText(this, "You Pressed Test Item 2!",Toast.LENGTH_SHORT).show(); 
	  break;

	case R.id.testitem3:
	    Toast.makeText(this, "You Pressed Test Item 3!",Toast.LENGTH_SHORT).show();  
	  break;
	  }
  return true;
}

And that’s it! That’s how you can add your own menus and context handlers into Android. It’s not a hard process, and it’s one that’s vital to the success of your apps – with context handling, you can create apps that are easier to use and provide your users with more functionality and diversity than they would otherwise get with an app that relied solely on hard buttons and single presses to do the heavy lifting. Streamline your app and make it better for your users by adding in well-placed and smart context menus using the above sample code!

Help us spread the word!
  • Twitter
  • Facebook
  • LinkedIn
  • Pinterest
  • Delicious
  • DZone
  • Reddit
  • Sphinn
  • StumbleUpon
  • Google Plus
  • RSS
  • Email
  • Print
If you liked this article, consider enrolling in one of these related courses:
Don't miss another post! Receive updates via email!

Comment