UILongPressGestureRecognizer gets called twice when pressing down

UILongPressGestureRecognizer is a continuous event recognizer. You have to look at the state to see if this is the start, middle or end of the event and act accordingly. i.e. you can throw away all events after the start, or only look at movement as you need. From the Class Reference: Long-press gestures are continuous. … Read more

How to simulate a touch event in Android?

Valentin Rocher’s method works if you’ve extended your view, but if you’re using an event listener, use this: view.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { Toast toast = Toast.makeText( getApplicationContext(), “View touched”, Toast.LENGTH_LONG ); toast.show(); return true; } }); // Obtain MotionEvent object long downTime = SystemClock.uptimeMillis(); long eventTime = SystemClock.uptimeMillis() + … Read more

Draw on HTML5 Canvas using a mouse

Here is a working sample. <html> <script type=”text/javascript”> var canvas, ctx, flag = false, prevX = 0, currX = 0, prevY = 0, currY = 0, dot_flag = false; var x = “black”, y = 2; function init() { canvas = document.getElementById(‘can’); ctx = canvas.getContext(“2d”); w = canvas.width; h = canvas.height; canvas.addEventListener(“mousemove”, function (e) { … Read more

Android: How to handle right to left swipe gestures

OnSwipeTouchListener.java: import android.content.Context; import android.view.GestureDetector; import android.view.GestureDetector.SimpleOnGestureListener; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; public class OnSwipeTouchListener implements OnTouchListener { private final GestureDetector gestureDetector; public OnSwipeTouchListener (Context ctx){ gestureDetector = new GestureDetector(ctx, new GestureListener()); } @Override public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } private final class GestureListener extends SimpleOnGestureListener { private static final … Read more

Fling gesture detection on grid layout

Thanks to Code Shogun, whose code I adapted to my situation. Let your activity implementOnClickListener as usual: public class SelectFilterActivity extends Activity implements OnClickListener { 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; private GestureDetector gestureDetector; View.OnTouchListener gestureListener; @Override protected void onCreate(Bundle … Read more