0

I'm working with a Progress Bar in Android, the XML declaration is below:

   <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleHorizontal"
        android:progressDrawable="@color/green" <!-- See below about this line -->
        android:max="10000"
        android:progress="10000"
        android:layout_width="fill_parent"
        android:layout_height="3dp"
        android:layout_alignParentTop="true"/>

So the progress bar shows up fine in its full glory or whatever. But I have an ASyncTask which will reset the progress bar's progress to 0, then load it back up to it's max. This doesn't work. Instead, the progress bar just remains at it's maximum value. I went ahead and logged the progress bar's progress when it is supposed to reset to 0 and update, and according to the progress bar's getProgress() method, it does in fact go to 0 then build back up to 10000. When you actually look at the phone though it just remains full.

So look back at the XML and you'll see that I noted the line android:progressDrawable. This is where I change the color of the progress bar. If I remove that line, the progress bar behaves as expected. However when the progress bar's drawable is set to the color I want, it just sits there will full progress on the screen.

I've used that progressDrawable attribute before without a hitch, but in this case it's just giving me a big headache. Any experience or ideas on how to fix this? Thanks!

1 Answer 1

3

The problem is the android:progressDrawable attribute. In this case you should use layer-list drawable.

Horizontal ProgressBar utilizes up to 3 different layers - background, progress and secondaryProgress (you don't need secondaryProgress in your case). Since you used simple color I suspect the color is used for the background and the other two colors remain unset and transparent.

All you need is a drawable like this one:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@android:id/background"
        android:drawable="@android:color/transparent"/>
    <item android:id="@android:id/progress">
        <clip android:drawable="@color/green" />
    </item>

</layer-list>
1
  • Of course... I remember now. Hopefully this question will serve to help someone in the future. Thanks! Aug 14, 2012 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.