Search code examples
javaandroidonlongclicklistener

Releasing onLongClickListener Android


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;
        }
    });

Solution

  • 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