8

I want to detect three finger tap in android screen.I am able to detect up to two fingers.How to detect three fingers it?I heard some where that android is capable of detecting 2 fingers.Is it so?

androidGuy
  • 5,553
  • 12
  • 39
  • 56

2 Answers2

12

This code will can help you:

public boolean onTouchEvent(MotionEvent event)
{
    int action = event.getAction();
    switch(action & MotionEvent.ACTION_MASK)
    {
        case MotionEvent.ACTION_POINTER_DOWN:
            // multitouch!! - touch down
            int count = event.getPointerCount(); // Number of 'fingers' in this time
            break;
    }
}
vicentazo
  • 1,739
  • 15
  • 19
  • 1
    I tried that but count is returning 2 even when i tap with three fingers – androidGuy Oct 20 '11 at 19:10
  • Do any android mobile is capable of detecting three finger tap?If so do u what are the mobiles capable of doing it? – androidGuy Oct 20 '11 at 19:14
  • Ok, I'm sorry. It's correct because getPointerCount() returns the number of POINTER events that they are events in multitouch. Therefore, if you want to count the number of fingers, you must add one – vicentazo Oct 20 '11 at 19:15
  • 1
    For one and two finger tap i am getting the pointer count correctly,So logic of adding 1 to the pointer count wont work always.I want to differentiate single finger,two finger and three finger tap. – androidGuy Oct 20 '11 at 19:19
1

I was asked to handle only three fingers tap. With the above solution if you tap 4 & 5 fingers and more you will get 3 fingers triggering inside onTouchEvent so I had to implement my own solution and since it is good I can share it.

so the idea is to create a CountDownTimer that will fire back an event whenever users finish tapping on the screen.

In this example, I defined numberOfFingers to 3 so I show a Toast if and only if three fingers were tapped on the screen.

You can change it and get whatever number of fingers you wish to track

First, create a new java class and name it FingerTapHandler

import android.os.CountDownTimer;

public class FingerTapHandler {

    private final int numberOfTrackingFingers;
    private int receivedNumOfFingers;
    private final OnTouchTimerListener touchTimerListener;

    public FingerTapHandler(int numberOfTrackingFingers, OnTouchTimerListener listener) {
        this.numberOfTrackingFingers = numberOfTrackingFingers;
        this.touchTimerListener = listener;
    }

    public void updateFingersCount(int fingers) {
        reset();
        this.receivedNumOfFingers = fingers;
    }

    private void reset() {
        onTouchTimer.cancel();
        onTouchTimer.start();
    }

    CountDownTimer onTouchTimer = new CountDownTimer(100, 100) {
        @Override
        public void onTick(long l) {
        }

        @Override
        public void onFinish() {
            if (receivedNumOfFingers == numberOfTrackingFingers) {
                touchTimerListener.handleOnFinish();
            }
        }
    };
}

And also we need an interface to handle the tap event: OnTouchTimerListener

public interface OnTouchTimerListener {
    void handleOnFinish();
}

Now, you can create a new instance from the above class and let your Activity tracks the wanted numbers of taps.

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements OnTouchTimerListener {

    private FingerTapHandler fingerTapHandler;
    private static final int numberOfFingers = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fingerTapHandler = new FingerTapHandler(numberOfFingers, this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        if ((action & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_DOWN) {
            int fingers = event.getPointerCount();
            fingerTapHandler.updateFingersCount(fingers);
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void handleOnFinish() {
        Toast.makeText(this, "Tapped on screen with " + numberOfFingers + " fingers", Toast.LENGTH_SHORT).show();
    }
}