Yesterday I spent several hours trying to get the onDoubleTap event in my custom View.
I Googled it and all I could find was how to do it through an Activity. However, I wanted a nice and clean solution, which would properly encapsulate everything within a class and also be reusable, and most importantly, NOT mess with my Activity.
It turns out, the solution is quite simple, so here it is, the
GestureView class for Android
which takes care of all gestures, including onFling, onLongPress, onDoubleTap, onSingleTapConfirmed and all other gestures available in the SimpleOnGestureListener. It encapsulates a GestureDetector.
package net.djape.android.DjapeDeveloper;
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;public class GestureView extends View implements OnClickListener, OnTouchListener {
private GestureDetector detector;public GestureView(Context context, AttributeSet attrs) {
super(context, attrs);
detector = null;
setOnTouchListener(this);
setOnClickListener(this);
}public void setDetector(SimpleOnGestureListener listener)
{
detector = new GestureDetector(listener);
}public boolean onTouch(View v, MotionEvent event) {
if(detector!=null)
return detector.onTouchEvent(event);
else
return false;
}public void onClick(View v) {
// TODO Auto-generated method stub//Nothing needs to be done here, but this method must remain there!
}
}
So, this is just an underlying class that extends View and all your custom View classes that need to handle these events should inherit (extend) from this class.
Here is how to use it:
package net.djape.android.DjapeDeveloper;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;public class MyView extends GestureView {
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);setDetector(new MyGestureDetector());
setFocusable(true);
setFocusableInTouchMode(true);
}class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
return super.onDoubleTap(e);
}@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return super.onSingleTapConfirmed(e);
}@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
super.onLongPress(e);
}@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return super.onFling(e1, e2, velocityX, velocityY);
}
}}
Basically, all you need to do is to define your event handlers in the MyGestureDetector class and call the setDetector method in the constructor. This is the cleanest way of handling the gesture events.





