Android uses an event queue to manage events generated by the system or device user. When an event occurred ( for example when a user clicks a button it will trigger the on-click event ), the event will be placed in the event queue. All the events in the queue comply with the FIFO ( first in first out) principle.
1. How Do UI Component Consume Events?
- When an event takes place in a UI component area ( a View object ), if the view object wants to respond 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 specifies which UI component triggers the event, the other is an android.view.MotionEvent object which contains detailed 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 the above example, the onTouch method is just the callback method for the OnTouchListener, the method contains a View and a MotionEvent input parameter.
- But for some simple and clear events such as click events, the callback method does not need to include both the two input parameters, 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 the android framework whether this listener has consumed the event or not.
- true: The listener has consumed the event and android OS will discard it.
- 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.
If you can not watch the above video, you can see it on the youtube URL https://youtu.be/4aQrYolnujY
- There is a TextView object in this example, it has registered two event listeners, one is the click event listener the other is the long click event listener.
- When you long-click it, it will display a Toast to say some message, because the onLongClick method returns false, so the android OS will pass the event to the next listener to process.
- Then the onClick listener will process the event also. You can see two Toast popup messages displayed one after another.
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 the android OS will trigger onClick event because this listener does not consume this event. return false; // Return true to tell android OS this event has been consumed, android OS will pass the event to the 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>