Android: swipe screen to open another activity? Android: swipe screen to open another activity? android android

Android: swipe screen to open another activity?


I realise this is an old question but for anyone else wondering why the above code does not work it is because he has not set the OnTouchListener to a View object. This is why his swipe "event" is not being picked up, because nothing is listening for it.

He could add this line to set swipes on his image button (though you would probably want a better View object then this):

flora.setOnTouchListener(gestureListener);


Android Activity Swipe Detection

Create A Base Activity Class

public abstract class _SwipeActivityClass extends AppCompatActivity{    private static final int SWIPE_MIN_DISTANCE = 120;    private static final int SWIPE_MAX_OFF_PATH = 250;    private static final int SWIPE_THRESHOLD_VELOCITY = 200;    private GestureDetector gestureDetector;    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        gestureDetector = new GestureDetector( this, new SwipeDetector());    }    protected abstract void onSwipeRight();    protected abstract void onSwipeLeft();    public class SwipeDetector extends GestureDetector.SimpleOnGestureListener    {        @Override        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)        {            // Check movement along the Y-axis. If it exceeds SWIPE_MAX_OFF_PATH,            // then dismiss the swipe.            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)            {                return false;            }            //toast( "start = "+String.valueOf( e1.getX() )+" | end = "+String.valueOf( e2.getX() )  );            //from left to right            if( e2.getX() > e1.getX() )            {                if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)                {                    onSwipeRight();                    return true;                }            }            if( e1.getX() > e2.getX() )            {                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)                {                    onSwipeLeft();                    return true;                }            }            return false;        }    }    @Override    public boolean dispatchTouchEvent(MotionEvent ev)    {        // TouchEvent dispatcher.        if (gestureDetector != null)        {            if (gestureDetector.onTouchEvent(ev))            // If the gestureDetector handles the event, a swipe has been            // executed and no more needs to be done.            return true;        }        return super.dispatchTouchEvent(ev);    }    @Override    public boolean onTouchEvent(MotionEvent event)    {        return gestureDetector.onTouchEvent(event);    }}

Then extend your Activity from _SwipeActivityClass

implement methods onSwipeLeft() and onSwipeRight() to start another activity