Tuesday 6 October 2015

Gesture Detection in Android Part-1

What is a Gesture?


When you click a button or scroll a list, you basically perform some kind of gesture which is detected by the system as an event and system perform some action on that. Gestures allow users to interact with your app by manipulating the screen objects you provide.


Some common kind of gestures are:


1. Single click
2. Double click
3. Scroll
4. Long pressed
5. Zoom


What do android provide for Gesture Detection ?


Android framework provides a set of APIs which can be used to detect these kind of gestures in your application. All these gesture are performed by using action involving one or two fingers. GestuteDetector is the primary class which is used to detect the gestures. This class has two inner interface namely  OnDoubleTapListener for duble click gesture and OnGestureListener for gesture notification. This class has also an inner class SimpleOnGestureListener which is generally extended when you require only to detect a subset of gestures not all.


How to use GestureDetection API ?


1. First you need one of the below which will be used as argument for GestureDetection constructor.


  • Create a class which implements either OnDoubleTapListener or OnGestureListener.
  • Create a class which extends SimpleOnGestureListener.


2. Create an instance of GestureDetection for a view.  
 
3. Set the setOnTouchListener to the view and inside the OnTouch method of OnTouchListener, call onTouchEvent on the instance of GestureDetection.


Create a SimpleOnGestureListener class


A convenience class to extend when you only want to listen for a subset of all the gestures. This implements all methods in the GestureDetector.OnGestureListener and GestureDetector.OnDoubleTapListener but does nothing and return false for all applicable methods.This can be an inner class for the activity class or an independent class.


class MyGestureDetection extends GestureDetector.SimpleOnGestureListener{

@Override
public boolean onDown(MotionEvent e) {
// It must return true for any gesture to be detected.
return true;
}

@Override
public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
return super.onDoubleTap(e);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
return;
}

           // four more methods are here….you can check documentation for that.
}


Whichever the methods you don’t need return false for them. Suppose you need fling and not scroll then return false for scroll.


Create an instance of GestureDetection for a view.  


public class MainActivity extends Activity {

private GestureDetector mDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mDetector = new GestureDetector(this, new GestureDetection());
}
}


Set the setOnTouchListener to the view


public class MainActivity extends Activity {

private ImageView imageView;
private GestureDetector mDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageView = (ImageView) findViewById(R.id.imageView1);
mDetector = new GestureDetector(this, new GestureDetection());

imageView.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
mDetector.onTouchEvent(event);
return true;
}
});

}


This detector instance can be applied to any view and viewgroup. If you want to apply it on the whole display area of activity instead of a view, then override onTouchEvent of activity class. Inside this method, call onTouchEvent method on detector instance instead of a view.


Apply for the whole view area


public class MainActivity extends Activity {

private ImageView imageView;
private GestureDetector mDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDetector = new GestureDetector(this, new GestureDetection());
}

           @Override
public boolean onTouchEvent(MotionEvent event) {
mDetector.onTouchEvent(event);
return true;
}

}


In the next post, will will go through a demo application…..;)