Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: SeekBar onProgressChanged-event doesn't get fired when setting progress programmatically

My onProgressChanged()-event doesn't get fired when I set the progress of a SeekBar programmatically, but it does get fired perfectly fine when I physically move the SeekBar slider.

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

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

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

Some code snippets 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...
}
like image 287
Magnus Avatar asked Aug 23 '13 23:08

Magnus


People also ask

How do I SeekBar progress?

We can get the current progress value from a Seekbar in java class using getProgress() method. This method returns an integer value. Below code is used to get the current progress value from a Seek bar.

What is SeekBar progress bar?

Android SeekBar is a kind of ProgressBar with draggable thumb. The end user can drag the thum left and right to move the progress of song, file download etc. The SeekBar. OnSeekBarChangeListener interface provides methods to perform even handling for seek bar.

What is SeekBar Setprogress?

↳ android.widget.SeekBar. A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys.


1 Answers

Stumbled across the same problem just now.

In my case, the onProgressChanged did not get fired simply because the value did not actually change. I was setting the same value as the current one (0 :)

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

like image 60
Romuald Brunet Avatar answered Oct 21 '22 09:10

Romuald Brunet