Alk
Alk

Reputation: 5557

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

Upvotes: 1

Views: 2788

Answers (2)

archived
archived

Reputation: 647

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

Upvotes: 2

liminal
liminal

Reputation: 1164

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

Upvotes: 0

Related Questions