oming from a Windows and .NET background, I had some trouble understanding how to interact with the ListView control and context menu creation in Android. Context menus are supposed to be shown on your mobile device when you touch/click the screen and hold on for a longer time. So here is how to determine which item is long-clicked and how to show a context menu for it.
Source code for this blog post is available as a complete Eclipse project athttp://github.com/mikeplate/ListViewDemo (zip download link in upper right corner).
An Activity with an expanding ListView and a docked TextView
If you have an activity that will only contain a single ListView control, you can derive your activity from the ListActivity instead of Activity. However, I think I might like to show some extra info below my ListView so I chose to have a separate ListView object. My activity layout looks like this:
2 < LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
3 android:layout_width ="fill_parent"
4 android:layout_height ="fill_parent"
5 android:orientation ="vertical"
6 >
7 < ListView
8 android:id ="@+id/list"
9 android:layout_width ="fill_parent"
10 android:layout_height ="0px"
11 android:layout_weight ="1"
12 />
13 < TextView
14 android:id ="@+id/footer"
15 android:layout_width ="fill_parent"
16 android:layout_height ="60dip"
17 android:text ="@string/footer"
18 android:padding ="4dip"
19 android:background ="#FF666666"
20 />
21 </ LinearLayout >
Note a nice trick that I’ve used to get the TextView to “dock” at the bottom with a definied height, and have the ListView automatically fill out the rest of the height. This kind of thinking is important since Android devices can have different resolutions. The trick is to set the layout_height to zero pixels and the layout_weight to one (default is zero). Not sure about the logic behind that, but it works!
In order to have something to put into my ListView, I created a few country names in a string array as a resource and I sort that array before adding it to the ListView with the ArrayAdapter object. (Check out source code link above for this content.)
2 private String[] Countries;
3 @Override
4 public void onCreate(Bundle savedInstanceState) {
5 super.onCreate(savedInstanceState);
6 setContentView(R.layout.main);
7 Countries = getResources().getStringArray(R.array.countries);
8 Arrays.sort(Countries);
9 ListView list = (ListView)findViewById(R.id.list);
10 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem, Countries);
11 list.setAdapter(adapter);
12 registerForContextMenu(list);
13 }
14 }
Creating a ContextMenu in Android
When the user long-clicks, the event onCreateContextMenu is fired for the control that the user is clicking. For me, that is the ListView control. But since I don’t want to write a custom ListView-derived class, I want to catch that event in my activity. There does not seem to be any bubbling going on. Events fired in a child control does not bubble up to the parent if they are unhandled.
But obviously, the api designers have thought of this since there is a special method for this situation. Call the registerForContextMenu in your activity for this! This will actually make sure your overridden methods for both onCreateContextMenu and onContextItemSelected is called for the ListView-events as we’ll see soon.
Next, we’ll provide the implementation of onCreateContextMenu. Here I want to ensure that the event comes from the ListView and if so, I want to determine on which item in the ListView the user long-clicked.
2 public void onCreateContextMenu(ContextMenu menu, View v,
3 ContextMenuInfo menuInfo) {
4 if (v.getId() == R.id.list) {
5 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
6 menu.setHeaderTitle(Countries[info.position]);
7 String[] menuItems = getResources().getStringArray(R.array.menu);
8 for ( int i = 0 ; i < menuItems.length; i ++ ) {
9 menu.add(Menu.NONE, i, i, menuItems[i]);
10 }
11 }
12 }
As you can see, the argument of type ContextMenuInfo can actually change depending on what type of control is sending the event. For ListViews, the class you need to type cast into is AdapterView.AdapterContextMenuInfo. From there I used the position, which in my case corresponds to the index into the string-array. From the array I retrieve the string for that particular item and use as title for the menu. Then you can of course add all the menu commands you like. For the demo, I defined another string array as a resource with the commands I want to add.
When creating the menu items with the add-call, I specify that I don’t want any grouping of the items (Menu.NONE) and that the order and id of the item is the same (i). The last argument to add is the text to display for the item.
Responding to selected MenuItem
If the user dismisses the context menu (for instance, by back button) you don’t need to do anything. But for catching the actual selection of one of the items, you need to override onContextItemSelected.
2 public boolean onContextItemSelected(MenuItem item) {
3 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
4 int menuItemIndex = item.getItemId();
5 String[] menuItems = getResources().getStringArray(R.array.menu);
6 String menuItemName = menuItems[menuItemIndex];
7 String listItemName = Countries[info.position];
8 TextView text = (TextView)findViewById(R.id.footer);
9 text.setText(String.format(”Selected % s for item % s”, menuItemName, listItemName));
10 return true ;
11 }
The MenuItem argument holds all information that you need. The ContextMenuInfo object that got sent to onCreateContextMenu is still there and still needs type casting. Or I guess you could have saved that info in the activity between the calls, but I didn’t.
The id of the menu item selected is the same as the index into the string array of menu item texts for me. Instead of just outputting the menu command name and the list item text in a TextView, you would most likely have a big switch statement on menuItemIndex.
This was my first blog post and code demo for the Android platform. I hope it won’t be the last! The goal is to build upon this demo and/or other demos in my investigations of the Android platform. Please let me know in the comments if you have even better methods or code patterns that solves problems like this