Multitouch and gesture detection part 1


SDK Version: 
M3

In this little tutorial I’m going to show you how to detect multitouch event on an Activity screen.

First of all let’s create a class next to the Main activity that called MultitouchView. It extends View:

  1. public class MultitouchView extends View {
  2.  
  3.  
  4.  
  5.         public MultitouchView(Context context) {
  6.                 super(context);
  7.  
  8.         }
  9.  
  10. }

It has only one consturctor that you must implement. You have to add those two another constructors, otherwise the touch handling will not work:

  1.         public MultitouchView(Context context, AttributeSet attrs, int defStyle) {
  2.                 super(context, attrs, defStyle);
  3.  
  4.         }
  5.  
  6.         public MultitouchView(Context context, AttributeSet attrs) {
  7.                 super(context, attrs);
  8.  
  9.         }

Now let’s edit your main.xml. I’m going to add a new xml element that’s called
com.helloandroid.multitouchexample.MultitouchView. In this case com.helloandroid.multitouchexample is my application package name. MultitouchView is the class name of View that handle the touch events.
After this your main xml will look like this:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="vertical" >
  6.  
  7. <com.helloandroid.multitouchexample.MultitouchView
  8.         android:id="@+id/touchView"
  9.         android:layout_width="fill_parent"
  10.         android:layout_height="fill_parent" />
  11.  
  12. </LinearLayout>

We no need to modify the main activity of the application:

  1. public class Main extends Activity {
  2.         @Override
  3.         protected void onCreate(Bundle savedInstanceState) {
  4.                 super.onCreate(savedInstanceState);
  5.                 setContentView(R.layout.main);
  6.         }
  7. }

With the setContentView method you can set the main xml as your activity content layout.

Now go back to MultitouchView class and implement the method that catch the motionevents, called onTouchEvent.

  1. @Override
  2.         public boolean onTouchEvent(MotionEvent event) {
  3.                 super.onTouchEvent(event);
  4.  
  5.                
  6.                 int action = event.getAction() & MotionEvent.ACTION_MASK;
  7.        
  8.                 switch (action) {
  9.                 case MotionEvent.ACTION_DOWN: {
  10.                         Log.d("MultitouchExample","Action Down");
  11.                         break;
  12.                 }
  13.                 case MotionEvent.ACTION_MOVE: {
  14.                         Log.d("MultitouchExample","Action Move");
  15.                         break;
  16.                 }
  17.  
  18.                 case MotionEvent.ACTION_POINTER_DOWN: {
  19.                         Log.d("MultitouchExample","Pointer Down");
  20.                 break;
  21.                 }
  22.                 case MotionEvent.ACTION_POINTER_UP: {
  23.                         Log.d("MultitouchExample","Pointer up");
  24.  
  25.                         break;
  26.                 }
  27.                 case MotionEvent.ACTION_UP: {
  28.                         Log.d("Multitouch", "Action up");
  29.                
  30.                         break;
  31.                 }
  32.                 }
  33.  
  34.                 return true;
  35.         }

You can get the user action from the MotionEvent object. There are a lot of constant actions that you can use to implement your own algorythm with them.
ACTION_DOWN is called when the user’s first finger reaches the screen.
ACTION_MOVE is called when the user move his finger on screen.
ACTION_POINTER_DOWN is called when the user touches the screen with a second, third non-primary-finger. In the next tutorial we are going to use this action to handle the multi touch input.
When the user’s non primary finger leave the screen, ACTION_POINTER_UP is called.
At the time when the last finger leave the screen the ACTION_UP is called.

In my next tutorial I’m going to show you an example how to use System.currentTimeMillis() to detect swipe gesture with multitouch.