BadCash BadCash - 2 months ago 25
Android Question

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...
}

Answer

+1 for Romuald Brunet's answer:

Here was my "hack" to fix it:

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

Note the progress="1", set the default progress in the layout to 1, then in my code when I actually default it to 0, a change in the progress occurs and the onProgressChanged() event fires.

Comments