Android - basic gesture detection?

Thanks to Code Shogun who's code I adapted to my situation. Let your activity implement OnClickListener 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 savedInstanceState) { super.

OnCreate(savedInstanceState); /* ... */ // Gesture detection gestureDetector = new GestureDetector(new MyGestureDetector()); gestureListener = new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { return gestureDetector. OnTouchEvent(event); } }; } 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) { Toast. MakeText(SelectFilterActivity. This, "Left Swipe", Toast.

LENGTH_SHORT).show(); } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math. Abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { Toast. MakeText(SelectFilterActivity.

This, "Right Swipe", Toast. LENGTH_SHORT).show(); } } catch (Exception e) { // nothing } return false; } } Attach your gesture listener to all the views you add to the main layout; // Do this for each view added to the grid imageView. SetOnClickListener(SelectFilterActivity.

This); imageView. SetOnTouchListener(gestureListener); Watch in awe as your overridden methods are hit, both the onClick(View v) of the activity and the onFling of the gesture listener. Public void onClick(View v) { Filter f = (Filter) v.getTag(); FilterFullscreenActivity.

Show(this, input, f); } The post 'fling' dance is optional but encouraged. Hope this helps someone else! Gav.

24 Thank you for this code! It was very helpful. However, I ran into one very very frustrating catch while trying to get gestures working.In my SimpleOnGestureListener, I have to override onDown for any of my gestures to register.

It can just return true but I has to be defined. P. S: I don't know if its my api revision or my hardware, but i'm using 1.5 on a HTC Droid Eris.

– Cdsboy Dec 6 '09 at 22:35 And thank You Cdsboy! – Fedearne Mar 11 '10 at 14:10 2 I had to implement onDown as well. Really nice of Cdsboy for pointing this out!

– corgrath Feb 27 at 12:32 Thanks Cdsboy, this got me as well. – Karolis Mar 3 at 22:22 your link need right,can you modify the link or delete it. Thank you – pengwang Mar 16 at 7:44.

One of the answers above mentions handling different pixel density but suggests computing the swipe parameters by hand. It is worth noting that you can actually obtain scaled, reasonable values from the system using ViewConfiguration class: final ViewConfiguration vc = ViewConfiguration. Get(getContext()); final int swipeMinDistance = vc.

GetScaledTouchSlop(); final int swipeThresholdVelocity = vc. GetScaledMinimumFlingVelocity(); // (there is also vc. GetScaledMaximumFlingVelocity() one could check against) I noticed that using these values causes the "feel" of fling to be more consistent between the application and rest of system.

I wish I could vote you up 1000 times. This is awesome. – dfetter88 Jul 7 at 21:55 3 I use swipeMinDistance = vc.

GetScaledPagingTouchSlop() and swipeMaxOffPath = vc. GetScaledTouchSlop(). – Thomas Ahle Jul 13 at 12:05.

The swipe gesture detector code above is very useful! You may however wish to make this solution density agnostic by using the following relative values (REL_SWIPE) rather than the absolute values (SWIPE_) DisplayMetrics dm = getResources(). GetDisplayMetrics(); int REL_SWIPE_MIN_DISTANCE = (int)(SWIPE_MIN_DISTANCE * dm.

DensityDpi / 160.0f); int REL_SWIPE_MAX_OFF_PATH = (int)(SWIPE_MAX_OFF_PATH * dm. DensityDpi / 160.0f); int REL_SWIPE_THRESHOLD_VELOCITY = (int)(SWIPE_THRESHOLD_VELOCITY * dm. DensityDpi / 160.0f).

3 +1 for bringing this up. Note that DensityMetrics. DensityDpi was introduced in API 4.

For backward compatibility with API 1, use DensityMetrics. Density instead. This then changes the calculation to be just SWIPE_MIN_DISTANCE * dm.density.

– Thane Anthem Apr 4 at 11:06.

I do it a little different, and wrote an extra detector class that implements the View. OnTouchListener onCreateis simply add it to the lowest layout like this: ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this); lowestLayout = (RelativeLayout)this. FindViewById(R.id.

LowestLayout); lowestLayout. SetOnTouchListener(activitySwipeDetector); where id. LowestLayout is the id.Xxx for the view lowest in the layout hierarchy and lowestLayout is declared as a RelativeLayout And then there is the actual activity swipe detector class: public class ActivitySwipeDetector implements View.

OnTouchListener { static final String logTag = "ActivitySwipeDetector"; private Activity activity; static final int MIN_DISTANCE = 100; private float downX, downY, upX, upY; public ActivitySwipeDetector(Activity activity){ this. Activity = activity; } public void onRightToLeftSwipe(){ Log. I(logTag, "RightToLeftSwipe!

"); activity.doSomething(); } public void onLeftToRightSwipe(){ Log. I(logTag, "LeftToRightSwipe!"); activity.doSomething(); } public void onTopToBottomSwipe(){ Log. I(logTag, "onTopToBottomSwipe!

"); activity.doSomething(); } public void onBottomToTopSwipe(){ Log. I(logTag, "onBottomToTopSwipe! "); activity.doSomething(); } public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()){ case MotionEvent.

ACTION_DOWN: { downX = event.getX(); downY = event.getY(); return true; } case MotionEvent. ACTION_UP: { upX = event.getX(); upY = event.getY(); float deltaX = downX - upX; float deltaY = downY - upY; // swipe horizontal? If(Math.

Abs(deltaX) > MIN_DISTANCE){ // left or right if(deltaX 0) { this. OnRightToLeftSwipe(); return true; } } else { Log. I(logTag, "Swipe was only " + Math.

Abs(deltaX) + " long, need at least " + MIN_DISTANCE); } // swipe vertical? If(Math. Abs(deltaY) > MIN_DISTANCE){ // top or down if(deltaY 0) { this.

OnBottomToTopSwipe(); return true; } } else { Log. I(logTag, "Swipe was only " + Math. Abs(deltaX) + " long, need at least " + MIN_DISTANCE); } return true; } } return false; } } Works really good for me!

1 This actually made it much easier for me to apply gesture functionality, and required "less" wiring :D Thanks @Thomas – mcnemesis Sep 20 at 22:14 This looks like a neat utility class - but I think your four on...swipe() methods should be interfaces – Someone Somewhere Oct 28 at 17:28.

Also as a minor enhancement. The main reason for the try/catch block is that e1 could be null for the initial movement. In addition to the try/catch, include a test for null and return.

Similar to the following if (e1 == null || e2 == null) return false; try { ... } catch (Exception e) {} return false.

There is a lot of excellent information here. Unfortunately a lot of this fling-processing code is scattered around on various sites in various states of completion, even though one would think this is essential to many applications. I've taken the time to create a fling listener that verifies that the appropriate conditions are met.

I've added a page fling listener that adds more checks to ensure that flings meet the threshold for page flings. Both of these listeners allow you to easily restrict flings to the horizontal or vertical axis. You can see how it's used in a view for sliding images.

I acknowledge that the people here have done most of the research---I've just put it together into a usable library. These last few days represent my first stab at coding on Android; expect much more to come.

I've been battling to get 'fling' gesture detection working on my Android application today. Nothing has worked for me so far and I was hoping for some pointers. What I have is a 'GridLayout' that contains 9 ImageViews.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions