2

I have SwipeActivity. when I swipe from right to left, I want to go activity B and when swipe left to right go to activity C. but my code didn't work. please help me.

my SwipeActivity is:

public class SwipeActivity extends Activity {

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final GestureDetector gestureDetector = new GestureDetector(new GestureListener()); View mainview = (View) findViewById(R.id.mainView); // Set the touch listener for the main view to be our custom gesture listener mainview.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { return true; } return false; } }); } 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; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { boolean result = false; try { 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(); } } } } catch (Exception exception) { exception.printStackTrace(); } return result; } } public void onSwipeRight() { Intent in = new Intent(SwipeActivity.this,page1Activity.class); startActivity(in); } public void onSwipeLeft() { Intent in = new Intent(SwipeActivity.this,page2Activity.class); startActivity(in); } 

}

2
  • What behavior do you observe? If your application is crashing or you get an Exception, what does your logcat say? Commented Nov 15, 2012 at 15:05
  • Did you try stepping through with the debugger? I'd verify that the view you are setting the on touch listener to is actually firing on touch. Commented Nov 15, 2012 at 15:12

3 Answers 3

2

I think that you can use fragments, and a ViewPager that contains the fragment inside the activity.

Check this is example: http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/

I hope that it ll be usefull for you.

Sign up to request clarification or add additional context in comments.

Comments

1

You should use Android ViewPager for better UI experience and use Fragment instead of activity. you can save the more time using ViewPager instead of developing custom swipe. have a look on to following examples.

1. ViewPager Tab Indicator

2. ViewPager Circle Style

Thanks

Comments

0

Try using onTouch listener. use its onDown and on up events. take the coordinate in onDown and onUp then compare them. if xUp< xDown = leftswipe and the opposite for left swipe.(do not forget to adjust sensivity by adding a margin like : (if distance (upX,downX)>50))

here is a sample code :

yourView.setOnTouchListener(new OnTouchListener() { int xDown=0; @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if(event.getAction()==MotionEvent.ACTION_DOWN){ xDown= event.getRawX(); } if(event.getAction()==MotionEvent.ACTION_MOVE){ } if(event.getAction()==MotionEvent.ACTION_UP){ if(Math.abs(xDown-evet.getRawX())>50) if(xDown-evet.getRawX()>0){ //left } else{ //right } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.