Search code examples
androidseekbar

Prevent SeekBar from moving past a dynamic point


I need two SeekBars for setting a minimum and maximum, and the thumb on each SeekBar cannot move past the other. Is there a way to disallow moving the SeekBar past a specific point?


Solution

  • Found the solution myself: in SeekBar.OnSeekBarChangeListener.onProgressChanged() method, just set the progress to a correct value, which in this case is the same as the other SeekBar

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
        if (!isLegalMove(seekBar)) {
            seekBar.setProgress(mOtherSeekBar.getProgress());
        } 
    }
    
    private boolean isLegalMove(SeekBar thisSeekBar) {
        if (mOtherSeekBar == null) {
            return true;
        }
        return mIsMax && mOtherSeekBar.getProgress() <= thisSeekBar.getProgress() ||
            !mIsMax && mOtherSeekBar.getProgress() >= thisSeekBar.getProgress();
    }