Android Long Click Context Menu

To demonstrate adding a long-click content menu to a ListView, I’ll reuse the Stock Viewer application from here: Android Lists V: Accessing and Consuming a SOAP Web Service II.

Initial code: (Revision 4) https://subversion.assembla.com/svn/vexed/trunk/StockViewer

Menu Layout

First, we need to make a menu layout file that will contain the items to appear on the context menu.  This is done by creating a menu sub-folder in res\, and then creating a new XML file (I named mine stock_item_menu.xml).

<menu xmlns:android="http://schemas.android.com/apk/res/android">
	<item android:id="@+id/remove_item" android:title="Remove" />
</menu>

This is a very simple context menu that only contains a single item: Remove.  The expected functionality of this item, when selected, is to remove the stock quote from the ListView.

Context Menu Event

In order to hook this menu XML file up to the long-click event, we need to “register” the ListView for a context menu.  This is done by adding the following line in onCreate(…).

registerForContextMenu(getListView());

By registering the ListView for a context menu, the onCreateContextMenu(…) method will be called when a stock quote list item is long clicked.  Let’s display the item menu that we created earlier by overriding the implementation for onCreateContextMenu(…).

Add the following method to the StockList.java file.

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

Go ahead… test it out.  Try to long click a stock quote item.  The menu with the single “Remove” option appears; however, it doesn’t appear to do anything… yet.

Context Menu Actions

Time to make the “Remove” option perform as intended.  This requires us to override one more method, onContextItemSelected(…), which is fired when the “Remove” option is selected from the context menu.

@Override
public boolean onContextItemSelected(MenuItem item) {
	AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
			.getMenuInfo();

	switch (item.getItemId()) {
	case R.id.remove_item:
		quoteResult.remove(info.position);
		((StockQuoteAdapter)getListAdapter()).notifyDataSetChanged();
		return true;
	}
	return false;
}

The call to item.getMenuInfo() retrieves the information about the list item that triggered the context menu.  This allows us to remove to correct stock quote from the list.

Also, the notifyDataSetChanged() call tells the ListView adapter that we have modified the contents of the list that is it display and to refresh itself (without this call, it would appear that the “Remove” option didn’t do anything, even though it actually updated the underlying list).

That’s it!  You have a fully functioning context menu that removes stock quotes that have been added to the ListView.

Final code: (Revision 5) https://subversion.assembla.com/svn/vexed/trunk/StockViewer

Tags: , , ,

This entry was posted on Saturday, July 30th, 2011 at 2:07 pm and is filed under Android. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “Android Long Click Context Menu”

GENiALi November 26th, 2012 at 1:15 am

thx for this helpfull post.

Nicolas April 20th, 2013 at 12:03 pm

Thanks a lot, this was really helpful.

Leave a Reply

You must be logged in to post a comment.