Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android progressBar does not update progress view/drawable

two Bars which shows the progress of a game. If the user get points or time is up etc the progressBars should be updated:

private TextView tv;
private ProgressBar levelHoldBar;
private ProgressBar levelUpBar;

//...
private void updateViews() {

    // ...
    levelHoldBar.setMax(currentLevel.getThreshold());
    levelHoldBar.setProgress(currentPoints > currentLevel.getThreshold() ? currentLevel.getThreshold() : currentPoints);

    levelUpBar.setMax(nextLevel.getThreshold());
    levelUpBar.setProgress(currentPoints > nextLevel.getThreshold() ? nextLevel.getThreshold() : currentPoints);

    tv.setText(currentPoints+"/"+currentLevel.getThreshold());

    Log.d(TAG, "hold prog/max "+levelHoldBar.getProgress()+"/"+levelHoldBar.getMax());
    Log.d(TAG, "up   prog/max "+levelUpBar.getProgress()+"/"+levelUpBar.getMax());
}

ie. Outputs:

12-03 17:48:33.918: DEBUG/MainActivity(6829): hold prog/max 20/20
12-03 17:48:33.918: DEBUG/MainActivity(6829): up   prog/max 20/50

The Log.d(...) in the end shows ALWAYS the correct values, but SOMETIMES the visual bars of the progressBars do not show the correct progesses. They show progresses that had been set previously even if the getters for "max" and "progress" return correct values (in the example the bar shows about 20% (instead of 100%) for the levelHoldBar and about 2% (instead of 40%) for the levelUp-bar). I cannot figure out, why the log-output is correct but the drawables are wrong!? The TextView (tv) is updated correctly! Whats going on here? How can I fix that?

like image 995
Stuck Avatar asked Dec 03 '10 17:12

Stuck


4 Answers

SOLUTION: It's a Bug in ProgressBar!

finally... I think I found the solution...

this does not work as one would expect:

bar.setMax(50);
bar.setProgress(20);
bar.setMax(20);
bar.setProgress(20);

The setProgress(...) seems to not trigger the update on the drawable if the same value is passed again. But it's not triggered during the setMax, too. So the update is missing. Seems like a Bug in the android ProgressBar! This took me about 8 hours now.. lol :D

To solve this, I'm just doing a bar.setProgress(0) before each update... this is only a workaround, but it works for me as expected:

bar.setMax(50);
bar.setProgress(20);
bar.setProgress(0); // <--
bar.setMax(20);
bar.setProgress(20);
like image 185
Stuck Avatar answered Nov 14 '22 23:11

Stuck


I was able to get this to work with View.post():

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // ...

    mSeekBar.post(new Runnable() {
        @Override
        public void run() {
            mSeekBar.setProgress(percentOfFullVolume);
        }
    });
}

As with SKTs answer it is unclear to me why this should work when this does not

mSeekBar.setProgress(percentOfFullVolume);

However, it seems to be true up to and including Android Lollipop.

like image 30
david.mihola Avatar answered Nov 14 '22 22:11

david.mihola


You can use a handler to update progressbar

Handler progressBarHandler = new Handler();

ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar1);;

progressBarHandler .post(new Runnable() {

      public void run() {
          bar.setProgress(progress);
      }
});
like image 43
SKT Avatar answered Nov 14 '22 21:11

SKT


For me, calling setMax() before setProgress() worked for some reason.

like image 25
Farbod Salamat-Zadeh Avatar answered Nov 14 '22 22:11

Farbod Salamat-Zadeh