Android: SeekBar onProgressChanged-event does not start when setting progress progress programmatically

My onProgressChanged () - the event does not fire when I programmed the SeekBar program, but it fires fine when I physically move the SeekBar slider.

I expect the event to fire when using setProgress () - the Android Developers Reference even states that:

public abstract void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser)

Notification that the level of progress has changed. Clients can use the fromUser parameter to distinguish user-initiated changes from programs that occurred programmatically.

Some snippets of code from my project:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_activity); final SeekBar mySeekBar = ((SeekBar) findViewById(R.id.mySeekBar)); mySeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){ @Override public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) { // Do some stuff } } } @Override protected void onResume() { super.onResume(); final SeekBar mySeekBar = ((SeekBar) findViewById(R.id.mySeekBar)); mySeekBar.setProgress(someValue); // This SHOULD trigger onProgressChanged(), but it doesn't... } 
+6
source share
5 answers

+1 for Romuald Brunet:

Here is my hack to fix this:

  <SeekBar android:id="@+id/seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:progress="1" android:max="200" /> 

Pay attention to progress = "1", set the default move in the layout to 1, and then in my code, when I actually default to 0, a change occurs during the event and the onProgressChanged () event occurs.

+8
source

For a moment I came across the same problem.

In my case, onProgressChanged did not start simply because the value did not actually change . I set the same value as the current one ( 0 :)

(and I don't see anything wrong with your code)

+5
source

I can confidently confirm that setProgress works on Android version 6.0.1.

Another fooobar.com/questions/952399 / ... said it was a mistake. If this was true, it is definitely not so.

This code works in my test setup:

 @Override public void onResume() { super.onResume(); SeekBar seekBar = (SeekBar)findViewById(R.id.seek_bar); seekBar.setProgress(seekBar.getProgress()); } 

As expected, a call to the SeekBar OnSeekBarChangeListener is called.

To troubleshoot, make sure you set the listener correctly using setOnSeekBarChangeListener() , and make sure that you implement the interface functions. Other SO answers seem to indicate that you should only call setProgress() from the user interface thread. This answer shows how this can be done.

0
source

SeekBar.OnSeekBarChangeListener.onProgressChanged is called only when the progress actually changes, not necessarily when setProgress is called - this only happens if the new progress is different from the last.

Look at the source SeekBar , which extends AbsSeekBar , which, in turn, extends ProgressBar , we see that the call to setProgress() does not start onProgressChanged() if the new progress is not different from the previous one, as shown in the source below.

 /* Snippet from ProgressBar.java */ public void setProgress(int progress, boolean animate) { setProgressInternal(progress, false, animate); } synchronized boolean setProgressInternal(int progress, boolean fromUser, boolean animate) { if (mIndeterminate) { // Not applicable. return false; } progress = MathUtils.constrain(progress, mMin, mMax); if (progress == mProgress) { // No change from current. return false; } mProgress = progress; refreshProgress(R.id.progress, mProgress, fromUser, animate); return true; } 
0
source

just call

  mySeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){ @Override public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) { int i= arg0.getProgress(); //youre seekbar.setProgress(i); } } 
-1
source

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


All Articles