Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProgressBar.setProgressDrawable not working for Android 2.3

Tags:

android

I am currently working with a dynamically updating ProgressBar. Through certain percentages, the progessbar sets a drawable of a different color. We currently have various colored clip drawable defined in a drawable xml. The one entitled progressbar_blue_states is detailed as follows:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

  <item android:id="@android:id/background"
      android:drawable="@drawable/progressbar_grey">
  </item>

  <item android:id="@android:id/progress">
      <clip android:drawable="@drawable/progressbar_blue" />
  </item>
</layer-list>

Whenever we need to update the dialog, we call the following code:

progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.progressbar_blue_states));

However, not only does this not update the ProgressBar, but also it takes out the progress bar altogether where whitespace is left in it's place. However, if I set android:progressDrawable="@drawable/progressbar_blue_states" in the xml and take out this setProgressDrawable() call, it loads correctly. We need the setProgressDrawable to update the colors as needed.

This call works fine in Android 4.0+ however in Android 2.3 we're running into some trouble. Any ideas?

Edit

This is how we set up the ProgressBar in the xml:

<ProgressBar android:id="@+id/progress"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="2dp"
            android:indeterminate="false"
            android:indeterminateOnly="false"
            android:progress="24"
            android:max="100"
            android:progressDrawable="@drawable/progressbar_red_states" />
like image 855
jbenowitz Avatar asked Mar 20 '13 23:03

jbenowitz


Video Answer


2 Answers

I have got the same bug, but it is solved by using this answer

This means there is a new drawable set to the seekbar, but the size of the drawable is 0, you won't see anything.

Rect bounds = mySeekBar.getProgressDrawable().getBounds();
mySeekBar.setProgressDrawable(newSeekBarBackground);
mySeekBar.getProgressDrawable().setBounds(bounds);
like image 85
anshad Avatar answered Oct 17 '22 06:10

anshad


According to this post https://stackoverflow.com/a/6953534/3223742

a good solution is to set progress to 1; re-set max progress, and then set the real progress :

progressBar.setProgressDrawable(...);
progressBar.setProgress(1);
progressBar.setMax(maxProgress);
progressBar.setProgress(progress);
like image 25
yochi376 Avatar answered Oct 17 '22 06:10

yochi376