Filtering context menu

A really nice feature that you can use when using a list view is the use of context menu. A context menu appears when a user touches the screen and holds on for a long time. Context menu imitates a click mouse button users usually have in Windows.

I won’t explain how to setup a context menu (you can read it straight from Android dev site), however I do want to explain how to show different context menu or even not showing at all depending on the context.

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
	ContextMenuInfo menuInfo) {
		// Show context menu only in case it is a real record.
		AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
		if (info.targetView instanceof WorkoutListView) {
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.history_context_menu, menu);
		}
	}

The magic happens in onCreateContextMenu. This method is being called every time a context menu is about to show up. You can access the list item view on which the method is being triggered by accessing targetView property.

From here you can either query if the specific list item view is of certain type, cast it and use a property to determine whether to display a context menu. In our case I’ve decided to display context menu only on items that are of a certain type.

Advertisements

Leave a comment

Filed under Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s