I have set Horizontal Progress bar.
I would like to change progress color of yellow.
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="80dip"
android:layout_height="20dip"
android:focusable="false"
style="?android:attr/progressBarStyleHorizontal" />
The problem is, the progress color is different in different devices. So, I want it to fix the progress color.
Just create a style in
values/styles.xml
.Then set this style as your
ProgressBar
theme.and doesn't matter your progress bar is horizontal or circular. That's all.
ProgressBar color can be changed as follows:
/res/values/colors.xml
/res/values/styles.xml
onCreate:
for API level 21 or higher just use
final LayerDrawable layers = (LayerDrawable) progressBar.getProgressDrawable(); layers.getDrawable(2).setColorFilter(color,PorterDuff.Mode.SRC_IN);
You guys are really giving me a headache. What you can do is make your layer-list drawable via xml first (meaning a background as the first layer, a drawable that represents secondary progress as the second layer, and another drawable that represents the primary progress as the last layer), then change the color on the code by doing the following:
This method will give you the best flexibility of having the measurement of your original drawable declared on the xml, WITH NO MODIFICATION ON ITS STRUCTURE AFTERWARDS, especially if you need to have the xml file screen folder specific, then just modifying ONLY THE COLOR via the code. No re-instantiating a new ShapeDrawable from scratch whatsoever.
There are 3 ways to solve this question:
In particular there is a lot of confusion around #2 and #3, as seen in comments to amfcosta's answer. That answer will yield unpredictable color results anytime you'd like to set the progressbar to anything except primary colors, as it only modifies the background color, and the actual progress bar "clip" area will still be a yellow overlay with reduced opacity. For example, using that method to set the background to dark purple will result in a progress bar "clip" color of some crazy pinkish color resulting from dark purple and yellow mixing via reduced alpha.
So anyhow, #1 is answered perfectly by Ryan and Štarke answers most of #3, but for those looking for a complete solution to #2 and #3:
How to adjust the progressbar color programmatically, but choose the color from a predetermined color declared in XML
res/drawable/my_progressbar.xml:
Create this file but change the colors in my_progress and my_secondsProgress:
(NOTE: This is just a copy of the actual XML file defining the default android SDK ProgressBar, but I've changed the IDs and Colors. The original is named progress_horizontal)
In your Java:
How to adjust the progressbar color programmatically, and also create the color programatically
EDIT: I found the time to solve this properly. My former answer left this a bit ambiguous.
A ProgressBar is composed as 3 Drawables in a LayerDrawable.
In the example below I'll change the color of the main progress bar to cyan.
That example set it to a solid color. You may set it to a gradient color by replacing
with
Cheers.
-Lance