Android SeekBar set progress value
Asked Answered
E

10

34

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.

Elastance answered 20/3, 2012 at 18:37 Comment(0)
P
36

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);
        }
    }
});
// ...
Primine answered 29/3, 2013 at 16:29 Comment(3)
Why are you calling setMax(int) twice? Seems the second one just nullifies the first one.Bulldoze
"this is because a bug in SeekBar (issue 12945) (don't really checked if it was corrected)"Primine
This solution does not work for me. setting the max to zero and then to the desired value before setting progress has no effect. Call to setProgress() still does not work.Bulldoze
R
8

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

Redcap answered 4/10, 2018 at 17:55 Comment(0)
E
4

You can also use :

mSeekBar.refreshDrawableState();

after you set progress .

Excruciating answered 29/10, 2014 at 11:19 Comment(0)
M
2

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

Mano answered 3/6, 2016 at 22:37 Comment(0)
J
2

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.

Jeaninejeanlouis answered 26/9, 2018 at 6:10 Comment(0)
E
1

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.

Epiblast answered 18/6, 2012 at 21:22 Comment(0)
M
1

Update Seekbar with coroutines delay

lifecycleScope.launch(Dispatchers.Main) {
            delay(100)
            getViewDataBinding()!!.seekBar.progress = it?.position?.toInt() ?: 0
        }
Milkwort answered 12/2, 2021 at 7:0 Comment(1)
No need for delay, it is working without it.Ovenware
G
0

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)
Grattan answered 23/12, 2020 at 13:41 Comment(1)
I think the problem is related to setting the progress of the seek bar something greater than its max value.Marconigraph
O
0

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

lifecycleScope.launch(Dispatchers.Main) {
    seekBar.progress = level
}
Ovenware answered 15/11, 2021 at 8:3 Comment(0)
E
0

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) {}
}
Excessive answered 10/8, 2022 at 13:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.