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() {
    @Override
    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() {
    @Override
    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.
READ :   Android Force Logout Use Service And Broadcast Example

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.

android event handling tutorial

3. Main Activity Java File.

MainActivity.java

package com.dev2qa.androideventhandle;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

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

        // Register click listener to the textview.
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            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() {
            @Override
            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.

activity_main.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:id="@+id/consume_event_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me Twice, One Short, One Long"
        android:textSize="20dp"/>

</LinearLayout>

Leave a Reply

Your email address will not be published. Required fields are marked *

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