Last Updated: June 14, 2015

SeekBar Swipe Control

Description:

In this post I'm gonna show you How you can control your seekbar( horizontal + vertical ) using the android GestureDetector.In this technique you can move your seekbar progress on screen's swipe up,down,left right movement using simple callbacks.






So let's get stared :





Step1: Add a Gesture Detector to calculate the screen's swipe(left,right,top,down).

private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 10;
        private static final int SWIPE_VELOCITY_THRESHOLD = 10;

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            boolean result = float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(distanceX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            iSwipeRefresh.rightSwipe();
                            return true;
                        } else {
                            iSwipeRefresh.leftSwipe();
                            return true;

                        }
                    }
                } else {
                    if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(distanceY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {

                            iSwipeRefresh.downSwipe();
                            return true;
                        } else {
                            iSwipeRefresh.upSwipe();
                            return true;
                        }
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }


        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return true;
        }
    }
Step 2: Create a interface to receive the callback.

public interface ISwipeRefresh {

    void leftSwipe();

    void rightSwipe();

    void upSwipe();

    void downSwipe();
}
Step 3: Add the above Gesture detector in a class implementing onTouchListener.

public class OnScrollTouchListenerControl implements View.OnTouchListener {

    private static final String TAG = OnScrollTouchListenerControl.class.getName();
    private final GestureDetector gestureDetector;
    private ISwipeRefresh iSwipeRefresh;

    public OnScrollTouchListenerControl(Context ctx, ISwipeRefresh iSwipeRefresh) {
        this.iSwipeRefresh = iSwipeRefresh;
        gestureDetector = new GestureDetector(ctx, new GestureListener());
    }

    public boolean onTouch(final View view, final MotionEvent motionEvent) {
        return gestureDetector.onTouchEvent(motionEvent);
    }
 }




Step:4 Finally lets integrate with this Seekbar.


public class MainActivity extends Activity implements ISwipeRefresh ,SeekBar.OnSeekBarChangeListener {

    private final static String TAG = MainActivity.class.getName();
    private SeekBar horizontalSeekB;
    private int MIN_PROGRESS = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    private void init() {

        findViewById(R.id.mainContainer).setOnTouchListener(new OnScrollTouchListenerControl(this, this));
        horizontalSeekB     = (SeekBar)findViewById(R.id.horizontalSeekBar);
        horizontalSeekB.setMax(100);
        horizontalSeekB.setOnSeekBarChangeListener(this);
    }

    @Override
    public void leftSwipe() {
        horizontalSeekB.setProgress(horizontalSeekB.getProgress() - MIN_PROGRESS);
    }

    @Override
    public void rightSwipe() {
        horizontalSeekB.setProgress(horizontalSeekB.getProgress() + MIN_PROGRESS);
    }

    @Override
    public void upSwipe() {
        //verticalSeekbarB.setProgress(verticalSeekbarB.getProgress() - MIN_PROGRESS);
    }

    @Override
    public void downSwipe() {
        //verticalSeekbarB.setProgress(verticalSeekbarB.getProgress() + MIN_PROGRESS);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {}

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {}

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {}
}


GITHUB Project HERE

Bingo we're DONE!!!


3 comments :

  1. There's no doubt i would fully rate it after i read what is the idea about this article. You did a nice job. Taylor Swift Red Cardigan

    ReplyDelete
  2. Fashion is the thing that you make it - The primary thing you need to comprehend about fashion is that it is just what you make it, that's it. Peaky Blinders Outfit

    ReplyDelete
  3. Our service immediately approaches each complaint https://expertpaperwriter.com/samedayessay-review/. In case the paper requires corrections, get it revised at no cost. Get your money back in case the problem defies any solution.

    ReplyDelete

Your comments are valuable for us !!!