I am trying to learn gesture detection in android apps. But apparently, on using onFling method, I am getting nothing in the logcat. I have implemented GestureDetector.OnGestureListener on the main class. I saw the example of gesture detection from here: https://developer.android.com/training/gestures/detector.html#detect
package com.persi.fragmenttester;
import android.content.Intent;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener{
private GestureDetectorCompat mDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDetector=new GestureDetectorCompat(this,this);
mDetector.setOnDoubleTapListener(this);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d("App",e.toString());
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
Log.d("App",e.toString());
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d("App", "onFling: " + e1.toString() + e2.toString());
return true;
}
}
You need to override onTouchEvent()
@Override
public boolean onTouchEvent(MotionEvent event){
if (this.mDetector.onTouchEvent(event)) {
return true;
}
return super.onTouchEvent(event);
}
From the documentation:
To make it possible for your GestureDetector object to receive events, you override the View or Activity's onTouchEvent() method, and pass along all observed events to the detector instance.