Android Event Handling Tutorial

Android use event queue to manage events generated by system or device user. When an event occurred ( for example user click a button will trigger on click event ), the event will be placed in the event queue. All the events  in the queue comply with FIFO ( first in first out) principle.

1. How UI Component Consume Events?

When an event take place in a UI component area ( a View object ), if the view want to response to the event, it should first register an event listener to the view. This event listener will listen to the queue and when a monitored event happened on the view, it will execute the listener’s callback method to process that event.

Each listener’s callback method contains two input parameters. One is the android.view.View object specify which UI component trigger the event the other is a android.view.MotionEvent object which contains detail event information such as event action, coordinates etc.

okButton.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        // Get touch action.
        int action = motionEvent.getAction();
        return false;

In above example, onTouch method is just the callback method for OnTouchListener, the method contains a View and a MotionEvent input parameter.

But for some simple and clear event such as click event, the callback method do not need to include both the two input parameter, only the source View object is enough.

disableOkButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {

When an event listener callback method has consumed the event, it can return a Boolean value to tell android framework whether this listener has consumed the event or not.

  1. true : The listener has consumed the event and android OS will discard it.
  2. false : The listener has not consumed the event and android OS will pass the event to the next listener registered on this view.

2. Android Event Handling Example.

There is a textview in this example, it has registered two event listener, one is click event listener the other is long click event listener. When you long click it, it will display a Toast to say some message, because the onLongClick method return false, so android OS will pass the event to next listener to process. Then the onClick listener will process the event also. You can see two Toast pupup message displayed one after another.

If you can not watch the above video, you can see it on the youtube URL


3. Main Activity Java File.

package com.dev2qa.androideventhandle;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        setTitle(" - Android Consume Event Example.");

        // Get the TextView object.
        TextView textView = (TextView)findViewById(;

        // Register click listener to the textview.
        textView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // When you click this textview show a Toast popup message.
                Toast.makeText(getApplicationContext(), "Click event occurred.", Toast.LENGTH_LONG).show();

        // Register long click listener to the textview.
        textView.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View view) {
                // When long click this textview show a Toast popup message also.
                Toast.makeText(getApplicationContext(), "Long click event occurred.", Toast.LENGTH_LONG).show();

                // If return false then android OS will trigger onClick event because this listener do no consume this event.
                return false;

                // Return true to tell android OS this event has been consumed, android OS will pass the event to next listener(onClickListener)
                //return true;

4. Layout Xml File.



        android:text="Click Me Twice, One Short, One Long"

0 0 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x