Wednesday, June 26, 2013

Android GestureDetector Example

In android can listen to the gestures and events performed by user on screen.

To listen the Gestures in Android we need to do 3 things
1: Create Class GestureListener which should extends GestureDetector.SimpleOnGestureListener
2: Override s all the callback methods of GestureDetector.SimpleOnGestureListener
3:  Bind the gestureDetector to GestureListener

 GestureListener.java


class GestureListener extends GestureDetector.SimpleOnGestureListener
{
   
       static String currentGestureDetected;
      
      // Override s all the callback methods of GestureDetector.SimpleOnGestureListener
      @Override
      public boolean onSingleTapUp(MotionEvent ev) {
          currentGestureDetected=ev.toString();
      
        return true;
      }
      @Override
      public void onShowPress(MotionEvent ev) {
          currentGestureDetected=ev.toString();
       
      }
      @Override
      public void onLongPress(MotionEvent ev) {
          currentGestureDetected=ev.toString();
      
      }
      @Override
      public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
          currentGestureDetected=e1.toString()+ "  "+e2.toString();
     
        return true;
      }
      @Override
      public boolean onDown(MotionEvent ev) {
          currentGestureDetected=ev.toString();
       
        return true;
      }
      @Override
      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
          currentGestureDetected=e1.toString()+ "  "+e2.toString();
        return true;
      }
}


MainActivity.java

public class MainActivity extends Activity
{
          
            private GestureDetector mGestureDetector;
            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                   
                    // Bind the gestureDetector to GestureListener
                    mGestureDetector = new GestureDetector(this, new GestureListener());
            }

            // onTouch() method gets called each time you perform any touch event with screen
            @Override
            public boolean onTouchEvent(MotionEvent event)
            {
                //method onTouchEvent of GestureDetector class Analyzes the given motion event
                //and if applicable triggers the appropriate callbacks on the GestureDetector.OnGestureListener supplied.
                //Returns true if the GestureDetector.OnGestureListener consumed the event, else false
.
               
                boolean eventConsumed=mGestureDetector.onTouchEvent(event);
                    if (eventConsumed)
                    {
                        Toast.makeText(this,GestureListener.currentGestureDetected,Toast.LENGTH_LONG).show();
                        return true;
                    }
                    else
                        return false;
            }
}



6 comments:

  1. i want to detect face and fingers so for that if you have any suggetions then tell me please
    thank you

    ReplyDelete
  2. Nice code. :)
    I would use something different from toasts to output the events.
    toasts are so slow. :)

    ReplyDelete
  3. seems to be detecting multiple swipes for me instead of 1. ??

    ReplyDelete
  4. Hi, Please suggest me. How to enable gesture for specific layout?

    ReplyDelete
  5. Hi,
    Thanks for sharing the information with us it was very informative. https://hangup.in

    ReplyDelete