In standard, the Android View class support touch events. It lets you to make applications that react to touch events in your activities. Even better, Android supports multi-touch in standard via the Android View class. To be more concrete, a multi-touch gesture is when a user touches the screen with multiple fingers at the same time. In the documentation, each finger touching the screen is called a pointer. In this tutorial, you’re going to learn how to handle each of these pointers.
img_framed
To react to touch events, the View class has a onTouchEvent() method that must be overrided. This method has a parameter of type MotionEvent. The MotionEvent class contains all information related to a touch event : the number of pointers, the coordinates (x and y), size and pressure of each pointer detected. The onTouchEvent() method must return true if the touch event has been handled.
A MotionEvent has an action associated which is given by the system. You can access this action via the getActionMasked() method. It can return one of the following constants :
  • ACTION_DOWN : for the first pointer touching the screen.
  • ACTION_POINTER_DOWN : for extra pointers touching the screen.
  • ACTION_MOVE : notifies that a change has happened during a press gesture.
  • ACTION_POINTER_UP : notifies that a non-primary pointer goes up.
  • ACTION_UP : notifies when the last pointer leaves the screen.
To get information associated to each pointer within a MotionEvent you can identify them via a pointer’s index and ID :
  • Index : internally, a MotionEvent object stores information about each pointer in an array. The index lets you get information associated to a particular pointer. Most of operations use the index rather the ID.
  • ID : each pointer has also an ID staying persistent across touch events to allow tracking an individual pointer across a complete gesture.
Note that the order in which individual pointers appear in a MotionEvent can change between each event whereas the ID of a pointer remains constant during the same gesture.
So, to handle multi-touch on a View descendant, you will need to use the following code :

public boolean onTouchEvent(MotionEvent event) {

   int action = MotionEventCompat.getActionMasked(event);
   String strAction = "";

   switch (action) {
      case MotionEvent.ACTION_DOWN: strAction = "Down"; break;
      case MotionEvent.ACTION_MOVE: strAction = "Move"; break;
      case MotionEvent.ACTION_POINTER_DOWN: strAction = "Pointer Down"; break;
      case MotionEvent.ACTION_UP: strAction = "Up"; break;
      case MotionEvent.ACTION_POINTER_UP: strAction = "Pointer Up"; break;
      case MotionEvent.ACTION_OUTSIDE: strAction = "Outside"; break;
      case MotionEvent.ACTION_CANCEL: strAction = "Cancel"; break;
   }

   Log.i("SSAUREL", "The action is : " + strAction);

   int index = MotionEventCompat.getActionIndex(event);
   int xPos = -1;
   int yPos = -1;

   if (event.getPointerCount() > 1) {
      Log.i("SSAUREL","Multi-Touch event");
   } else {
      Log.i("SSAUREL","Single Touch event");
   }

   // get coordinates
   xPos = (int) MotionEventCompat.getX(event, index);
   yPos = (int) MotionEventCompat.getY(event, index);

   // ... make some operations ...

   return true;
}
In this code, we use the MotionEventCompat class, which is part of the Support Library. It’s better for some compatibility reasons. First, you get the action type associated to the current event. Then, you get the index of the MotionEvent. Finally, you have just to use the index get x and y coordinates of the pointer associated to the current event on the screen.
Like you can see, it’s really simple to handle multi-touch gestures in Android. So, don’t hesitate to implement it in your application.