Copyright© Jeffrey Jongko, Ateneo de Manila University Editing ListAdapter Data part 2
Selection and Touch Selection and Touch are two different interaction systems in Android Selection is used when using Arrows/Fire keys Touch is used when you tap using a touch screen These two are independent of each other It is a major UI design issue to be able to handle both cleanly Usually, focuses on Touch since many phones no longer have arrow/fire keys
Example When interaction with a ListView you can do the following Use the arrow keys to move around and use the fire key to select the row Tap the ListView and row directly For now, we will focus on the use of Touch
Copyright© Jeffrey Jongko, Ateneo de Manila University Context Sensitive Menus
Context Sensitive Menus are menus whose contents are dependent on the selected item (the context) The Activity class is responsible for the creation of these menus and provides methods that need to be overridden (just like the Options menu)
General Steps To enable Context Menus you need to do the following Register which view needs context menus Override onCreateContextMenu() Override onContextItemSelected()
registerForContextMenu() For a view to have a context menu, it must be registered to the activity using registerForContextMenu(View) Any number of views can have context menus, just register them all
Example ListView lv = getListView(); lv.setTextFilterEnabled(true); // NOTE: REGISTER listview for context menu registerForContextMenu(lv);
onCreateContextMenu() Works similar to the onCreateOptionsMenu() Use a MenuInflater to initialize the menu based on an XML file Alternatively you can invoke the menu’s add() method to add options programatically E.g. for dynamic menus
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); // places the contents of the XML to the menu inflater.inflate(R.menu.context_menu, menu); }
Example XML <menu xmlns:android="
onContextItemSelected() To handle selections override onContextItemSelected(MenuItem item) Switch against the menuItem’s ID to determine which action was selected Make sure to propagate the call to the superclass should none of the cases handle the option
AdapterContextMenuInfo The MenuItem parameter contains a ContextMenuInfo object retrieved from using getMenuInfo() For a ListView, it returns an AdapterContextMenuInfo instance The main job of this class is to pass back the following target view – which specific view this menu is for position of the view within the targetView This will allow you to extract the specific object within the backing data id of the view within the targetView
public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); switch (item.getItemId()) { case R.id.delete: // delete the currently selected row // NOTE: trigger the changes sampleList.remove(info.position); adapter.notifyDataSetChanged(); return true; default: return super.onContextItemSelected(item); }
Extras Other menu features Submenus MenuGroups Checkable menus Shortcut keys Check out file:///C:/android-sdk- windows/docs/guide/topics/ui/menus.html