Listening for and handling click and long-press (a.k.a. long-click) events in an Android ListActivity is a a simple matter of defining setOnItemClickListener and setOnItemLongClickListener delegate methods in the ListView.

For individual list items to be clickable, you will want to let the ListActivity’s ListView handle the onItemClick and onItemLongClick events that Android fires when a user interacts with your application.

First, get your ListView from your ListActivity. The ListView will handle the click and long press Android events for your ListActivity.

ListView lv = getListView();

Next, provide a method to handle the click when users press and release a list item in the ListView.

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
        onListItemClick(v,pos,id);
    }
});

Define your method to handle the onItemClick. This accepts a View which is the individual UI element that was clicked. This View corresponds to your layout that you set in your Adapter for your ListActivity. The position corresponds to which list item was clicked in the ListView (starting with a 1-index).

protected void onListItemClick(View v, int pos, long id) {
    Log.i(TAG, "onListItemClick id=" + id);
}

Handling long press events is essentially the same as handling clicks except that you return a boolean which specifies whether Android should continue to propagate the click event.

lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
        return onLongListItemClick(v,pos,id);
    }
});

If your long-click method returns true then you are telling Android that you handled the event and nobody else should know about the long-press. If your method returns false, Android will still call other handlers such as your onItemClick handler. Long-press events in Android typically do not perform the same action as a regular press, so it may be best to return true to stop event propagation.

protected boolean onLongListItemClick(View v, int pos, long id) {
    Log.i(TAG, "onLongListItemClick id=" + id);
    return true;
}

Outside This Scope

This is just the beginning for handling clicks within Android ListActivities and ListViews. Below are topics not covered within the scope of this post.

  • Performing actions within your onListItemClick such as starting a new Activity and sending information about the item that was clicked
  • Creating a custom context menu during your onLongListItemClick method
  • In Android SDK 1.6+, there is an easier way to attach click listeners to Views
  • You can set individual click listeners for Views within each list item to perform different actions depending on where you click within the list item

Resources

I did not come up with this out of my own brainz. I have only compiled and annotated information I have found online. The following resources may provide additional, useful information.