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 recipes we use the XML onClick attribute, but more advanced listeners require to be set 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 a click event, as well as a long-press event.
Getting ready
Create a new project in Android Studio and call it PressEvents. Use the default Phone & Tablet options and select Empty Activity on the Add an Activity to Mobile dialog.
How to do it...
Setting up to receive...