Android : How to apply swipe to widget or layout in one particular direction in android below is my code

on Friday, September 5, 2014


In Main Activity RelativeLayout layoutOptionGet = (RelativeLayout)findViewById(R.id.layoutOptionGet); layoutOptionGet.setOnTouchListener(new OnSwipeTouchListener(this) { @Override public void onSwipeDown() { Toast.makeText(Main.this, "Down", Toast.LENGTH_SHORT).show(); }



@Override
public void onSwipeLeft() {
Toast.makeText(Main.this, "Left", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Main.this, GetActivity.class);
startActivity(intent);
finish();
}

@Override
public void onSwipeUp() {
Toast.makeText(Main.this, "Up", Toast.LENGTH_SHORT).show();
}

@Override
public void onSwipeRight() {
Toast.makeText(Main.this, "Right", Toast.LENGTH_SHORT).show();
}
});


OnSwipeTouchListener.class


public class OnSwipeTouchListener implements OnTouchListener {



private GestureDetector gestureDetector;

public OnSwipeTouchListener(Context c) {
gestureDetector = new GestureDetector(c, 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;
}

// Determines the fling velocity and then fires the appropriate swipe event accordingly
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
Log.e("onfling","onfling");
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) {
onSwipeDown();
} else {
onSwipeUp();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeUp() {
}

public void onSwipeDown() {
}


}


But any swipe action not working.Please send me the answer.


0 comments:

Post a Comment