Android Development – Detecting Common Gestures

Gestures allow users to interact with an app via touch gestures

Detecting Common Gestures


In this Post:

How touch gesture occurs?

Two phases of gesture detection

Support Library Classes

Gather Data about touch events

Capturing touch events (Custom gestures processing)

Capturing touch events for a single view

Detecting Gestures

Detecting all supported Gestures

Detecting a subset of supported Gestures

Caution

Useful resources


  • How touch gesture occurs on an Android phone?
    • When a user places one or more fingers on the screen, plus
    • Your application interprets that pattern of touches as a particular gesture
  • Two phases of gesture detection
    • Gathering data about touch event(s)
    • Interpreting the data to see if it meets the criteria for any of the gestures your app
      supports
  • Support Library Classes (in the Support Library)
    • Use Support Library classes where possible to provide compatibility with devices running Android 1.6 and higher
    • Examples below will use these classes:
      • GestureDetectorCompat
      • MotionEventCompat
        • Is not a replacement for MotionEvent class
        • Rather provides static utility methods to which you pass MotionEvent object in order to receive the desired action associated with that event

Gathering data about touch events

  • When user places one or more finger on the screen, the onTouchEvent() callback is triggered on the View that received the touch events
  • For each sequence of touch events (position, pressure, size, addition of another finger) that is ultimately identified as a gesture, onTouchEvent() is fired several times
  • Throughout a single interaction (when the user first touches the screen until leaving the screen), the MotionEvent
    delivered to onTouchEvent() provides the details of every interaction.

 

Capturing touch events for an Activity or View (Custom gestures)

  • A kind of processing you would have to do for custom gesture
  • Intercept touch events in an Activity or View, by overriding onTouchEvent() callback
  • Use getActionMasked() to extract the action the user performed from the event parameter.
    This gives you the raw data you need to determine if a gesture you care about occurred

 

Snippet

 

public class MainActivity extends Activity {

//
This example shows an Activity, but you would use the same approach if

//you were subclassing a View.

@Override

public boolean onTouchEvent(MotionEvent event){

      

   int action = MotionEventCompat.getActionMasked(event);

      

   switch(action) {

       case (MotionEvent.ACTION_DOWN) :

           Log.d(DEBUG_TAG,“Action was DOWN”);

           return true;

       case (MotionEvent.ACTION_MOVE) :

           Log.d(DEBUG_TAG,“Action was MOVE”);

           return true;

       case (MotionEvent.ACTION_UP) :

           Log.d(DEBUG_TAG,“Action was UP”);

           return true;

       case (MotionEvent.ACTION_CANCEL) :

           Log.d(DEBUG_TAG,“Action was CANCEL”);

           return true;

       case (MotionEvent.ACTION_OUTSIDE) :

           Log.d(DEBUG_TAG,“Movement occurred outside bounds of current screen element”);

           return true;

       default :

           return super.onTouchEvent(event);

   }

}

 

  • Do your own processing to determine if a gesture occurred
  • If your app uses common gestures you (such as double tap, long press and fling) you can take advantage of GestureDetector class
  • GestureDetector makes it easy for you to detect common gestures without processing individual touch events

 

Capturing touch events for a single view

  • As an alternative to onTouchEvent() you can attach View.OnTouchListener object to any view object using the
    setOnTouchListener() method
  • This makes it possible to listen for touch events without subclassing an existing View

 

Snippet  

 

View
myView
= findViewById(R.id.my_view);

myView.setOnTouchListener(new OnTouchListener() {

   public boolean
onTouch
(View v, MotionEvent event) {

       //
… Respond to touch events      

       return true;

   }

});

 

Beware

  • ACTION_DOWN event is the starting point for all the touch events
  • So creating a listener that returns false for the ACTION_DOWN event will stop the calling for
    subsequent ACTION_MOVE and ACTION_UP events

 

Detecting Gestures

  • Use GestureDetector class for detecting common gestures – onDown(), onLongPress(), onFling() and so on
  • Use GestureDetector in conjunction with onTouchEvent() method described above

 

Detecting all supported Gestures

  • GestureDetectorCompat object takes a class, that implements the GestureDetector.OnGestureListener interface, as a parameter
  • GestureDetector.OnGestureListener notifies users when a particular touch event has occurred
  • To enable your GestureDetector object to receive events, override the View or Activity’s onTouchEvent() method, and pass along all observed events to the detector instance.
  • In the following snippet, a return value of true from the individual on<TouchEvent> methods indicates that you have handled the touch event.
  • A return value of false passes events down through the view stack until the touch has been successfully handled
  • Run the following snippet to watch
    • Actions that are triggered when you interact with the touch screen
    • Contents of the MotionEvent for each touch event for even simple interactions

 

Snippet

 

public class MainActivity extends Activity implements

       GestureDetector.OnGestureListener,

       GestureDetector.OnDoubleTapListener{

  

   private static final String DEBUG_TAG = “Gestures”;

   private GestureDetectorCompat mDetector;

 

   // Called when the activity is first created.

   @Override

   public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

       // Instantiate the gesture detector with the

       // application context and an implementation of

       // GestureDetector.OnGestureListener

       mDetector = new GestureDetectorCompat(this,this);

       // Set the gesture detector as the double tap

       // listener.

       mDetector.setOnDoubleTapListener(this);

   }

 

   @Override

   public boolean onTouchEvent(MotionEvent event){

       this.mDetector.onTouchEvent(event);

       // Be sure to call the superclass implementation

       return super.onTouchEvent(event);

   }

 

   @Override

   public boolean
onDown
(MotionEvent event) {

       Log.d(DEBUG_TAG,“onDown: “ + event.toString());

       return true;

   }

 

   @Override

   public boolean
onFling
(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {

       Log.d(DEBUG_TAG, “onFling: “ + event1.toString()+event2.toString());

       return true;

   }

 

   @Override

   public void
onLongPress
(MotionEvent event) {

       Log.d(DEBUG_TAG, “onLongPress: “ + event.toString());

   }

 

   @Override

   public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,

           float distanceY) {

       Log.d(DEBUG_TAG, “onScroll: “ + e1.toString()+e2.toString());

       return true;

   }

 

   @Override

   public void onShowPress(MotionEvent event) {

       Log.d(DEBUG_TAG, “onShowPress: “ + event.toString());

   }

 

   @Override

   public boolean onSingleTapUp(MotionEvent event) {

       Log.d(DEBUG_TAG, “onSingleTapUp: “ + event.toString());

       return true;

   }

 

   @Override

   public boolean onDoubleTap(MotionEvent event) {

       Log.d(DEBUG_TAG, “onDoubleTap: “ + event.toString());

       return true;

   }

 

   @Override

   public boolean
onDoubleTapEvent
(MotionEvent event) {

       Log.d(DEBUG_TAG, “onDoubleTapEvent: “ + event.toString());

       return true;

   }

 

   @Override

   public boolean
onSingleTapConfirmed
(MotionEvent event) {

       Log.d(DEBUG_TAG, “onSingleTapConfirmed: “ + event.toString());

       return true;

   }

}

 

Detecting a Subset of Supported Gestures

  • If you only want to process a few gestures, you can extend GestureDetector.SimpleOnGestureListener
    interface
  • SimpleOnGestureListener provides an implementation for all of the on<TouchEvent> methods by returning
    false
    for all of them. You can override only the methods you care about
  • The example below creates a class that extends GestureDetector.SimpleOnGestureListener and overrides
    onFling() and onDown()

 

Snippet

 

public class MainActivity extends Activity {

  

   private GestureDetectorCompat
mDetector
;

 

   @Override

   public void
onCreate
(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

       mDetector = new GestureDetectorCompat(this, new MyGestureListener());

   }

 

   @Override

   public boolean onTouchEvent(MotionEvent event){

       this.mDetector.onTouchEvent(event);

       return super.onTouchEvent(event);

   }

  

   class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

       private static final String DEBUG_TAG = “Gestures”;

      

       @Override

       public boolean onDown(MotionEvent event) {

           Log.d(DEBUG_TAG,“onDown: “ + event.toString());

           return true;

       }

 

       @Override

       public boolean onFling(MotionEvent event1, MotionEvent event2float velocityX, 

float velocityY) {

           Log.d(DEBUG_TAG, “onFling: “ + event1.toString()+event2.toString());

           return true;

       }

   }

}

 

Caution

  • App should not depend on touch gestures for basic behaviors because gestures may not be available to all the users in all contexts
  • Follow accepted android conventions for touch gestures

 

Useful resources

 

Reference

http://developer.android.com/training/gestures/detector.html

 

Training Videos found on YouTube

Advertisements
Tagged with: ,
Posted in Android Development

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: