Search code examples
androidtouchandroid-event

Detect ongoing touch event


I am currently trying to detect an ongoing touch event in my Android app. In detail I want to recognize within a fragment whether the user touches any part of the app's screen.

Android's OnTouchListener works as expected except if the touch event lasts longer than a few seconds without moving.

For example the first 1-2 seconds of touch are being detected but everything after won't.

Is there something like an "OnPressListener" or a workaround?


Solution

  • I finally found it out.

    The solution is to use a simple OnTouchListener:

    private boolean pressed = false;
    
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            pressed = true;
    
        } else if ((action == MotionEvent.ACTION_UP)
                || (action == MotionEvent.ACTION_CANCEL)) {
            pressed = false;
        }
    
        return true;
    }