Home » Android » android – Using SeekBar and setProgress doesn't change seekBar position

android – Using SeekBar and setProgress doesn't change seekBar position

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am using a Seekbar in a fragment and need to move the Seekbar to different positions. However, setProgress(xxx) is not working.

How do you trigger this method programmatically for a Seekbar: public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)? Moving the Seekbar manually works fine.

How to&Answers:

It’s a Bug in ProgressBar!

The setProgress(...) seems to not trigger the update on the drawable if the same value is passed again. But it’s not triggered during the setMax, too. So the update is missing.

To solve this, I’m just doing a bar.setProgress(0) before each update… this is only a workaround, but it works for me as expected:

bar.setProgress(0); // call these two methods before setting progress.
bar.setMax(20);
bar.setProgress(20);

Second Option.

mSeekBar.post(new Runnable() {
        @Override
        public void run() {
            mSeekBar.setProgress(percentOfFullVolume);
        }
    });

it may also work for someone.

Try to set max value first and then set the progress.

Answer:

I add this answer as this can help someone in the future.
In my case, I also thought setProgress was not working properly but here is my mistake.

increaseIncomeSeekBar.setProgress(progress);
increaseIncomeSeekBar.setMax(maxProgress);

For info progress and maxProgress equals 2000 and 12000. But when displaying the SeekBar, it seems like the progress was at 0 and thus not working properly.
You have to be aware of this which is pretty logical when you know it:

  • first SeekBar max = 100.
  • when you set a progress > max then progress = max.
  • so you need to set a max value before setting a progress value.

Answer:

I add a comment to this topic because it happenned to me and it took me hours to figure out what the problem was. The bug is still active and the setProgress() method does not work properly sometimes.

I have a MainActivity which commit and replace Fragments and transmit the SeekBar value inside the bundle of each Fragments. The User changes the value of the Seekbar and its progress are put in the Bundle. Then when the User switch of Fragment, the new one get the SeekBar progress.

It works perfectly fine to transmit it this way and I always have the right value on my progress variable in the second Fragment. The problem appears when I use setProgress(theProgressValueTransmitted) method. This set my SeekBar progress only the first time I replace the first Fragment. After that, it never changes it wheareas the value of progress is still the right one.

I used :

int seekBarProgress;
Bundle bundle = this.getArguments();
    if (bundle != null) {
        seekBarProgress = bundle.getInt("seekBarProgress");
    mySeekBar.post(new Runnable() {
                @Override
                public void run() {
                    eventListTimeSeekBar.setProgress(seekBarProgress);

                }
            });
    }

And this is the only way I can make this work. Hope it could help someone with the same problem. This post is more than 4 years old, I don’t even understand how this bug can still exist since it has been reported.

Answer:

SeekBar.setProgress() should work just fine. Are you sure your code is executing on the UI thread? If not, then this would be the obvious explanation. Check the example in the API doc for ProgressBar

https://developer.android.com/reference/android/widget/ProgressBar.html

There they show how to bring the execution back to the main thread.

Answer:

Nowadays everything seems to work as expected.
Post is not needed anymore.
Just do:

seekBar.setMax(200);
seekBar.setProgress(50);

(order doesn’t matter)

Answer:

Most of the time, SeekBar works fine. Sometimes, it won’t.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    //...
    seekBar.setProgress(value);
    //...
}

After I refresh the fragment view by detach() + attach(), the code does not work. My solution is to apply setProgress() within onViewStateRestored().

@Override
public void onViewStateRestored(Bundle inState) {
    //...
    seekBar.setProgress(value);
    //...
}