Using SeekBar and setProgress does not change SeekBar position

I use Seekbar in a fragment and I need to move Seekbar to different positions. However, setProgress(xxx) does not work.

How do you programmatically run this method for Seekbar : public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) ? Moving manually Seekbar is excellent.

+3
source share
5 answers

This is a mistake in the ProgressBar!

It seems that setProgress(...) does not start the update in drawable if the same value is passed again. But this did not work during setMax either. Therefore, there is no update.

To solve this problem, I just do bar.setProgress(0) before each update ... this is just 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); 

The second option.

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

It may also work for someone.

First try to set the maximum value, and then set the progress.

+6
source

I am adding a comment to this topic because it happened to me and it took me several hours to understand what the problem was. The error is still active, and the setProgress () method sometimes does not work as expected.

I have MainActivity that capture and replace Fragments and pass the value of the SeekBar inside the packet of each fragment. The user changes the value of Seekbar, and his progress is placed in the Bundle . Then, when the user switch Fragment , the new one receives the SeekBar progress.

It works great to pass it that way, and I always have the correct value for my progress variable in the second snippet . The problem occurs when I use the setProgress (theProgressValueTransmitted) method. This sets my SeekBar progress only the first time that I replace the first fragment. After that, he never changes it, because the significance of progress is still correct.

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 to do this job. Hope this helps someone with the same issue. This message is more than 4 years old, I don’t even understand how this error still exists, since it was reported.

+2
source

I am adding this answer as it may help someone in the future.
In my case, I also thought that setProgress was not working correctly, but here is my mistake.

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

For progress information, both maxProgress is 2000 and 12000. But when displaying SeekBar, it seems that progress was 0 and therefore does not work properly.
You should be aware of this, which is pretty logical when you know this:

  • SeekBar max = 100 first.
  • when you set progress> max then progress = max.
  • therefore you need to set the maximum value before setting the progress value.
+1
source

SeekBar.setProgress() should work fine. Are you sure your code is executing in a user interface thread? If not, this will be the obvious explanation. Check the example in the API document for ProgressBar

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

There they show how to return execution to the main thread.

0
source

In most cases, SeekBar works just fine. Sometimes it will not.

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

After I updated the fragment view with the detach () + attach () command, the code does not work. My solution is to apply setProgress () in onViewStateRestored ().

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

Source: https://habr.com/ru/post/952399/


All Articles