package com.example.simplegesturedetector; import android.os.Bundle; import android.util.Log; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private GestureDetector mDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // this is the view we will add the gesture detector to View myView = findViewById(R.id.textView1); // get the gesture detector mDetector = new GestureDetector(this, new MyGestureListener()); // Add a touch listener to the view // The touch listener passes all its events on to the gesture detector myView.setOnTouchListener(touchListener); } // This touch listener passes everything on to the gesture detector. // That saves us the trouble of interpreting the raw touch events // ourselves. View.OnTouchListener touchListener = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // pass the events to the gesture detector // a return value of true means the detector is handling it // a return value of false means the detector didn't // recognize the event return mDetector.onTouchEvent(event); } }; // In the SimpleOnGestureListener subclass you should override // onDown and any other gesture that you want to detect. class MyGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onDown(MotionEvent event) { Log.d("TAG","onDown: "); // don't return false here or else none of the other // gestures will work return true; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { Log.i("TAG", "onSingleTapConfirmed: "); return true; } @Override public void onLongPress(MotionEvent e) { Log.i("TAG", "onLongPress: "); } @Override public boolean onDoubleTap(MotionEvent e) { Log.i("TAG", "onDoubleTap: "); return true; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Log.i("TAG", "onScroll: "); return true; } @Override public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { Log.d("TAG", "onFling: "); return true; } } }