Madhan
Madhan

Reputation: 554

Detect long click event for seekbar thumb

I have a seekbar in my android app. I need to detect the long click event for the seekbar in order to perform some action. Suggest me any solution. I have tried out onLongClickListener on the seekbar, but it is not working.

Upvotes: 0

Views: 1043

Answers (2)

Jeffery Ma
Jeffery Ma

Reputation: 3351

A customized SeekBar, handle the logic in onTouchEvent()

LongClick

when ACTION_DOWN postDelay() a long click runnable, you can modified the LONG_CLICK_DELAY value as you want. ACTION_MOVE ACTION_UP ACTION_CANCEL remove the runnable

Click

when ACTION_UP check the ACTION_DOWN motion event param make sure it is a click event


     mSeekBar.setEventListener(new DetectorSeekBar.IListener() {
        @Override
        public void onClick(DetectorSeekBar detectorSeekBar) {
            Log.e("test", "click");
        }

        @Override
        public void onLongClick(DetectorSeekBar detectorSeekBar) {
            Log.e("test", "long click");
        }
    });

public class DetectorSeekBar extends AppCompatSeekBar {

//modified the value as you wish
private static final int LONG_CLICK_DELAY = 500;

private LongClickChecker mLongClickChecker;
private ClickChecker mClickChecker;
private IListener mListener;

public DetectorSeekBar(Context context) {
    this(context, null);
}

public DetectorSeekBar(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public DetectorSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mLongClickChecker = new LongClickChecker(this);
    mClickChecker = new ClickChecker(this);
}

public void setEventListener(IListener listener) {
    mListener = listener;
    mLongClickChecker.setLongClickListener(listener);
    mClickChecker.setClickListener(listener);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (isEnabled()) {
                postDelayed(mLongClickChecker, LONG_CLICK_DELAY);
                mClickChecker.x = event.getX();
                mClickChecker.y = event.getY();
                mClickChecker.time = event.getEventTime();
            }
            break;
        case MotionEvent.ACTION_MOVE:
            removeCallbacks(mLongClickChecker);
            mClickChecker.onMoveEvent();
            break;
        case MotionEvent.ACTION_UP:
            removeCallbacks(mLongClickChecker);
            if (isEnabled()
                    && mClickChecker.checkCondition(event)) {
                post(mClickChecker);
            }
            break;
        case MotionEvent.ACTION_CANCEL:
            removeCallbacks(mLongClickChecker);
            removeCallbacks(mClickChecker);
            break;
    }
    return super.onTouchEvent(event);
}

private static class LongClickChecker implements Runnable {

    private WeakReference<IListener> mListenerRef;
    private WeakReference<DetectorSeekBar> mViewRef;

    LongClickChecker(DetectorSeekBar view) {
        mViewRef = new WeakReference<>(view);
    }

    void setLongClickListener(IListener listener) {
        mListenerRef = new WeakReference<>(listener);
    }

    @Override
    public void run() {
        if (mListenerRef != null && mListenerRef.get() != null
                && mViewRef != null && mViewRef.get() != null) {
            mListenerRef.get().onLongClick(mViewRef.get());
        }
    }
}

private static class ClickChecker implements Runnable {

    private long time = 0;
    public float x;
    public float y;
    private boolean mMoved = false;

    private WeakReference<IListener> mListenerRef;
    private WeakReference<DetectorSeekBar> mViewRef;

    ClickChecker(DetectorSeekBar view) {
        mViewRef = new WeakReference<>(view);
    }

    @Override
    public void run() {
        if (mListenerRef != null && mListenerRef.get() != null
                && mViewRef != null && mViewRef.get() != null) {
            mListenerRef.get().onClick(mViewRef.get());
        }
    }

    void onMoveEvent() {
        mMoved = true;
    }

    void setClickListener(IListener listener) {
        mListenerRef = new WeakReference<>(listener);
    }

    boolean checkCondition(MotionEvent upEvent) {
        if (upEvent != null) {
            // have moved cancel click
            if (mMoved) {
                mMoved = false;
                return false;
            }
            //ACTION_DOWN  ACTION_UP time too long cancel click
            boolean timeCorrect = upEvent.getEventTime() - time < LONG_CLICK_DELAY;
            time = 0;

            return timeCorrect;
        }
        return false;
    }
}

public interface IListener {
    void onClick(DetectorSeekBar detectorSeekBar);
    void onLongClick(DetectorSeekBar detectorSeekBar);
}

}

Upvotes: 0

Mann
Mann

Reputation: 560

After reading the documentation and other SeekBar LongClick problems with developers it looks like the LongClickListener is not working with SeekBar.

However, I can suggest a workaround :

  1. SeekBar : You can implement onTouch event for seekbar and for MotionEvent.ACTION_DOWN action you can set a timeout for 1 second and consider it the LongClick. Cancel the timer if MotionEvent.ACTION_UP is called.

  2. Thumb : You can implement OnSeekBarChangeListener and on onStartTrackingTouch event you can set a timeout for 1 second and consider it the LongClick if onStopTrackingTouch or onProgressChanged is not called during the timeout (Cancel the timer if onStopTrackingTouch/onProgressChanged is called.)

Not posting the code because my IDE isn't open now. But I hope you can find out the way using this logic. (Read documentations if you don't know about the events I mentioned)

Thank you.

Upvotes: 1

Related Questions