07/08/2021 Android Touch Event Handling | Alexandria
MODULE
Android Touch Event Handling
Nawfal Ali
Updated 25 May 2020
In the previous weeks, we learn how to translate a touch on the screen into an action. In this week, we will learn how to intercept touch events in an Activity or View and execute the appropriate behaviour.
What is Gesture?
A gesture is simply a sequence of touch events. Each touch event comes with x and y coordinates. A gesture starts with the touch-down even, continues as the system tracks the position of the user’s nger(s), and end by the touch-up event.
Intercepting Touch Events
Touch events can be intercepted by a view through the overriding
of onTouchEvent() method or the registration of an onTouchListerner and the implementation of onTouch() callback method.
https://www.alexandriarepository.org/module/android-touch-event-handling/ 1/13
07/08/2021 Android Touch Event Handling | Alexandria
https://
2/13
Types of Events
There are several types the MotionEvent object can report and here are some of them:
MotionEvent.ACTION_DOWN: This event is generated when the rst touch on a view occurs.
MotionEvent.ACTION_UP: This event is generated when the touch is lifted from the screen.
MotionEvent.ACTION_MOVE: Any motion of the touch between the ACTION_DOWN and ACTION_UP events will be represented by this event.
Overriding onTouchEvent() method
If you want to listen to all the touch events that might occur on your activity (if none of the child views handle it), it is possible to implement onTouchEvent() callback method as shown in the following example.
1. publicclassMainActivityextendsAppCompatActivity{
2.
3. private static final String DEBUG_TAG = “WEEK10_TAG”;
What is MotionEvent?
Object used to report movement (mouse, pen, nger, trackball) events. Motion events may hold either absolute or relative movements and other data, depending on the type of device.
It is an object that is passed through to the onTouch() callback method. It is the key to obtaining information about motion events such as the location of the touch within the view, the type of the event, and others.
www.alexandriarepository.org/module/android-touch-event-handling/
07/08/2
https://www.alexandriarepository.org/module/android-touch-event-handling/ 3/13
021 Android Touch Event Handling | Alexandria
4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28.
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
}
@Override
public boolean onTouchEvent(MotionEvent event) { int action = event.getActionMasked(); switch(action) {
case (MotionEvent.ACTION_DOWN) : Log.d(DEBUG_TAG,”Action was DOWN”); return true;
case (MotionEvent.ACTION_MOVE) : Log.d(DEBUG_TAG,”Action was MOVE”); return true;
case (MotionEvent.ACTION_UP) : Log.d(DEBUG_TAG,”Action was UP”); return true;
default :
return false;
} }
}
The activity implements onTouchEvent() method at line@12. The input parameter to this callback method is of type MotionEvent. Using this input parameter, we can extract the type of the event as shown in live@13. The getActionMasked() method returns an integer number (constant) that represents the type of the current event. The action is then compared with pre-de ned constant values to determine the type of the current event as implemented by the switch case in lines@14-26.
07/08/2021 Android Touch Event Handling | Alexandria
https://
4/13
Registering onTouchListerner
This approach allows us to listen to a speci c view instead of the entire layout.
1. 2. 3. 4. 5.
6. 7. 8.
9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29.
publicclassMainActivityextendsAppCompatActivity{ private static final String DEBUG_TAG = “WEEK10_TAG”;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
View view=findViewById(R.id.my_layout); view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) { int action = event.getActionMasked(); switch(action) {
case (MotionEvent.ACTION_DOWN) : Log.d(DEBUG_TAG,”Action was DOWN”); return true;
case (MotionEvent.ACTION_MOVE) : Log.d(DEBUG_TAG,”Action was MOVE”); return true;
case (MotionEvent.ACTION_UP) : Log.d(DEBUG_TAG,”Action was UP”); return true;
default :
return false;
} }
}); }
www.alexandriarepository.org/module/android-touch-event-handling/
07/08/2
https://
021 Android Touch Event Handling | Alexandria
5/13
30.
31. }
As shown in the code above, the activity retrieves a reference to the layout that has id=my_layout at line@9. The code then registers a touch listener by calling setOnTouchListener() method and providing an anonymous instance of View.OnTouchListener() that implements onTouch() callback Event (line@12).
The callback method onTouch() accepts as input the MotionEvent object that provides the type of the event and its coordinates.
What does the return true statement indicate?
It indicates that you have handled the touch event.
What does the return false statement indicate?
It indicates that you have not handled the current event.
MotionEvent Position
In order to query the position of the current event, we must call getX() and getY() methods to retrieve the absolute coordinates relative to the View, that dispatched them.
1. 2. 3. 4. 5.
publicclassMainActivityextendsAppCompatActivity{ private static final String DEBUG_TAG = “WEEK10_TAG”; @Override
www.alexandriarepository.org/module/android-touch-event-handling/
07/08/2
https://www.alexandriarepository.org/module/android-touch-event-handling/ 6/13
021 Android Touch Event Handling | Alexandria
6. 7. 8.
9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19.
20. 21. 22.
23. 24. 25.
26. 27. 28. 29. 30.
31. 32. 33.
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
View view=findViewById(R.id.my_layout); view.setOnTouchListener(new View.OnTouchListener() {
y=”+y);
y=”+y);
y=”+y);
@Override
public boolean onTouch(View v, MotionEvent event) { int x=(int)event.getX();
int y=(int)event.getY();
int action = event.getActionMasked(); switch(action) {
case (MotionEvent.ACTION_DOWN) : Log.d(DEBUG_TAG,”Action was DOWN at x=”+x+ ” and
return true;
case (MotionEvent.ACTION_MOVE) :
Log.d(DEBUG_TAG,”Action was MOVE at x=”+x+ ” and
return true;
case (MotionEvent.ACTION_UP) :
Log.d(DEBUG_TAG,”Action was UP at x=”+x+ ” and
return true; default :
} }
}); }
}
return false;
07/08/2021 Android Touch Event Handling | Alexandria
https://
7/13
Lines 13 and 14 retrieve the x and y coordinates of the current event. The return type of both methods getX() and getY() is oat and this is due to sub- pixel accuracy.
Now, let’s deploy the code above into an emulator and test it.
1. D/WEEK10_TAG: Action was DOWN at x=334 and y=653
2. D/WEEK10_TAG: Action was UP at x=334 and y=653
and here is the log when some move events generated:
1. D/WEEK10_TAG: Action was DOWN at x=416 and y=431
2. D/WEEK10_TAG: Action was MOVE at x=416 and y=436
3. D/WEEK10_TAG: Action was MOVE at x=420 and y=440
4. D/WEEK10_TAG: Action was MOVE at x=420 and y=448
5. D/WEEK10_TAG: Action was MOVE at x=422 and y=451
6. D/WEEK10_TAG: Action was MOVE at x=422 and y=456
7. D/WEEK10_TAG: Action was MOVE at x=422 and y=458
8. D/WEEK10_TAG: Action was MOVE at x=422 and y=462
9. D/WEEK10_TAG: Action was MOVE at x=425 and y=460
10. D/WEEK10_TAG: Action was UP at x=425 and y=460
As you can see the rst and last events are ACTION_DOWN and ACTION_UP
respectively.
Is there a way to get the X and Y coordinates relative to the device screen?
YES. You have to use getRawX() and getRawY().
To test the two new methods, let’s listen to the touch events in a view in a layout.
1.
www.alexandriarepository.org/module/android-touch-event-handling/
07/08/2
https://
021 Android Touch Event Handling | Alexandria
2.
9.
10.
19.
20.
22.
28.
29.
39.
40.
52.
53.
65.
66.
77.
78.
89.
90.
07/08/2
https://
021 Android Touch Event Handling | Alexandria
11/13
100. and here is the activity controller:
1. publicclassMainActivityextendsAppCompatActivity{ 2.
3.
4. TextView actionType;
5. TextView getXY;
6. TextView getRawXY;
7.
8. @Override
9. protected void onCreate(Bundle savedInstanceState) {
10. super.onCreate(savedInstanceState);
11. setContentView(R.layout.activity_main);
12. View view=findViewById(R.id.frame_layout_id);
13. actionType=findViewById(R.id.action_type);
14. getXY=findViewById(R.id.get_x_y_id);
15. getRawXY=findViewById(R.id.get_raw_x_y_id);
16. view.setOnTouchListener(new View.OnTouchListener() {
17. @Override
18. public boolean onTouch(View v, MotionEvent event) {
19. int x=(int)event.getX();
20. int y=(int)event.getY();
21. int rawX=(int)event.getRawX();
22. int rawY=(int)event.getRawY();
23. getXY.setText(x+”,”+y);
24. getRawXY.setText(rawX+”,”+rawY);
25.
26. int action = event.getActionMasked();
27. switch(action) {
28. case (MotionEvent.ACTION_DOWN) :
29. actionType.setText(“Down”);
www.alexandriarepository.org/module/android-touch-event-handling/
07/08/2
https://www.alexandriarepository.org/module/android-touch-event-handling/ 12/13
021 Android Touch Event Handling | Alexandria
30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43.
return true;
case (MotionEvent.ACTION_MOVE) :
actionType.setText(“MOVE”);
return true;
case (MotionEvent.ACTION_UP) :
actionType.setText(“UP”);
return true; default :
return false;
Let’s test it:
} }
}); }
}
0:00 / 2:14
07/08/2021 Android Touch Event Handling | Alexandria
References:
Android Studio 3.5 Development Essentials – Java Edition https://developer.android.com/
Copyright © Monash University, unless otherwise stated. All Rights Reserved, except for individual components (or items) marked with their own licence restrictions
Copyright © 2021 Monash University, unless otherwise stated
Disclaimer and Copyright Privacy
Service Status
https://www.alexandriarepository.org/module/android-touch-event-handling/ 13/13