shiva
shiva

Reputation: 483

Android SeekBar set progress value

I want the SeekBar progress to change whenever I click a button. Using setProgress() works only for initial value. It throws an error if I use it after some changes.

Upvotes: 34

Views: 88407

Answers (10)

derPureStoff
derPureStoff

Reputation: 1

I had the same problem. Calling the onChangeListener() only when fromUser is true helped:

private class MyListener implements SeekBar.OnSeekBarChangeListener {
          public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
                   if(fromUser){ 
                        ..... 
                   }
          }
    
          public void onStartTrackingTouch(SeekBar seekBar) {}
    
          public void onStopTrackingTouch(SeekBar seekBar) {}
}

Upvotes: 0

Jazib Khan
Jazib Khan

Reputation: 440

I was also facing the same issue. I used coroutines to fix it:

lifecycleScope.launch(Dispatchers.Main) {
    seekBar.progress = level
}

Upvotes: 0

Mohd Danish
Mohd Danish

Reputation: 204

Update Seekbar with coroutines delay

lifecycleScope.launch(Dispatchers.Main) {
            delay(100)
            getViewDataBinding()!!.seekBar.progress = it?.position?.toInt() ?: 0
        }

Upvotes: 1

raz amsily
raz amsily

Reputation: 11

I've just been tinkering with the seek Bar on My App....

In Order to the seekBar to Change while you are in the Activity you need to Call the method setProgress(), and pass it a boolean animate value.

Like this:

seekBar.setProgress(int valueOfProgress, bool animate)

Upvotes: 0

shellym
shellym

Reputation: 576

Try this :

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

instead of :

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

Only if you set the Max Value first, can the SeekBar get an idea of how much progress is actually made, when you set the progressValue.

This actually made a difference for me when I wanted a default value for my Progress Bar

Upvotes: 8

Suragch
Suragch

Reputation: 511626

As of support library 28.0.0 anyway, I can set the progress simply like this

mySeekBar.setProgress(17);

and the position gets updated without a problem.

Upvotes: 2

Mahmoud
Mahmoud

Reputation: 2883

The solution that works for me:

mSeekbaBar.setProgress(progress);
mSeekbaBar.post(new Runnable() {
            @Override
            public void run() {
                mSeekbaBar.setProgress(progress);
            }
        });

It removes the blinking of the thumb

Upvotes: 2

Jesus Dimrix
Jesus Dimrix

Reputation: 4568

You can also use :

mSeekBar.refreshDrawableState();

after you set progress .

Upvotes: 4

PFROLIM
PFROLIM

Reputation: 1194

Perhaps you should try to use a handler? I use this in an app of mine and works fine.

1) When creating your SeekBar:

// ...
seekBarHandler = new Handler(); // must be created in the same thread that created the SeekBar
seekBar = (SeekBar) findViewById(R.id.my_seekbar);
// you should define max in xml, but if you need to do this by code, you must set max as 0 and then your desired value. this is because a bug in SeekBar (issue 12945) (don't really checked if it was corrected)
seekBar.setMax(0);
seekBar.setMax(max);
seekBar.setProgress(progress);
// ...

2) When your button is clicked

// ...
seekBarHandler.post(new Runnable() {
    @Override
    public void run() {
        if (seekBar != null) {
            seekBar.setMax(0);
            seekBar.setMax(max);
            seekBar.setProgress(newProgress);
        }
    }
});
// ...

Upvotes: 36

similuke
similuke

Reputation: 11

You need to call setOnSeekbarChangeListener() on your seekbar object and let for example your activity implement OnSeekbarChangeListener() and in that function you can do whatever you want.

Upvotes: 1

Related Questions