Android - swipe left|right . gesture detection

cornelyus picture cornelyus · Mar 26, 2011 · Viewed 17.8k times · Source

was trying to add this "gesture" function to my first program, and almost every search I did came to this thread:

Fling gesture detection on grid layout

I was able to get it working.. but in my case, I am not sure if it's 100 % correct.

In my layout, I have 3 horizontal linearLayouts, each one has 5 buttons.. so it's 3 columns of 5 buttons.. according to the thread I had to:

"Attach your gesture listener to all the views you add to the main layout;"

that means for all the 15 buttons:

"button.setOnTouchListener(gestureListener);"

Couldn't I just say that the LinearLayouts with the buttons are the ones looking out for the swipe gestures? Or because they are "hidden" on the back of the buttons, this can't be done?

Because I have another layout I want to implement with this, and it has even more buttons..

So .. just looking for a simpler way of detecting on my screen for swipe right | left .. if it does exists..

Thank you in advance

Answer

Squonk picture Squonk · Mar 26, 2011

Couldn't I just say that the LinearLayouts with the buttons are the ones looking out for the swipe gestures? Or because they are "hidden" on the back of the buttons, this can't be done?

I think that's precisely the problem you're going to face unless you detect the swipe on all visual objects.

The more buttons (or other widgets) on the screen, the more accurate a user will have to be when they start/end the swipe gesture - in other words they'd need to start/end both in gaps between your buttons.

To detect a swipe requires start and end locations - if the user starts and ends on buttons (which aren't listening for the swipe) then a swipe won't be detected. Similarly, starting on a button and ending in a 'gap' (or vice versa) won't detect a valid swipe either because only one of the start/end points is valid for 'swipe detection'.

The best approach I can think of (although I've never tried myself) is to use a GestureOverlayView. This allows you to have a transparent overlay on top of your background view(s) and can be used to detect things like 'swipe' without your underlying views needing to know about gestures.

See this in the developers resources Gestures