2

I want to create simple swipe gestures between two activity ,i have searched a lot got something like below.But my doubt is how could i swipe the activity or view using swipe gesture!!

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) {
                    Toast.makeText(SelectFilterActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Toast.makeText(SelectFilterActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }

Hope anybody will help me out @Thanks!

  • When you run this and swipe it does it show any toast? – Abx Apr 05 '13 at 09:57
  • – Abhilash yes it is showing! –  Apr 05 '13 at 10:08
  • Ok,than just write dcode to remove an activity and show another activity in the place where you have put the toast msg – Abx Apr 05 '13 at 10:14
  • So you can use `viewpager`.can add `framents` on it.. – Abhijit Chakra Apr 05 '13 at 09:53
  • thanks fine..But using viewpager with fragment i will get continuously pages,My requirement is- I have one view if i swipe left another view will show as same as i right swipe!!am i clear!! –  Apr 05 '13 at 09:57
  • Have you tried with viewpager ..do you mean you want two pages in a singlescreen..here by using viewpager by left and right swipe you can show pages whats wrong in it. – Abhijit Chakra Apr 05 '13 at 10:02
  • Abhijit i have done it using view pager but getting continuous pages one after another using swipe.BUT in my case i dont need continuous swipe if i swipe left the the left view should open and swipe right the right view activity should show.. –  Apr 05 '13 at 10:07
  • Actually i am not cleara bout your requirement can you make a simple diagram of your requirement . – Abhijit Chakra Apr 05 '13 at 10:09

4 Answers4

6

Try this code now..

page = (LinearLayout) findViewById(R.id.flipper);

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    super.dispatchTouchEvent(event);
    return gestureDetector.onTouchEvent(event);
}

SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {

        float sensitvity = 50;
        if ((e1.getX() - e2.getX()) > sensitvity) {
            SwipeLeft();
        } else if ((e2.getX() - e1.getX()) > sensitvity) {
            SwipeRight();
        }

        return true;
    }

};

 GestureDetector gestureDetector = new GestureDetector(
        simpleOnGestureListener);



private void SwipeLeft() {

     page.setInAnimation(animFlipInForeward);
      page.setOutAnimation(animFlipOutForeward);
      page.showNext();

}


 private void SwipeRight() { 

     page.setInAnimation(animFlipInBackward);
     page.setOutAnimation(animFlipOutBackward);
     page.showPrevious();

}
Preet_Android
  • 2,332
  • 5
  • 25
  • 43
  • Check this link.. http://stackoverflow.com/questions/7461879/looking-for-android-viewflipper-example-with-multiple-webviews – Preet_Android Apr 05 '13 at 11:12
1
implement your activity with OnGestureListener

 yourview.setOnTouchListener(touchListener);

implement touchListener as shown below

 OnTouchListener touchListener = new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                gestureScanner.onTouchEvent(event);
                return false;
            }
        };      

onFling method for swipe.

  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(animFlipInPrevious);
                         viewFlipper.setOutAnimation(animFlipOutPrevious);
                         Globle.falg =false;
                    if (count != 2) {
                        if (count == 2) {
                            count = 0;
                        } else {
                            count = count + 1;
                        }
                        if (currentView == 0) {
                            viewFlipper.showNext();
                            currentView = 1;
                        } else if (currentView == 1) {
                            viewFlipper.showNext();
                            currentView = 2;
                        } else {
                            currentView = 0;
                            viewFlipper.showNext();
                        }
                    }
                    count = viewFlipper.getDisplayedChild();
                    } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE  && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                        viewFlipper.setInAnimation(animFlipInNext);
                        viewFlipper.setOutAnimation(animFlipOutNext);
                        Globle.falg =false;
                    if (count != 0) {
                        if (count == 0) {
                            count = 2;
                        } else {
                            count = count - 1;
                        }
                        if (currentView == 2) {
                            currentView = 1;
                            viewFlipper.showPrevious();
                        } else if (currentView == 1) {
                            currentView = 0;
                            viewFlipper.showPrevious();
                        } else {
                            currentView = 2;
                            viewFlipper.showPrevious();
                        }
                    }
                    count = viewFlipper.getDisplayedChild();
                }
                } catch (Exception e) {
                    // nothing
                }
                return false;
            }
        }

as above code you can change your view and here in my case there is three view but you need only two so change above count value. hop this is helpful to you.

kyogs
  • 6,766
  • 1
  • 34
  • 50
  • @priya2134412 edited code with some condition for view change as you need. – kyogs Apr 05 '13 at 10:08
  • moj ma ho i have a small doubt using this can i use other activity or only view from same layout!! –  Apr 05 '13 at 10:13
  • @priya2134412 yes...you can change other activity or only view from same layout!!. – kyogs Apr 05 '13 at 10:16
1
public class Main extends Activity implements OnGestureListener {

    private GestureDetector gDetector;
    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;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gDetector = new GestureDetector(this);
    }


    @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) {
                ((ImageView) findViewById(R.id.image_place_holder))
                        .setImageResource(R.drawable.down);

            }
            // left to right swipe
            else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                ((ImageView) findViewById(R.id.image_place_holder))
                        .setImageResource(R.drawable.up);

            }
        } catch (Exception e) {

        }
        return false;
    }

}
NRahman
  • 2,717
  • 5
  • 24
  • 29
0

see this link it might be helpfull to you.

gestures detector

or you have to use viewpager in your app.

for the viewpager the following link will help you

view-pager-example

kyogs
  • 6,766
  • 1
  • 34
  • 50
Preet_Android
  • 2,332
  • 5
  • 25
  • 43
  • Preet_Android thanks fine..But using Viewpager with fragment i will get continuously pages,My requirement is- I have one view if i swipe left another view will show as same as i right swipe !! Am i clear !! –  Apr 05 '13 at 10:01