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 pressedonLongClick()
: It's called when the View is long-pressedonFocusChange()
: It's called when the user navigates to or from the ViewonKey()
: It's called when a hardware key is pressed or releasedonTouch()
: It's called when a touch event occurs
This recipe will demonstrate responding to the click event, as well as the long-press event.
Create a new project in Android Studio and call it: PressEvents
. Use the default Phone & Tablet options and select Empty Activity when prompted for the Activity Type.