1

I'm facing an issue on how to control Seekbar progress when user ONLY touching the Thumb

I knew it is a duplicated question but here are the links that gave me an idea about it this and this.

After several rounds of testing, below are my working logic result

mySeekbar.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Rect rcThumb = mySeekbar.getThumb().getBounds();
        int iWidth = rcThumb.width();
        if (event.getX() >= (rcThumb.left - iWidth) && event.getX() <= (rcThumb.right + iWidth)) {
            return false;
        } else
            return true;
    }
});

Expected result
no drag or tap
when released
during drag or tap
enter image description here
Problem
You can just copy and paste the above code for testing. As we know that when touching or dragging the thumb will be larger, but there is an issue were thumb will be large when not touching it. This issue will not happen often but seldom. Anyway to solve this ? Much help is appreciated.

Problem result
no drag or tap
when released
during drag or tap
enter image description here

Arduino
  • 285
  • 1
  • 3
  • 17

1 Answers1

2

After few rounds of testing, I was able to accomplish the result. Cheers :)

  private boolean mIsDragging;

  private boolean isWithinThumb(MotionEvent event, SeekBar seekBar) {
    Rect rcThumb = seekBar.getThumb().getBounds();
    Rect rcDetectTouchArea = new Rect();
    int iWidth = rcThumb.width();
    int iHeight = rcThumb.height();
    rcDetectTouchArea.left = rcThumb.left - iWidth;
    rcDetectTouchArea.right = rcThumb.right + iWidth;
    rcDetectTouchArea.bottom = rcThumb.bottom + iHeight;
    Log.i("TAG", "rcDetectSize.left  = " + rcDetectTouchArea.left + " | rcDetectSize.right = " + rcDetectTouchArea.right + " |  rcDetectSize.bottom = " + rcDetectTouchArea.bottom + " | event.getX()= " + event.getX() + " | event.getY()= " + event.getY() + " | rcThumb  area = " + rcDetectTouchArea.contains((int) event.getX(), (int) event.getY()));
    return rcDetectTouchArea.contains((int) event.getX(), (int) event.getY());
}

  mSbTest.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        if (isWithinThumb(event, mSbTest)) {
                            mIsDragging = true;
                            return false;
                        }
                    case MotionEvent.ACTION_UP:
                        if (mIsDragging) {
                            mIsDragging = false;
                            return false;
                        }
                        if (isWithinThumb(event, mSbTest)) {
                            return false;
                        } else {
                            return true;
                        }
                    case MotionEvent.ACTION_MOVE:
                        if (!mIsDragging) {
                            return true;
                        }
                }
                return onTouchEvent(event);
            }
        });
Arduino
  • 285
  • 1
  • 3
  • 17