androidGuy
androidGuy

Reputation: 5643

How to identify three finger tap in android

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?

Upvotes: 8

Views: 10834

Answers (2)

Mohamad Ghaith Alzin
Mohamad Ghaith Alzin

Reputation: 979

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();
    }
}

Upvotes: 1

vicentazo
vicentazo

Reputation: 1799

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;
    }
}

Upvotes: 12

Related Questions