Android detect gesture

If you application has to detect common gestures such as long press, double tab, down, fling, scroll, and so son, this Android tip is useful to you. A gesture starts when the user places one or more fingers on the screen. It continues as the system tracks the position of the user's finger(s), and ends by capturing the final event of the user's fingers leaving the screen. The details of the user interaction with the screen are stored in the MotionEvent object that is passed to the onTouchEvent() method. Android provides the GestureDetector class for detecting common gestures.


To detect the gestures using the GestureDetector class, you have to create an object of the GestureDetectorCompat class. It takes two parameters: the current context, and an object of a class that implements the GestureDetector.OnGestureListener interface. GestureDetector.OnGestureListener notifies users when a particular touch event has occurred. You need to override the View or Activity's onTouchEvent() method, and pass along all observed events to the detector instance in order the GestureDetectorCompat object to receive events. To make it possible for the GestureDetectorCompat object to receive double tab event, you can register it with the GestureDetector.OnDoubleTapListener interface by calling the setOnDoubleTabListener() method.
Below is the complete code of the MainActivity class that uses the GestureDetector to detect common gestures of the user.

package com.example.androidexample;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.GestureDetectorCompat;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
private GestureDetectorCompat GDetector;
private Activity context;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
GDetector=new GestureDetectorCompat(this,new GestureDetecting());
GDetector.setOnDoubleTapListener(new TabDetecting());
}
public boolean onTouchEvent(MotionEvent event){
     this.GDetector.onTouchEvent(event);
     return super.onTouchEvent(event);
   }
private class GestureDetecting implements OnGestureListener{
public boolean onSingleTapUp(MotionEvent event) {
Toast.makeText(context,"onSingleTapUp:"+event.toString(), Toast.LENGTH_SHORT).show();
return true;
}
public boolean onScroll(MotionEvent event1, MotionEvent event2, float distanceX,
        float distanceY) {
Toast.makeText(context,"onFling:"+event1.toString()+","+event2.toString(), Toast.LENGTH_SHORT).show();
         return true;
   }
public void onLongPress(MotionEvent event) {
Toast.makeText(context,"onLongPress:"+event.toString(), Toast.LENGTH_SHORT).show();
   }
public boolean onFling(MotionEvent event1, MotionEvent event2,
        float velocityX, float velocityY) {
Toast.makeText(context,"onFling:"+event1.toString()+","+event2.toString(), Toast.LENGTH_SHORT).show();
     return true;
   }
public void onShowPress(MotionEvent event) {
Toast.makeText(context,"onShowPress:"+event.toString(), Toast.LENGTH_SHORT).show();
   }
public boolean onDown(MotionEvent event) {
Toast.makeText(context,"onDown:"+event.toString(), Toast.LENGTH_SHORT).show();
     return true;
   }


}
private class TabDetecting implements OnDoubleTapListener{
public boolean onSingleTapConfirmed(MotionEvent event) {
Toast.makeText(context,"onSingleTapConfirmed:"+event.toString(), Toast.LENGTH_SHORT).show();
   return true;
   }
public boolean onDoubleTap(MotionEvent event) {
Toast.makeText(context,"onDoubleTap:"+event.toString(), Toast.LENGTH_SHORT).show();
     return true;
   }
public boolean onDoubleTapEvent(MotionEvent event) {
Toast.makeText(context,"onDoubleTapEvent:"+event.toString(), Toast.LENGTH_SHORT).show();
       return true;
   }

}
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


Android-detect-gesture

Posted by: Dara | post date: 09-21-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.