Using Gesture Detector in android

Posted On // Leave a Comment
Gestures are those subtle motions to trigger interactions between the touch screen and the user. It lasts for the time between the first touch on the screen to the point when the last finger leaves the surface. We’re all familiar with it as it’s the most common way to communicate with apps. Some examples are scrolling in an app by swiping vertically/horizontally, pinch to zoom, long press to select and so on.
Android provides us with a class called GestureDetector using which we can detect common gestures like tapping down and up, swiping vertically and horizontally (fling), long and short press, double taps, etc. and attach listeners to them. Let’s see how that’s done.

Creating the GestureDetector and Detecting Swipe/Fling Direction

Now, we have to create a GestureDetector object and attach the listener objects to it which will intercept the gesture events. 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class CustomGestureDetector implements GestureDetector.OnGestureListener,
                                        GestureDetector.OnDoubleTapListener{
private TextView mGestureText;
private GestureDetector mGestureDetector;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gesture);
 mGestureDetector = new GestureDetectorCompat(this, new GestureDetector.OnGestureListener() {
            @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        mGestureText.setText("onSingleTapConfirmed");
        return true;
    }
 
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        mGestureText.setText("onDoubleTap");
        return true;
    }
 
    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        mGestureText.setText("onDoubleTapEvent");
        return true;
    }
 
    @Override
    public boolean onDown(MotionEvent e) {
        mGestureText.setText("onDown");
        return true;
    }
 
    @Override
    public void onShowPress(MotionEvent e) {
        mGestureText.setText("onShowPress");
    }
 
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        mGestureText.setText("onSingleTapUp");
        return true;
    }
 
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        mGestureText.setText("onScroll");
        return true;
    }
 
    @Override
    public void onLongPress(MotionEvent e) {
        mGestureText.setText("onLongPress");
    }

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

            @Override
            public void onShowPress(MotionEvent e) {
           }

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

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

            @Override
            public void onLongPress(MotionEvent e) {

            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                if (e1.getX() < e2.getX()) {
                    Toast.makeText(c, "Left to Right swipe performed", Toast.LENGTH_SHORT).show();
                }

                if (e1.getX() > e2.getX()) {
                    Toast.makeText(c, "Right to Left swipe performed", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });
}
Detecting the fling directions like up to down, down to up, left to right and right to left can be a requirement which is fairly easy to implement inside the onFling() method:
e1 MotionEvent object contains data regarding the first down motion event that started the fling, i.e., the first down interaction with the touch screen by the finger (pointer), whereas, the e2object contains data regarding the move motion event (end of gesture) that triggered onFling().getX/Y() is used to get the X and Y co-ordinates.

Implementing onTouchEvent()

Our gesture detectors won’t fire yet. This is because we arn’t intercepting the touch events and re-routing them to our gesture detectors. In order to do that we’ll have to override our Activity’sonTouchEvent() method and do the re-routing there like this:
1
2
3
4
5
6
@Override
public boolean onTouchEvent(MotionEvent event) {
    mGestureDetector.onTouchEvent(event);
 
    return super.onTouchEvent(event);
}
Wow, that was really easy! Now you should just go and start testing the code out by running the app on your physical device and making various gestures on the screen.
Note: If you want to capture the touch events on a particular view rather than the entire Activity, then we’ll need to attach a View.OnTouchListener object to the View object usingsetOnTouchListener from whose onTouch() method the re-routing to the gesture detectors will need to be done:
1
2
3
4
5
6
7
view.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, final MotionEvent event) {
        mGestureDetector.onTouchEvent(event);
        return true;
    }
});
There’s another way to do the same thing which is to subclass a View class and override it’sonTouchEvent() method to do the delegation but that’s a little complicated and messy.
The above was an edited extract from this site. They deserve all the credit. We're just trying to point you in the right direction.

0 comments :

Post a Comment