Search code examples
androidseekbar

How to stop the movement of seekBar past a dynamic point


I am trying to stop the movement of a certain seekBar after it reaches a dynamic point (calculated from subtraction the setMax() value and other seekBar.getProgress()). I tried using the onTouchEvent() but couldn't stop the movement of the bar and setting manually the seekBar.setProgress() but it only jumps to that point and continue moving with the finger.

  sb1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            sb1.setMax(Integer.parseInt(et.getText().toString()));
            if (progress >= Integer.parseInt(et.getText().toString()) - sb2.getProgress() - sb3.getProgress())
                sb1.setProgress(progress);
            tv1.setText(String.valueOf(progress));
        }


    });

    sb2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            sb2.setMax(Integer.parseInt(et.getText().toString()));

            if (progress >= Integer.parseInt(et.getText().toString()) - sb1.getProgress() - sb3.getProgress())
                sb2.setProgress(progress);
            tv2.setText(String.valueOf(progress));
        }

    });

please help


Solution

  • You have

    if (progress >= Integer.parseInt(et.getText().toString()) - sb2.getProgress() - sb3.getProgress())
        sb1.setProgress(progress);
    

    Inside your sb1 change listener. But that doesn't really make sense, because progress is already the progress of sb1. I think you should change this:

    sb1.setProgress(progress);
    

    To:

    sb1.setProgress(Integer.parseInt(et.getText().toString()) - sb2.getProgress() - sb3.getProgress());
    

    The value I got for that progress was from your if condition. Basically when the progress is greater than that value, you just set it to the value. So this way, it cannot be dragged past that point.

    Do the same thing for your other listeners as well.