0

I have a button, and i am trying to fire two actions in one button. to call the method takeVideo on button long press. and on button press i want to call the method imageCapture.

the below code is working for long press. but i am not able to detect the button press to call only the imageCapture method.

takePhotoBtn.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            long down;
            int action = motionEvent.getAction();
            if (action == MotionEvent.ACTION_BUTTON_PRESS) {
           imageCapture();
                return true;
            }
            if (action == MotionEvent.ACTION_DOWN) {

                    takeVideo();
                    timer.start();
                    return true;

            } else if (action == MotionEvent.ACTION_UP) {
                takeVideo();
                timer.cancel();
                return true;
            }

            return false;
        }


    });

EDIT

i am not using OnLongClickListener and OnClickListener.

i want to perform other actions on MotionEvent.ACTION_UP so i am trying to solve my problem using setOnTouchListener in this case

Naz141
  • 433
  • 1
  • 8
  • 31
  • Possible duplicate of [Android: long click on a button -> perform actions](http://stackoverflow.com/questions/4402740/android-long-click-on-a-button-perform-actions) – AdamMc331 Nov 23 '15 at 14:31
  • Thank you but i want to solve my problem with `setOnTouchListener ` – Naz141 Nov 23 '15 at 14:46

3 Answers3

2

You can get the duration of the Long Press over official documentation (getLongPressTimeOut) It use to be 1000ms and now 500ms... it could change. That's why you need to get the timeout.

Then, over ACTION_DOWN you can count the ms... if ms >= 500 (if currently is 500ms) then is a LongPress. But, like Marios says, use the OnLongClickListener and OnClickListener for that.

Mariano Zorrilla
  • 7,165
  • 2
  • 34
  • 52
1

Why don't you use

onLongClickListener (http://developer.android.com/reference/android/view/View.OnLongClickListener.html)

and

OnClickListener (http://developer.android.com/reference/android/view/View.OnClickListener.html)

harrane
  • 959
  • 11
  • 24
  • i want to use `MotionEvent.ACTION_UP` to perform other actions, that is why i dont use onLongClickListener – Naz141 Nov 23 '15 at 14:54
1

setOnLongClickListener can be used for this.

takePhotoBtn.setOnLongClickListener(new OnLongClickListener() { 
    @Override
    public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
        return true;
    }
});
Smittey
  • 2,475
  • 10
  • 28
  • 35
  • i want to use `MotionEvent.ACTION_UP` to perform other actions, that is why i dont use onLongClickListener – Naz141 Nov 23 '15 at 14:54