0

i'm writing an Android app for a school project that performs a different action depending on how many fingers the user taps the screen with.

Right now this action is just to display the number of pointers detected as a toast.

I'm using the getPointerCount() method, but for some reason, I get multiple toasts. For example, a three finger tap gives me toasts for two and three finger taps, a four finger tap will give me toasts for two, three and four finger taps, and so on.

I cannot for the life of me figure out why this is. A four finger tap should display ONE toast saying "4", not cycle through 2, 3 and 4! Any help would be greatly appreciated.

public boolean onTouchEvent(MotionEvent event)
{
    int action = event.getAction();

    switch(action & MotionEvent.ACTION_MASK)
    {
        case MotionEvent.ACTION_POINTER_DOWN:

            int count = event.getPointerCount();

            if (count == 2) {
                finish();
            }

            else if (count == 4) {
                Toast.makeText(this, String.valueOf(count), Toast.LENGTH_SHORT).show();
            }

            else if (count == 3) {
                Toast.makeText(this, String.valueOf(count), Toast.LENGTH_SHORT).show();
            }
            break;
    }
    return true;
}

P.S, I have tried moving the code outside of the switch statement, and bizarrely , this causes the toasts to count up AND down!

AVStark
  • 1
  • 1

1 Answers1

0

You're thinking of the taps as discrete events, but Android MotionEvents are delivered as a stream. In order to get an ACTION_POINTER_DOWN with three fingers, it is required that you get an action pointer down with two fingers first. (If you think about it, all three fingers do not go down at the exact same time, even if this was possible you wouldn't receive them that way).

If you log every motion event with

Log.i("test", event.toString());

You should be able to see the sequence of events you are receiving, and better understand how to deal with them.

Aaron Skomra
  • 230
  • 4
  • 7