Tal Angel
Tal Angel

Reputation: 1780

Detecting multi-touch on RelativeLayout

I want to run a method in my Activity, only if the user touched the screen using three fingers during 3 seconds. My Activity has only one RelativeLayout.

I'm using AtomicBoolean and one Thread, But I can't get my code to "know" when 3 fingers are touching the screen.

Here is part of my code:

public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                wantedTime = event.getPointerCount();
                T = new Thread() {
                    public void run() {
                        while (canThreadRun.get()) {

                            wantedTime - = 100;
                            UpdateText(timerText, wantedTime);
                            try {
                                Thread.sleep(100L);
                            } catch (InterruptedException e) {
                                return;
                            }
                        }
                    }
                };
                canThreadRun.set(true);
                T.start();

                break;
            case MotionEvent.ACTION_UP:
                canThreadRun.set(false);
                break;
            case MotionEvent.ACTION_MOVE:
                break;
        }
    return false;
}

Upvotes: 0

Views: 146

Answers (1)

Zamrony P. Juhara
Zamrony P. Juhara

Reputation: 5262

getPointerCount() method of MotionEvent will give you number of finger touching screen. If your device support multi touch it may return 1 or more, otherwise it returns 1.

But you store it in wantedTime variable and then you subtract it with 100, which does not make sense.

I think, what you need to do is

  • On ACTION_DOWN, wait until number of pointer equal to 3. store current time somewhere for example threeTouchStartTime, when it does.
  • on ACTION_MOVE, get current time and subtract it with threeTouchStartTime. Test if number of pointer is still 3 and elapsed time exceeds 3 seconds. When it does, then do things you want to do.

You may want to look at GestureDetector class.

Upvotes: 1

Related Questions