0

How do I track all gestures on an entire application screen. I was struggling with adding a gesture detector to the entire screen of my application. I am using gesturedetector compat to get gestures from the layout however when I tap or double tap the button and edit text gesturedetector does not detect the gesture on the view. Is it possible to have all tap,swipes,gestures on the ENTIRE screen as opposed to only the layout. If there is a better solution to tracking all touches,swipes,taps please let me know I have struggling with this for weeks.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private GestureDetectorCompat mGestureDetector;

    private TextView touchCheckText;
    private TextView singleTapText;
    private TextView doubleTapText;

    private static final String TAG = "Gesture ";

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction()!=KeyEvent.ACTION_DOWN)
            Log.i("key pressed", String.valueOf(event.getKeyCode()));
        return super.dispatchKeyEvent(event);
    }

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

        mGestureDetector = new GestureDetectorCompat(this,new GestureListener());

        touchCheckText = (TextView) findViewById(R.id.touchCheckTextView);
        scrollText = (TextView) findViewById(R.id.scrollTextView);
        singleTapText = (TextView) findViewById(R.id.singleTapTextView);
        doubleTapText = (TextView) findViewById(R.id.doubleTapTextView);

    }

    private class GestureListener extends GestureDetector.SimpleOnGestureListener{

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            int x = (int) e.getX();
            int y = (int) e.getY();

            Long tsLong = System.currentTimeMillis()/1000;
            String ts = tsLong.toString();

            Log.e(TAG, "in on onDoubleTap Event");
            doubleTapText.setBackgroundColor(Color.GREEN);
            return super.onDoubleTap(e);
        }

        @Override
        public boolean onDown(MotionEvent e) {
            touchCheckText.setText("TOUCHING!");
            return super.onDown(e);
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            int x = (int) e.getX();
            int y = (int) e.getY();

            Long tsLong = System.currentTimeMillis()/1000;
            String ts = tsLong.toString();

            singleTapText.setBackgroundColor(Color.GREEN);
            Log.e(TAG, "Single Tap "+ts+" x:"+x+" y:"+y);
            singleTapText.postDelayed(new Runnable() {

                @Override
                public void run() {
                    // change color in here
                    singleTapText.setBackgroundColor(Color.TRANSPARENT);
                }
            }, 100);

            return super.onSingleTapConfirmed(e);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mGestureDetector.onTouchEvent(event);
        switch ( event.getAction() ) {
            case MotionEvent.ACTION_UP:
                Log.e("onTouch","Released");
                doubleTapText.setBackgroundColor(Color.TRANSPARENT);
                touchCheckText.setText("No Longer Touching");
                break;
        }
        return super.onTouchEvent(event);
    }
}
  • You can try adding an `onTouchListener()` to each of the views you need to detect gestures on. Try following the accepted answer here: [SO: adding gesture detection to views](https://stackoverflow.com/questions/45054908/how-to-add-a-gesture-detector-to-a-view-in-android). – Alias Cartellano Oct 22 '21 at 17:11
  • Would it be possible to have sort of like a screen on top of the application that views all gestures without interfering with the functionality of the application – Kevin Randleman Oct 22 '21 at 18:41
  • You can use an [onInterceptTouchEvent()](https://developer.android.com/training/gestures/viewgroup#:~:text=Intercept%20touch%20events%20in%20a%20ViewGroup%20The%20onInterceptTouchEvent,ViewGroup%2C%20including%20on%20the%20surface%20of%20its%20children.) to have the touch to the layout's children intercepted to get it handled by one view only. I think this guide may suffice: [Correctly implemetning onInterceptTouchEvent](http://neevek.net/posts/2013/10/13/implementing-onInterceptTouchEvent-and-onTouchEvent-for-ViewGroup.html). – Alias Cartellano Oct 22 '21 at 19:00
  • Thank you but what am looking for is code that is independent to the code of an application like a screen that track movement without interfering with an app. – Kevin Randleman Oct 25 '21 at 03:53

0 Answers0