Search code examples
androidmulti-touchontouch

Multi touch : Get Action depending on PointerId


My app handle one & two fingers touch. It's possible that the first finger is sending ACTION_MOVE action and the second ACTION_POINTER_UP in the same time.

So, how can get action for the first finger, and action for the second ?


Solution

  • So you have pointer indexes that you can use in your MotionEvent.case. You have different cases according to which finger is down.

    Also keep in mind no matter what the pointer index is, they are both tied to the MOVE case. UNLESS you have access to your pointer IDs in which case you can differentiate by ID.

    For example, ACTION_POINTER_1_DOWN will be associated to your second finger.

    Here is a super simple example:

    switch(action) {
        case (MotionEvent.ACTION_DOWN):
    
            break;
        case (MotionEvent.ACTION_POINTER_1_DOWN): // Second finger
    
            break;
        case (MotionEvent.ACTION_MOVE):
            if(pointer > 0) {
                x = motionEvent.getX(pointer);
                y = motionEvent.getY(pointer);
            }
            else {
                x = motionEvent.getX();
                y = motionEvent.getY();
            }
    
            break;
        case (MotionEvent.ACTION_UP):
            System.out.println("UP");
            break;
        case (MotionEvent.ACTION_POINTER_1_UP): // Second finger
            System.out.println("1 UP");
            break;
        }