-1

I have a multiple SeekBars. I want to detect details about change in every SeekBar's progress value in order to calculate/update totalVal.

More specifically, I want to detect whether the selected SeekBar value was moved to the right or to the left (in respect of its previous progress level), and by how many steps it was moved.

mySeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progressVal, boolean fromUser) {
        // perform calculations here
    }
    // ...
}

For example:

  1. initialize totalVal to some value, i.e. totalval = 10
  2. One of the SeekBars was moved to the right by 1 step.
  3. totalVal = 11
  4. Different Seekbar was moved to the left by 5 steps.
  5. totalVal = 6

Can someone please tell me how to detect the direction of SeekBar movement, as well as the number of steps it was moved? If it is possible at all.

an0o0nym
  • 1,456
  • 16
  • 33
  • here an approach: set it right in the middle (that's your zero) if it's moving towards positive values that means it is moving right and vice versa, negative values for left or more simple if the value is greater than the previous one it is going right and if the next value is less that the previous it is going left – Thorvald Aug 22 '17 at 01:29

2 Answers2

2

You can save the last seekbar value. So you can detect the direction of seekbar..

protected void onCreate(Bundle savedInstanceState) {
    int lastSeekbarVal = 0;
    mySeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progressVal, boolean fromUser) {
            if (progressVal > lastSeekbarVal) {/*Seekbar moved to the right*/}
            else if (progressVal < lastSeekbarVal) {/*Seekbar moved to the left*/}
            lastSeekbarVal = progressVal;
        }
        // ...
    }
}
zihadrizkyef
  • 1,849
  • 3
  • 23
  • 46
-1

There are two Integer class members. Their name are seekBar1Current, seekBar2Current.

seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {   
@Override
    public void onProgressChanged(SeekBar seekBar, int progressVal, boolean fromUser) {
           if(progressVal < seekBar1Current) {
                  totalVal = totalVal - (seekBar1Current - progressVal);
           } else {
                  totalVal = totalVal + (progressVal - seekBar1Current);
           }
           seekBar1Current = totalVal;
       }
       // ...
    });

seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {   
@Override
    public void onProgressChanged(SeekBar seekBar, int progressVal, boolean fromUser) {
           if(progressVal < seekBar1Current) {
                  totalVal = totalVal - (seekBar2Current - progressVal);
           } else {
                  totalVal = totalVal + (progressVal - seekBar2Current);
           }
           seekBar2Current = totalVal;
       }
       // ...
    });

IMPORTANT: seekBar1Current and seekBar2Current must be the CLASS MEMBER!!!