Search code examples
androidseekbarandroid-seekbar

Android SeekBar Addition Issues


EDIT: I also have pre-existing elements within my app that also add to the counter, so assigning counter = progress would just override the sum instead of adding to.

seekBar = (SeekBar) findViewById(R.id.seekBarDemo);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        seekBarTextView.setText("Tracking: " + progress + "/" + seekBar.getMax());
        counter += progress;
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }

});

I'm having an issue with getting a counterto obtain a value off the seekbar. It is fine when you drag it to where you want to at first, but lets say from a scale of 1 - 10 and you are changing your mind consistently from 1 to 4 to 10 to 7.

7 appears on my TextView but the value I get when passing it to my counter is the sum of all of the numbers, where in this example, I would get 22.

Please let me know what I'm doing wrong. Thank you in advance.


Solution

  • counter += progress;

    Is the same as counter = counter + progress.

    First pass: counter = 0 + progress // let's say progress = 7

    Second pass: counter = 7 + progress // let's say progress = 5, counter is now = 12...

    If you just want the current value of the slider, you want to use counter = progress;