Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to apply swipe feature on imageView in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.11k
    Comment on it

    If you are looking to perform certain task on swiping on the imageView or other widgets in android (the task may be change of activity, or change of the image on imageview). Here is the code snippet that can help you out with this.

    Step 1:- Declare the variables

    private GestureDetector gdt;
    private static final int MIN_SWIPPING_DISTANCE = 50;
    private static final int THRESHOLD_VELOCITY = 50;
    

    Step:-2 Set touchListner on imageView:-

    gdt = new GestureDetector(new GestureListener());
    imageView.setOnTouchListener(new View.OnTouchListener() 
       {
           @Override
            public boolean onTouch(final View view, final MotionEvent event) {
            gdt.onTouchEvent(event);
            return true;
        } });
    

    Step:-3:- Create class GestureListener that extend GestureDetector.SimpleOnGestureListener

    private class GestureListener extends GestureDetector.SimpleOnGestureListener 
     {
       @Override
       public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
         {
           if (e1.getX() - e2.getX() > MIN_SWIPPING_DISTANCE && Math.abs(velocityX) > THRESHOLD_VELOCITY)
             {
               Toast.makeText(getApplicationContext(), "You have swipped left side", Toast.LENGTH_SHORT).show();
               /* Code that you want to do on swiping left side*/
               return false;
              } 
           else if (e2.getX() - e1.getX() > MIN_SWIPPING_DISTANCE && Math.abs(velocityX) > THRESHOLD_VELOCITY) 
             {
              Toast.makeText(getApplicationContext(), "You have swipped right side", Toast.LENGTH_SHORT).show();
              /* Code that you want to do on swiping right side*/
              return false; 
             } 
           return false;
          }
       }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: