Search code examples
androidimageviewonclicklisteneronlongclicklistener

Implement both of onTouchListner and onLongClickListner for an ImageView


I tryed to implement both of onTouchListner and onLongClickListner for my ImageView like this :

imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                ShowMessageToast("Long Touch ");
                return true ;
            }

        });
        imageView.setOnTouchListener(new ImageView.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {

                action = event.getAction();                  
                    switch (action) {
                        case MotionEvent.ACTION_DOWN:
                           ShowMessageToast("Down ");
                            break;

                        case MotionEvent.ACTION_MOVE:
                            ShowMessageToast("Moved ");
                            break;


                        default:
                            break;
                    }

                return true;
            }

but the onLongClickListner doesn't work until I desactivate onTouchListner(//comment).

if the onTouchListner is not make as comment (//....) it never display "Long Touch"

ShowMessageToast("Moved ") is juste a function to display a message in a Toast.


Solution

  • The LongClickListener and the TouchListener interfere with each other as a long click always starts with a touch (ACTION_DOWN). What kind of gesture would you like to detect in the TouchListener?

    If you want (as example) detect long-clicks together with scrolling, then a TouchListener together with a GestureListener would be the way to handle it. Here's an example how to do this:

    findViewById(R.id.myImageView).setOnTouchListener(new OnTouchListener() {
        private GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                Log.d("TEST", "onScroll");
                // TODO Auto-generated method stub
                return super.onScroll(e1, e2, distanceX, distanceY);
            }
    
            /* (non-Javadoc)
             * @see android.view.GestureDetector.SimpleOnGestureListener#onLongPress(android.view.MotionEvent)
             */
            @Override
            public void onLongPress(MotionEvent e) {
                Log.d("TEST", "onLongPress");
                // TODO Auto-generated method stub
                super.onLongPress(e);
            }
    
            // ... implement here other callback methods like onFling, onScroll as necessary
        });
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d("TEST", "Raw event: " + event.getAction() + ", (" + event.getRawX() + ", " + event.getRawY() + ")");
            return gestureDetector.onTouchEvent(event);
        }
    });
    

    Remarks

    • This implements just the onTouchListener and no onLonGClickListener is necessary. The TouchListener just passes all touch events to a gesture listener. The gesture listener collects the gestures and calls the callback methods whenever one of the gestures is detected.
    • Gestures can be: scroll, fling, tap (click - either confirmed to be not a double tap or without waiting), double-tap, long press.
    • My code shows an example for long-press and scrolling. Just implement other callbacks as you need to detect other gestures.