Step 2

Context menus should always be used when performing actions upon specific elements in the UI. When you register a View to a context menu, the context menu is revealed by performing a "long-click" on the UI component (press and hold the touchscreen or highlight and hold down the selection key for about two seconds).

First, let's create the context menu that will allow users to delete individual notes. Open the Notepadv2 class.

1. In order for each list item in the ListView to register for the context menu, we call registerForContextMenu() and pass it our ListView. So, at the very end of the onCreate() method add this line:

registerForContextMenu(getListView());

Because our Activity extends the ListActivity class, getListView() will return us the local ListView object for the Activity. Now, each list item in this ListView will activate the context menu.

2. Now fill in the onCreateContextMenu() method. This callback is similar to the other menu callback used for the options menu. Here, we add just one line, which will add a menu item to delete a note. Call menu.add() like so:

• Constructing a new Activity and adding it to the Android manifest

• Invoking another Activity asynchronously using startActivityForResult()

• Passing data between Activity in Bundle objects

• How to use a more advanced screen layout

• How to create a context menu

0 0

Post a comment