1

I have a Click function and long press on the same button. Implemented the long press event but, I need to find the button UP_EVENT and DOWN_EVENTS separately. How can I implement by using the OnLongClickListener

 View.OnLongClickListener listener = new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View view) {

                return true;
            }

        };
Vineesh TP
  • 7,755
  • 12
  • 66
  • 130

3 Answers3

1

Implement a TouchListener within the onLongClickListener:

    View.OnLongClickListener listener = new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View view) {
            view.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            // PRESSED
                            return true; // if you want to handle the touch event
                        case MotionEvent.ACTION_UP:
                            // RELEASED
                            return true; // if you want to handle the touch event
                    }
                    return false;
                }
            });
            return true;
        }

    };
Community
  • 1
  • 1
TejjD
  • 2,571
  • 1
  • 17
  • 37
0

To detect ACTION_UP and ACTION_DOWN events you need to implement OnTouchListener.

Eslam El-Meniawy
  • 105
  • 1
  • 2
  • 8
0

to sepate , you can do this way

@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:

            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            if (isOnClick) {                    
                //TODO onClick code
            }
            break;
        case MotionEvent.ACTION_MOVE:

            }
            break;
        default:
            break;
    }
    return true;
}
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142