views:

4975

answers:

3

I'm using an horizontal progress bar in my Android application, and I want to change its progress color (which is Yellow by default). How can I do it using code (not XML)?

A: 

Have you tried MyProgressBar.setProgressDrawable(Drawable d) specifying a bitmap with the color you want?

http://developer.android.com/reference/android/widget/ProgressBar.html#setProgressDrawable(android.graphics.drawable.Drawable)

http://developer.android.com/reference/android/graphics/drawable/Drawable.html

fupsduck
Yes i've tried it but it doesn't work. It sets the background color of the whole progress bar view instead of setting the background color of only the bar itself.Thanks.
WhiteTigerK
+10  A: 

I'm sorry that it's not the answer, but what's driving the requirement setting it from code ? And .setProgressDrawable should work if it's defined correctly

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

<item android:id="@android:id/background">
    <shape>
        <corners android:radius="5dip" />
        <gradient
                android:startColor="#ff9d9e9d"
                android:centerColor="#ff5a5d5a"
                android:centerY="0.75"
                android:endColor="#ff747674"
                android:angle="270"
        />
    </shape>
</item>

<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="5dip" />
            <gradient
                    android:startColor="#80ffd300"
                    android:centerColor="#80ffb600"
                    android:centerY="0.75"
                    android:endColor="#a0ffcb00"
                    android:angle="270"
            />
        </shape>
    </clip>
</item>
<item
    android:id="@android:id/progress"
>
    <clip>
        <shape>
            <corners
                android:radius="5dip" />
            <gradient
                android:startColor="@color/progress_start"
                android:endColor="@color/progress_end"
                android:angle="270" />
        </shape>
    </clip>
</item>

</layer-list>
Alex Volovoy
The reason is because I'm creating the progress bar dynamically, and setting its color upon user request. Since I usually use code for building my GUI screen and components, I'm not familiar with the attached XML and I don't know what's a layer-list (although I'm guessing that you are building the progress bar based on several layers..).In case I do want to use the XML you've attached - where should I place the it in the project's folder and is there anything more I need to do for creating a progress bar based on the XML settings ?Thanks.
WhiteTigerK
You save this xml as a file and put it in the drawable folder ( let's say my_progress.xml ) than you set it as a drawable inMyProgressBar.setProgressDrawable()To change colors - you'll need to change those values in @color/progress_start@color/progress_end It's basically a gradient and you can put hex in there.
Alex Volovoy
Note - file is the copy of the one that's in SDK. I've dropped copyright here. If you look in the res/drawable folder you'll see exactly what i've posted - colors are set to the yellow gradient, instead of custom colors.
Alex Volovoy
Ok, I'll try using this XML.Thanks!
WhiteTigerK
Its does not show any color change to me. please tell the color which worked out.
Praveen Chandrasekaran
A: 

If I use "myProgressBar.setProgressDrawable(R.drawable.my_progress);" it says:

"The method setProgressDrawable(Drawable) in the type ProgressBar is not applicable for the arguments (int) progress.java /progress/src/android/Progress line 23 Java Problem"

Bumper