Search code examples
androidandroid-layoutonclicklistenertouch-event

How do I determine if a screen is clicked outside of MotionEvent.ACTION_SCROLL?


I cannot access the click event of an object on the screen. so I want to catch the click events on the screen with the touchlistener. but when it is scrolling it is also perceived as a click. how can I overcome this?

 mMessagesList.setOnTouchListener(new View.OnTouchListener() {
        private static final int MAX_CLICK_DURATION = 200;
        private long startClickTime;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    startClickTime = Calendar.getInstance().getTimeInMillis();
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
                    if (clickDuration < MAX_CLICK_DURATION) {
                        //click event has occurred
                        myAction();
                    }
                    break;
                }
                case MotionEvent.ACTION_MOVE:
                    return false;
                case MotionEvent.ACTION_SCROLL:

                    return false;
                case MotionEvent.AXIS_SCROLL:
                    return false;
                case MotionEvent.ACTION_BUTTON_PRESS:
                    return true;
            }
            return false;
        }
    });

Solution

  • I used the "GestureDetector.OnGestureListener" for this. Thus "upScroll" is not triggered in "downScroll" situations. I only do it when it really touches the screen.