Friday, March 8, 2013

listening swipe event for any view-Android

"Android Code Snippet":

listening swipe event for any view

First Let me introduce you to the class that will be responsible for listening for swipe event.


import android.app.Activity;
import android.util.Log;
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 = new GestureDetector(
new GestureListener());

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

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() {
}


///////////////////////////////////////////////////////


public class ActivitySwipeDetector implements OnTouchListener {

private Activity activity;
static final int MIN_DISTANCE = 100;
private float downX, downY, upX, upY;

public ActivitySwipeDetector(final Activity activity) {
this.activity = activity;
}

public final void onRightToLeftSwipe() {
Log.i("RightToLeftSwipe!", null);
}

public void onLeftToRightSwipe() {
Log.i("LeftToRightSwipe!", null);
}

public void onTopToBottomSwipe() {
Log.i("onTopToBottomSwipe!", null);
}

public void onBottomToTopSwipe() {
Log.i("onBottomToTopSwipe!", null);
}

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.onLeftToRightSwipe();
return true;
}
if (deltaX > 0) {
this.onRightToLeftSwipe();
return true;
}
} else {
Log.i("Swipe was only " + Math.abs(deltaX)
+ " long, need at least " + MIN_DISTANCE, null);
}

// swipe vertical?
if (Math.abs(deltaY) > MIN_DISTANCE) {
// top or down
if (deltaY < 0) {
this.onTopToBottomSwipe();
return true;
}
if (deltaY > 0) {
this.onBottomToTopSwipe();
return true;
}
} else {
Log.i("Swipe was only " + Math.abs(deltaX)
+ " long, need at least " + MIN_DISTANCE, null);
}

// return true;
}
}
return false;
}

// public void rihjtToLeft() {
// Log.e("swipping ", "left from width");
//
// }
//
// public void LeftTorihjt() {
//
// }


public void onSwipeRight() {
}

public void onSwipeLeft() {
}
}



}

Now clearly this is the class we are going to use for listening any swipe event.For that we need to attach this listener to any view that we need swipe event from.

For me when i wanted to listen swipe from entire screen , so here is what i did:

        MyActivity.this.getWindow().getDecorView().setOnTouchListener(new OnSwipeTouchListener() {

   public void onSwipeRight() {
   // ScoreGroup.group.onBackPressed();
    //Log.e("No left View ","left");
    Toast.makeText(AntlerMetrixActivity.this, "No left View ", Toast.LENGTH_SHORT).show();
   }
   public void onSwipeLeft() {
    Log.e("swipping ","left");
    next();
   }

});

But if you want to listen swipe from any button or any kind of other view just replace         MyActivity.this.getWindow().getDecorView() with the instance of your view.

When using this swipe listener you need to be careful in some cases. Those view who already have scrolling effect with touch. Bcz in that case if you use this one with say for example with ScrollView this definitely will conflict.For those view one work around can be is to extend that view[ScrollView/ListView] override their touch handling and do some coding in there so that when swipe it does not conflit with the existing touch handling mechanism of ScrollView/... views.

Hope this helps anyone out there.


No comments:

Post a Comment