View

Tuesday 2 August 2011

How to use swipe in android?

Swipe is otherwise called  as a Fling. Its a touch free movement in android.
Before that we have set some SWIPE MIN,MAX Distance,Threshold_velosity and as well as path.
 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;
 View.OnTouchListener gestureListener;
 private Animation slideLeftIn;
 private Animation slideLeftOut;
 private Animation slideRightIn;
        private Animation slideRightOut;
        private ViewFlipper viewFlipper;
Next step is to write a code inside of the onCreate Method
        setContentView(R.layout.main);
        viewFlipper = (ViewFlipper)findViewById(R.id.flipper);
        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
        slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
        slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
       
        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
You will need to extend SimpleOnGestureListener to implement your own handling on swipe/fling action:

 class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                 viewFlipper.setInAnimation(slideLeftIn);
                    viewFlipper.setOutAnimation(slideLeftOut);
                 viewFlipper.showNext();
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                 viewFlipper.setInAnimation(slideRightIn);
                    viewFlipper.setOutAnimation(slideRightOut);
                 viewFlipper.showPrevious();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    }
At last, you need to make sure in your activity, you catch the gesture event by overriding onTouch() method:
 
@Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event))
         return true;
     else
      return false;
    }
Here i use viewflipper,two LinearLayouts and two Buttons in main.xml file you have to set as <ViewFlipper xmlns:android="http://......" like this

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/flipper"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
   
 <LinearLayout
  android:layout_width="fill_parent" android:layout_height="70dp" android:background="#E2A9F3">
  <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="30dp" android:layout_marginTop="10dp" android:text="Swipe Me" android:textSize="20dp" android:textColor="#DF0101"></TextView>
    </LinearLayout>  
    <LinearLayout
  android:layout_width="fill_parent" android:layout_height="60dp" android:background="#585858">
     <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Back"></Button>
     <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Next"></Button>
    </LinearLayout>  
</ViewFlipper>
At last thing u have to create a new folder and set name as anim or what u wish ,u give it.In that folder you have to  create a xml for Left_in,Left_out,Right_in and right_out and set the values.
name:slide_left_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="800"/>
</set>
name:slide_left_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="800"/>
</set>
name:slide_right_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="800"/>
</set>
name:slide_right_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="800"/>
</set>

Result:
 

2 comments: