5 Steps for Beginners to Understand Event-Dispatching in Android

A simple yet comprehensive demo included

Li Sisters
6 min readMay 18, 2020

Introduction

Android Event-Dispatching is a fundamental concept that Android developers must understand. In Android development, Event-Dispatching is usually associated with View, ViewGroup, and Activity, forming a complete and complex mechanism. For beginners, it is very difficult to learn all knowledge related toEvent-Dispatching at one go. Therefore, this article is dedicated to introducing fundamental Event-Dispatching in Android to beginners before they dive in it more deeply.

1. What events are in Android?

When one or more fingers touch the screen of the phone, there are usually 4 types of events:

  • ACTION_DOWN: occurs when the screen is pressed, this means the start of a touch event
  • ACTION_UP: user’s finger raised, which means the end of a touch event
  • ACTION_MOVE: user’s finger presses the screen, and it moves by a distance that exceeds a certain threshold before being released. This event will be considered as ACTION_MOVE
  • ACTION_CANCEL: the event is cancelled (not caused by the user’s behavior)

Touch events in Android all start from ACTION_DOWN. But after ACTION_DOWN occurs, different follow-ups occur according to different gestures.

In a complete series of touch events, there are only one ACTION_DOWN and one ACTION_UP, but ACTION_MOVE may be none or multiple. We also need to know that if the touch event is intercepted, ACTION_UP may be 0. So the process may also be like the following:

  • ACTION_DOWN -> ACTION_UP;
  • ACTION_DOWN -> ACTION_MOVE *n -> ACTION_UP.
  • ACTION_DOWN -> .
  • ACTION_DOWN -> ACTION_MOVE *n.

We can use a flowchart to illustrate this process:

Events in the event flow
Events in the event flow (drew by myself)

2. What objects can deliver these events?

Answer: View, ViewGroup, Activity.

UI of Android is composed of Activity, ViewGroup, View and its derived classes. (Note that ViewGroup is actually a derived class of View.) Their roles are summarized as below:

3 objects that can deliver and handle events
3 objects that can deliver and handle events

In a page, the event is first transmitted to Activity, then to ViewGroup, and finally to View after a click event occurs.

A simple Page in Android
A simple Page in Android (done by myself, for illustration purpose)

It can be inferred from above that to fully understand the Android Event-Dispatching, it is essential to know:

  • What happens when the event is delivered to the Activity?
  • What happens when the event is delivered to the ViewGroup?
  • What happens when the event is delivered to the View?

3. Which methods control Event-Dispatching?

In Activity, View and ViewGroup, there are methods related to Event-Dispatching that can be overridden.

Activity:
public boolean dispatchTouchEvent(MotionEvent ev);
public boolean onTouchEvent(MotionEvent ev);
ViewGroup:
public boolean dispatchTouchEvent(MotionEvent ev);
public boolean onInterceptTouchEvent(MotionEvent ev);
public boolean onTouchEvent(MotionEvent ev);
View:
public boolean dispatchTouchEvent(MotionEvent ev);
public boolean onTouchEvent(MotionEvent ev);

We can see that there are three different methods related to Event-Dispatching, and they all return boolean types. So what are the roles of these three methods and the meaning of their output? We can see the comments of each method in the source code. After interpretation, the results obtained are compiled in the table below.

A picture summarizing all knowledge points

The table above contains the gist of Event-Dispatching in Android. Do read it carefully.

4. Demo of the complete process

After learning the theories, here is a simple demo of the complete Event-Dispatching process.

Let’s make a very simple page, just like the picture above named “A simple Page in Android”, which is displayed by Activity. In this page, the outer layer is a TestLinearLayout that extends LinearLayout, and the middle part is a TestView that extends View. We override the methods related to Event-Dispatching in Activity, TestLinearLayout and TestView, add the necessary log-output in it, and finally obtain the following files:

A TestActivity.java which use activity_test.xml
A TestLinearLayout.java
A TestView.java
put the TestView and TestLinearLayout together into activity_test.xml

Let’s click TestView and TestLinearLayout respectively, and look at the output in both cases:

when click the TestVIew

As you can see, when click the TestView, the ACTION_DOWN event passes TestActivity and TestLinearLayout in order, and finally reaches TestView. Since no View consumes this event, ACTION_DOWN reaches TestView and then passes back from the innermost layer to the outermost layer, and is eventually consumed by the onTouchEvent() of the outermost TestActivity. This is because neither TestLinearLayout nor TestView use the setOnClickListener(), i,e. there is no internal View consuming this event, so the event is eventually consumed by TestActivity. ACTION_UP is passed directly to TestActivity’s onTouchEvent() without TestLinearLayout and TestView, as ACTION_DOWN is ultimately consumed by TestActivity.

when click the TestLinearLayout

When clicking the TestLinearLayout, this event is only passed to TestLinearLayout, but not to TestView. Why haven’t the methods inside TestView been called this time? This is because the click event occurred outside the scope of TestView.

This is the simplest and most basic example of Event-Dispatching. If you fully understand the large form at the end of step 3, you will understand why these two examples are presented this way.

5. Make some changes and see what happens

Set a click event for TestView in TestActivity:

set the setOnClickListener() to TestView in TestActivity

In this case, what happens when you click TestView?

when click the TestView which use setOnClickListener()

It can be seen that in this case, both ACTION_DOWN and ACTION_UP are consumed by TestView.

Let’s continue making some changes and writing the return value of onInterceptTouchEvent() method in TestLinearLayout as TRUE.

the return value of onInterceptTouchEvent method in TestLinearLayout is TRUE

In this case, when clicking the TestView

click the TestView when the return value of onInterceptTouchEvent method in TestLinearLayout is TRUE

This is because when the return value of onInterceptTouchEvent() in TestLinearLayout is TRUE, the event is intercepted by TestLinearLayout and cannot be passed to TestView.

Summary

Hopefully, after reading through these 5 steps, you are now able to understand the event distribution in Android.

If you are a beginner, you are expected to understand what each line of code did in Steps 4 and 5, what kind of results these codes output, and the rationale behind these results. This is a simple yet comprehensive demo. If you are a more experienced developer, you should be able to fully understand the last large form in Step 3.

If you have any questions, please feel free to leave me a comment!

--

--

Li Sisters

Account shared between two of us | Android Developer + Data Enthusiast :D