Listening for click and long-press events

Almost every application needs to recognize and respond to basic events such as clicks and long-presses. It's so basic, in most of the recipes, we use the XML onClick attribute, but the more advanced listeners require setting up through code.

Android provides an Event Listener interface for receiving a single notification when certain actions occur, as shown in the following list:

  • onClick(): It's called when a View is pressed
  • onLongClick(): It's called when the View is long-pressed
  • onFocusChange(): It's called when the user navigates to or from the View
  • onKey(): It's called when a hardware key is pressed or released
  • onTouch(): It's called when a touch event occurs

This recipe will demonstrate responding to the ...

Get Android Application Development Cookbook - Second Edition 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.