Arno
Arno

Reputation: 101

How to update progress bar in fragment without threading

I try to update the progress of my progressBar in a fragment, but nothing happens. I don't use threading and don't want to.

I found loads of examples and tried to use setProgress(0) and setMax(max) before updating the value but that was unsuccessful.

The code:

scoreABar.setProgress(0);
scoreBBar.setProgress(0);
scoreCBar.setProgress(0);
scoreDBar.setProgress(0);
scoreEBar.setProgress(0);
scoreABar.setMax(100);
scoreBBar.setMax(100);
scoreCBar.setMax(100);
scoreDBar.setMax(100);
scoreEBar.setMax(100);
scoreABar.setProgress((int) scoreA);
scoreBBar.setProgress((int) scoreB);
scoreCBar.setProgress((int) scoreC);
scoreDBar.setProgress((int) scoreD);
scoreEBar.setProgress((int) scoreE);

This code is in a method call in OnCreateView() of my fragment. I'm sure that my values change but the progressBar doesn't update.

Upvotes: 0

Views: 213

Answers (1)

bvk256
bvk256

Reputation: 1893

Better to define each your progressbar in xml like this:

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:indeterminate="false"
    android:max="100"
    android:progress="0" />

Then you set the progress with scoreBar.setProgress((int) score); Try it if it works for you

EDIT: Your task that you want to display progress for should be run in background thread, but you should always update the progress from main thread, e.g by using activity.runOnUiThread() method

Upvotes: 0

Related Questions