Question

I am trying to add gesture in my preference screen but i am not getting any positive result , My Question is can we add gesture to preference screen and if yes then How I am using the below code to add gesture in preference (not working for preference but its working to switch between two Activity )

@Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub

        return gestureDetector.onTouchEvent(event);
    }

    SimpleOnGestureListener simpleOnGestureListener
    = new SimpleOnGestureListener(){


        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            String swipe = "";
            float sensitvity = 50;

            // TODO Auto-generated method stub
            if((e1.getX() - e2.getX()) > sensitvity){
                // for left
            Intent i=new Intent(getApplicationContext(),MyActivity.class);
                startActivity(i);
                finish();


            }else if((e2.getX() - e1.getX()) > sensitvity){
                //for right
            }else{
                swipe += "\n";
            }

            if((e1.getY() - e2.getY()) > sensitvity){
                //Swipe Up
            }else if((e2.getY() - e1.getY()) > sensitvity){
                //Swipe Down
            }



            return super.onFling(e1, e2, velocityX, velocityY);
        }
    };

    GestureDetector gestureDetector
    = new GestureDetector(simpleOnGestureListener);
Was it helpful?

Solution

first make your MainActivity implements OnGestureListener then define the gesture

    private GestureDetector gestureScanner;

add the gesture to your onCreate gestureScanner = new GestureDetector(this); and this is the methods for the gesture

@Override
public boolean onTouchEvent(final MotionEvent event)
{
    return gestureScanner.onTouchEvent(event);
}
public boolean onDown(final MotionEvent e)
{

    return true;
}
public boolean onFling(final MotionEvent e1, final MotionEvent e2,
        final float velocityX, final float velocityY)
{
    return true;
}
public void onLongPress(final MotionEvent e) {

}
public boolean onScroll(final MotionEvent e1, final MotionEvent e2,
        final float distanceX, final float distanceY)
{
    return true;
}
public void onShowPress(final MotionEvent e)
{

}
public boolean onSingleTapUp(final MotionEvent e)
{
    showHide(); // show hide the statusBar On Small Phones prefer to
    // keep it on Single Tab Cuz On Swipe Down is Kind laggy!!!
    // WTF !! :D
    return true;
}

now as you can see on my onSingleTapUp(final MotionEvent e) i'm using to show hide the Status bar.

you have six different ways of touch events "gesture events" choose one of them and put your intent on it and when the user "lets say" scrolls the other activity will launch .

public boolean onScroll(final MotionEvent e1, final MotionEvent e2,
        final float distanceX, final float distanceY)
{
        Intent i=new Intent(getApplicationContext(),MyActivity.class);
            startActivity(i);
            finish();

    return true;
}

give it a try and i hope it benefit you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top