RSS

Swipe gesture implementation in android

24 Jan

First way of making swipe gesture:

public class OnSwipeTouchListener implements OnTouchListener {

    private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());

    public boolean onTouch(final View view, final MotionEvent motionEvent) {
        return gestureDetector.onTouchEvent(motionEvent);
    }

    private final class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

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

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                    }
                } else {
                    if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            onSwipeBottom();
                        } else {
                            onSwipeTop();
                        }
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }

    public void onSwipeRight() {
    }

    public void onSwipeLeft() {
    }

    public void onSwipeTop() {
    }

    public void onSwipeBottom() {
    }
}

//call OnSwipeTouchListener to set gesture to particular control.
relParent.setOnTouchListener(new OnSwipeTouchListener() {
				public void onSwipeTop() {
					Toast.makeText(this, "top", Toast.LENGTH_SHORT).show();
				}
				public void onSwipeRight() {
					Toast.makeText(this, "right", Toast.LENGTH_SHORT).show();
				}
				public void onSwipeLeft() {
					Toast.makeText(this, "left", Toast.LENGTH_SHORT).show();
				}
				public void onSwipeBottom() {
					Toast.makeText(this, "bottom", Toast.LENGTH_SHORT).show();
				}
			});

Second way of implementing swipe gesture:

@Override
	public boolean onTouchEvent(MotionEvent touchevent) {
		//return super.onTouchEvent(event);
		switch (touchevent.getAction()){
		// when user first touches the screen we get x and y coordinate
		case MotionEvent.ACTION_DOWN: {
			x1 = touchevent.getX();
			y1 = touchevent.getY();
			break;
		}
		case MotionEvent.ACTION_UP: {
			x2 = touchevent.getX();
			y2 = touchevent.getY();

			//if left to right sweep event on screen
			if (x1 < x2) {				 				Toast.makeText(this, "Left to Right Swap Performed", Toast.LENGTH_LONG).show(); 			} 			// if right to left sweep event on screen 			if (x1 > x2){
				Toast.makeText(this, "Right to Left Swap Performed", Toast.LENGTH_LONG).show();
			}

			// if UP to Down sweep event on screen
			if (y1 < y2) { 				Toast.makeText(this, "UP to Down Swap Performed", Toast.LENGTH_LONG).show(); 			} 			//if Down to UP sweep event on screen 			if (y1 > y2){
				Toast.makeText(this, "Down to UP Swap Performed", Toast.LENGTH_LONG).show();
			}
			break;
		}
		}
		return false;
	}
Advertisements
 
Leave a comment

Posted by on January 24, 2014 in Android Source Code

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: