Deleting a note – introducing OnLongClick

As a final improvement to Note To Self and as an excuse to talk about a neat Android UI feature, we will add the ability to delete a note. Context actions are often chosen in Android apps by long clicking. Long clicking is when the user holds their finger on the device screen rather than simply tapping and removing their finger.

First, let's add a delete note method to the NoteAdapter class. Here is the code that removes a Note object from the array list and then asks the adapter to update itself and ListView:

public void deleteNote(int n){

  noteList.remove(n);
  notifyDataSetChanged();

}

Now, in onCreate, we can prepare ListView to accept long clicks and then use an anonymous class to listen and respond to ...

Get Android Programming for Beginners now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.