Question

I am using the answer from Mohan at DoubleTap in android But I dont know How to implement it.

I have a webView and I want to detect swipe from left and right and double tap. How could I implement both at a time.Please help I am new to gestures.

Was it helpful?

Solution

Try this,

private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;

gestureDetector = new GestureDetector(new MyGestureDetector());
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }
    };

MyGestureDetector

class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            // right to left swipe
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

// your code

            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

// your code

            }
        } catch (Exception e) {
            Log.e(LOG_TAG, "onFling", e);

        }
        return false;
    }
}

By checking swipe distance, you can differentiate touch and swipe somehow. I hope this will help you.

OTHER TIPS

Here is an extended WebView that handles its own gestures. This may help those who find that the outer UI area is absorbing gestures and have trouble getting listeners in. That was my problem. Here is the WebView which detects all its own swipes and double-whatsits:

package org.this_voice.modabrowser;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.webkit.WebView;
import android.widget.Toast;

public class MoDaView extends WebView implements
    GestureDetector.OnDoubleTapListener {

Context mContext;
GestureDetector gestureDetector = new GestureDetector(
        new mGestureDetector());

public MoDaView(Context context) {
    super(context);
    mContext = context;
    gestureDetector.setOnDoubleTapListener(this);
    gestureDetector.setIsLongpressEnabled(true);
}

public MoDaView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mContext = context;
    gestureDetector.setOnDoubleTapListener(this);
    gestureDetector.setIsLongpressEnabled(true);
}

public MoDaView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    gestureDetector.setOnDoubleTapListener(this);
    gestureDetector.setIsLongpressEnabled(true);
}

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

class mGestureDetector extends SimpleOnGestureListener {

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
            float distanceX, float distanceY) {
        return super.onScroll(e1, e2, distanceX, distanceY);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
            return false;
         // right to left swipe
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(mContext, "Left Swipe", Toast.LENGTH_SHORT)
                    .show();
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(mContext, "Right Swipe", Toast.LENGTH_SHORT)
                    .show();
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }

}

public boolean onTouchEvent(MotionEvent event) {
    return gestureDetector.onTouchEvent(event) || super.onTouchEvent(event);
}

@Override
public boolean onDoubleTap(MotionEvent event) {
    Toast.makeText(mContext, "onDoubleTap", Toast.LENGTH_SHORT).show();
    return true;
}

@Override
public boolean onDoubleTapEvent(MotionEvent event) {
    Toast.makeText(mContext, "onDoubleTapEvent", Toast.LENGTH_SHORT).show();
    return true;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
    Toast.makeText(mContext, "onSingleTapConfirmed", Toast.LENGTH_SHORT)
        .show();
    return true;
}
}

And here is the setup in the class that uses the extended WebView:

wv = (MoDaView) findViewById(R.id.webview);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
WebSettings wvOptions = wv.getSettings();
wvOptions.setJavaScriptEnabled(true);
wvOptions.setGeolocationEnabled(false);
wvOptions.setBuiltInZoomControls(true);
wvOptions.setUseWideViewPort(true);

This is detecting all of the gestures above. I should add that this class combines about 6 different answers from stackoverflow.

One more thing, to save people what I went through. A doubletap is not a two-finger tap. I thought it was (long story, we'll skip it). A doubletap is one finger twice quickly. And two-finger is two at once and -- here's why I mention it -- NOT ALL PHONES CAN DETECT IT. I was going to use it but now I'll go with the actual double-tap. Android has two classes to "fake" two-finger gestures but they say those classes don't work everywhere. Just an FYI.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top