[Android] Beginner Tutorials – Lesson 15. Context menu

ranslated by Taras Leskiv (http://android-by-example.blogspot.com/)

Context menu in Android is invoked by long pressing any screen component. It is usually used in lists. When the list of similar objects is displayed (like messages in the mailbox) and you need to apply an action to one of these objects, a context menu is invoked for this object. But we didn’t study lists yet, so we will make a simpler example and will invoke a context menu for TextView.

Let’s create a project:

Project name: P0151_ContextMenu
Build Target: Android 2.3.3
Application name: ContextMenu
Package name: ru.startandroid.develop.contextmenu
Create Activity: MainActivity

Open main.xml and create two TextView objects there:


For the first TextView we will make a context menu using which we will change the text color. For the second we will change the text size.

The way of creating a context menu is quite similar to creating a simple menu. But there are some differences.

onCreateContextMenu method is invoked every time before the menu is displayed. It is passed these parameters:
ContextMenu, into which we will add items
View – screen element, which the context menu is invoked for
ContextMenu.ContextMenuInfo – contains additional information, when the context menu is invoked for a list element. We don’t use it for now, but when we will learn lists, we will see that it is useful.

onContextItemSelected process method is similar to onOptionsItemSelected for a simple menu. MenuItem is passed as a parameter – it is a menu item that has been clicked.

We will also need a third method registerForContextMenu. It is passed View as a parameter and it means that context menu should be created for this View. If you do not invoke this method, context menu for a View will not be created.

Lets code. Open MainActivity.java. Let’s declare and find TextView and point out that context menu must be created for them.

Now let’s code the creation of context menus. We will use constants for storing menu item IDs.

And create

Pay attention that we define View for which context menu was invoked by its ID and depending on this ID, create a specific menu. So if context menu was invoked for tvColor, we create a menu with colors enumeration, if for tvSize – enumeration with font sizes.

We use constants as item IDs. We don’t apply grouping or sorting, that’s why we use zeros as corresponding parameters.

Now you can save everything and run. When long pressing a TextView context menu should appear.


But clicking on items does nothing as we haven’t written processing in onContextItemSelected method. Let’s fill it in:

In this method we define by ID which menu item has been clicked. And we do the corresponding actions afterwards: change the text color for tvColor and font size for tvSize. Save, run and see that context menus now react to clicks and do what is required from them

To broaden your horizons I would like to write something else about this topic. It may seem complicated for now, but it it’s fine. So here are some thoughts.

We have used registerForContextMenu(View view) method for turning on context menu for a specific View. This method belongs to Activity class. I’ve looked at the source code of this method. The following is written there:

Remember our lesson (9) about listeners and have a look at help for setOnCreateContextMenuListener (View.OnCreateContextMenuListener l) method. It looks like View uses this object as listener for creating context menu. In this case, our code is inside Activity, so it means that this object is Activity. It means when a View wants to show a context menu, it refers to listener (Activity) and it invokes its onCreateContextMenu method. So it is the similar concept as with a simple click.

And a line in MainActivity.java:

is absolutely equal to this line:

We can also create our own object that implements View.OnCreateContextMenuListener and use it instead of Activity as a listener for creating a context menu.

Don’t forget that you can also use XML-method to create a context menu, which we have reviewed at the end of the previous lesson. Try to complete the same lesson again, but using XML-menu this time.

Complete lesson code:




Share this Post :

No comments yet.

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *