1

I have a button. When the user holds the button I want a video to be recorded. When the user releases the button I want to add some code to process the video and stop recording, however how do I detect when the user has released the button and the onLongClickListener is done executing?

snap.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            try {
                initRecorder(mCameraView.getHolder().getSurface());
                mMediaRecorder.start();
                try {
                    Thread.sleep(10 * 1000); // This will recode for 10 seconds, if you don't want then just remove it.
                } catch (Exception e) {
                    e.printStackTrace();
                }
                finish();
                return true;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return false;
        }
    });
Alk
  • 5,215
  • 8
  • 47
  • 116
  • Maybe you need to register a touch even rather than long click listener. Take a look at this https://stackoverflow.com/questions/3784514/capture-button-release-in-android – MoGa Feb 25 '16 at 01:13
  • I already have an onClickListener implemented for the button, it takes a picture on click and is meant to record a video on long click, I have already implemented the picture functionality, now I'm trying to implement the video. Would the ontouch listener interfere with the onclick listener? – Alk Feb 25 '16 at 01:16

2 Answers2

2

I have a ready snippet for your purpose, take a look at it https://gist.github.com/0x0af/013c4e7a90a481e04f77#file-snippet-java.

Basically, what you do is implement View.OnTouchListener() and wait for MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP

UPDATE: use a Timer to determine if action was a long press

archived
  • 647
  • 8
  • 24
  • would the onTouchListener interfere in any way with an onClickListener which I have already set for the button? I want 2 functionalities for the button, one is taking photos when the user simply clicks the button ( already implemented with the onClickListener) and the second one is the video recording with the hold/release event. – Alk Feb 25 '16 at 01:25
  • I recommend measuring time of press - this should tell you if your press was long – archived Feb 25 '16 at 01:26
  • Could you elaborate please with a short example. – Alk Feb 25 '16 at 01:26
  • check out the updated version https://gist.github.com/0x0af/013c4e7a90a481e04f77#file-snippet-java – archived Feb 25 '16 at 01:37
0

Look into GestureDetector where you can detect LongPress and then analyze onTouchEvent. Good info here Detecting a long press with Android

I've used it in the following fashion:

Define GestureDetector instance:

private class LongPressGestureDetector extends GestureDetector {
    private boolean longPressDetected = false;

    public LongPressGestureDetector(Context context, OnGestureListener listener) {
        super(context, listener);
    }
}

And then use it:

    gestureDetector = new LongPressGestureDetector(holder.rootView.getContext(),
            new GestureDetector.SimpleOnGestureListener() {
                @Override
                public void onLongPress(MotionEvent event) {
                    gestureDetector.longPressDetected = true;
                }
            });

    //set the content touch listener
    holder.rootView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            gestureDetector.onTouchEvent(event);
            if (gestureDetector.longPressDetected) {
                Log.d(getClass().getSimpleName(), "Handle longPress touch event.");
                gestureDetector.longPressDetected = false;
                return true;
            }
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    // handle MotionEvent.ACTION_DOWN
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    // handle MotionEvent.ACTION_UP
                    break;
                }
                case MotionEvent.ACTION_CANCEL: {
                    // handle MotionEvent.ACTION_CANCEL
                    break;
                }
            }
            return true;
        }
    });
}
Community
  • 1
  • 1
liminal
  • 1,144
  • 2
  • 13
  • 24
  • Are you suggesting I add the GestureDetector onTouchEvent method inside my onLongClick listener? I'm a bit confused, could you please provide an example of how you would use that to detect that the user is no longer holding his finger on the button? – Alk Feb 25 '16 at 01:10
  • I also have an onClickListener defined for this button which takes a picture when the user simply clicks the button. Would this code interfere with that as I understand the onTouchListener and onClickListener would be triggered simulatenously? – Alk Feb 25 '16 at 01:19
  • Do not use `OnLongClickListener` if you go with `LongPressGestureDetector` like I described above – liminal Feb 25 '16 at 01:20
  • No no, I want two functionalities for the same button. 1. Taking pictures (triggered by normal onClickListener) 2. Recording Videos (triggered now by onLongClickListener which would be replaced by the GestureDetector) What I am asking is would the gestureDetector interfere with the normal onClickListener used for functionality 1, as the onLongClickListener currently doesn't interfere with it – Alk Feb 25 '16 at 01:22