Android change Horizontal Progress bar colorHow to implement a material design circular progress bar in androidHow to change default ProgressDialog circle color in androidHow to change default ProgressBar circle color on AndroidHow to change progress bar progress color?How to create Custom Horizontal progress barHow to increase height of ProgressBar in ICS/JBCreate Dialog Box with Progress Bar in androidChange Horizontal Progress Bar ColorDynamically adding a TextviewSet Progress bar progress color programaticallyIs there a way to run Python on Android?How to save an Android Activity state using save instance state?How do I center text horizontally and vertically in a TextView?Close/hide the Android Soft KeyboardWhy is the Android emulator so slow? How can we speed up the Android emulator?How to change progress bar's progress color in AndroidIs there a unique Android device ID?change color of horizontal progress barProper use cases for Android UserManager.isUserAGoat()?Hex transparency in colors
Why are Oscar, India, and X-Ray (O, I, and X) not used as taxiway identifiers?
If hash functions append the length, why does length extension attack work?
How can I deal with someone that wants to kill something that isn't supposed to be killed?
Help!. Wiring an IKEA light fixture into old fixture
How often should alkaline batteries be checked when they are in a device?
Why was Quirrell said to be in the Black Forest if Voldemort was actually in Albania?
What would be the effects of (relatively) widespread precognition on the stock market?
Is it better to deliver many low-value stories or few high-value stories?
As the Ferris wheel turns
What kind of curve (or model) should I fit to my percentage data?
What is the metal bit in the front of this propeller spinner?
90s (or earlier) book in which a human detective is hired by a vampire to find killer of vampires
Magic is the twist
Why did modems have speakers?
As a DM of a 4-player group, would it be appropriate for me to run a private 1-on-1 session so that one PC can act secretly?
Is there any direct train from LHR Airport to Newcastle Gateshead?
Is it better to merge "often" or only after completion do a big merge of feature branches?
What kind of vegetable has pink and white concentric rings?
Caption in landscape table, need help?
Can a creature sustain itself by eating its own severed body parts?
I have a domain, static IP and many devices I'd like to access outside my house. How to route them?
Does Mathematica 12 support GT 730 CUDA?
I am a dual citizen of United States and Mexico, can I use my Mexican license in california when visiting?
How old is the Italian word "malandrino"?
Android change Horizontal Progress bar color
How to implement a material design circular progress bar in androidHow to change default ProgressDialog circle color in androidHow to change default ProgressBar circle color on AndroidHow to change progress bar progress color?How to create Custom Horizontal progress barHow to increase height of ProgressBar in ICS/JBCreate Dialog Box with Progress Bar in androidChange Horizontal Progress Bar ColorDynamically adding a TextviewSet Progress bar progress color programaticallyIs there a way to run Python on Android?How to save an Android Activity state using save instance state?How do I center text horizontally and vertically in a TextView?Close/hide the Android Soft KeyboardWhy is the Android emulator so slow? How can we speed up the Android emulator?How to change progress bar's progress color in AndroidIs there a unique Android device ID?change color of horizontal progress barProper use cases for Android UserManager.isUserAGoat()?Hex transparency in colors
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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.
android colors progress-bar android-progressbar
add a comment |
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.
android colors progress-bar android-progressbar
above answers change the whole background color and it's ambigios that it change the height of progress bar to very max.which unable to change in xml. better way to change only progress bar status that how much is completed i.s 20% red color, 50% yellow color 70% and above green color. you can do it programmatically in java. share your answer if you have any other solution.
– Mubashar
Mar 17 '17 at 9:52
add a comment |
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.
android colors progress-bar android-progressbar
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.
android colors progress-bar android-progressbar
android colors progress-bar android-progressbar
edited May 23 '18 at 3:42
Cœur
21.5k10 gold badges125 silver badges171 bronze badges
21.5k10 gold badges125 silver badges171 bronze badges
asked Apr 21 '11 at 14:54
Nishant ShahNishant Shah
1,8455 gold badges20 silver badges29 bronze badges
1,8455 gold badges20 silver badges29 bronze badges
above answers change the whole background color and it's ambigios that it change the height of progress bar to very max.which unable to change in xml. better way to change only progress bar status that how much is completed i.s 20% red color, 50% yellow color 70% and above green color. you can do it programmatically in java. share your answer if you have any other solution.
– Mubashar
Mar 17 '17 at 9:52
add a comment |
above answers change the whole background color and it's ambigios that it change the height of progress bar to very max.which unable to change in xml. better way to change only progress bar status that how much is completed i.s 20% red color, 50% yellow color 70% and above green color. you can do it programmatically in java. share your answer if you have any other solution.
– Mubashar
Mar 17 '17 at 9:52
above answers change the whole background color and it's ambigios that it change the height of progress bar to very max.which unable to change in xml. better way to change only progress bar status that how much is completed i.s 20% red color, 50% yellow color 70% and above green color. you can do it programmatically in java. share your answer if you have any other solution.
– Mubashar
Mar 17 '17 at 9:52
above answers change the whole background color and it's ambigios that it change the height of progress bar to very max.which unable to change in xml. better way to change only progress bar status that how much is completed i.s 20% red color, 50% yellow color 70% and above green color. you can do it programmatically in java. share your answer if you have any other solution.
– Mubashar
Mar 17 '17 at 9:52
add a comment |
13 Answers
13
active
oldest
votes
I copied this from one of my apps, so there's prob a few extra attributes, but should give you the idea. This is from the layout that has the progress bar:
<ProgressBar
android:id="@+id/ProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:maxHeight="10dip"
android:minHeight="10dip"
android:progress="50"
android:progressDrawable="@drawable/greenprogress" />
Then create a new drawable with something similar to the following (In this case greenprogress.xml):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<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="#33FF33"
android:endColor="#008000"
android:angle="270" />
</shape>
</clip>
</item>
</layer-list>
You can change up the colors as needed, this will give you a green progress bar.
And also i remove this android:indeterminate="false"
– Nishant Shah
Apr 22 '11 at 7:10
2
Please help me. I try to create greenprogress.xml by copy your code but the Android Studio 0.4.6 mark <layer-list> as and error "Element-list must be declared" what should I do?
– UmAnusorn
Feb 25 '14 at 9:22
1
Is it possible to change the colour dynamically?
– async
May 29 '14 at 21:08
can the colour be changed depending on current progress value defined in the actual bar?
– jonney
Nov 21 '14 at 15:41
@Ryan It would be helpful to explain whatandroid:id="@android:id/progress"
,android:id="@android:id/background"
andandroid:id="@android:id/secondaryProgress"
stand for.
– RustamG
May 29 '17 at 7:00
|
show 1 more comment
A simpler solution:
progess_drawable_blue
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<solid
android:color="@color/disabled" />
</shape>
</item>
<item
android:id="@android:id/progress">
<clip>
<shape>
<solid
android:color="@color/blue" />
</shape>
</clip>
</item>
</layer-list>
1
here @color/disabled is a custom color
– Nitin Saxena
Jan 31 '13 at 18:44
2
Or <solid android:color="@android:color/black" />, or even <solid android:color="#ff33b5e5" />
– superarts.org
Mar 18 '14 at 13:24
This is enough android:indeterminateTint="@color/white" for API lever 21 and above
– Ghanshyam Nayma
Nov 13 '18 at 14:39
add a comment |
If you only want to change the progress bar color, you can simply use a color filter in your Activity's onCreate() method:
ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressbar);
int color = 0xFF00FF00;
progressbar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
progressbar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
Idea from here.
You only need to do this for pre-lollipop versions. On Lollipop you can use the colorAccent style attribute.
ForSeekBar
it is the same, just add for API 16:seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
. This will change the colour of that round thumb.
– Sufian
Oct 1 '15 at 7:07
1
CallingsetColorFilter()
on a drawable directly will apply the colour everywhere. Since this is what I didn't need, I just calledmutate()
on theDrawable
(i.e likeseekBar.getThumb().mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
– Sufian
Oct 1 '15 at 10:22
2
I've seen this solution elsewhere but all it does (testing on a Gingerbread device) is turn the entire progress bar that one color, making it impossible see the actual progress
– Neil C. Obremski
Feb 27 '16 at 22:23
@NeilC.Obremski yes you're right. It might be ROM specific, or maybe version specific. I had to remove it altogether. Hopefully we will have this ported back to older versions of Android soon (via support lib).
– Sufian
Jun 27 '16 at 7:17
add a comment |
Just create a style in values/styles.xml
.
<style name="ProgressBarStyle">
<item name="colorAccent">@color/greenLight</item>
</style>
Then set this style as your ProgressBar
theme.
<ProgressBar
android:theme="@style/ProgressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
and doesn't matter your progress bar is horizontal or circular.
That's all.
1
Wow, the simpliest solution, works for me. I wondered why this answer has so many votes.
– comm1x
Dec 27 '17 at 16:54
1
Did not work for me for Indeterminate ProgressBar
– Vadim Kotov
Feb 12 at 15:56
add a comment |
for API level 21 or higher just use
android:progressBackgroundTint="@color/yourBackgroundColor"
android:progressTint="@color/yourColor"
add a comment |
There are 3 ways to solve this question:
- How to adjust the progressbar color declaratively
- How to adjust the progressbar color programmatically, but choose the color from various predetermined colors declared before compile-time
- How to adjust the progressbar color programmatically, and also create the color programatically.
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)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/my_progress_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="@+id/my_secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ff171d"
android:centerColor="#80ff1315"
android:centerY="0.75"
android:endColor="#a0ff0208"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="@+id/my_progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#7d2afdff"
android:centerColor="#ff2afdff"
android:centerY="0.75"
android:endColor="#ff22b9ba"
android:angle="270"
/>
</shape>
</clip>
</item>
In your Java:
final Drawable drawable;
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < 16)
drawable = ctx.getResources().getDrawable(R.drawable.my_progressbar);
else
drawable = ContextCompat.getDrawable(ctx, R.drawable.my_progressbar);
progressBar.setProgressDrawable(drawable)
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.
- Layer 1 is the background
- Layer 2 is the secondary progress color
- Layer 3 is the main progress bar color
In the example below I'll change the color of the main progress bar to cyan.
//Create new ClipDrawable to replace the old one
float pxCornerRadius = viewUtils.convertDpToPixel(5);
final float[] roundedCorners = new float[] pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius ;
ShapeDrawable shpDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));
shpDrawable.getPaint().setColor(Color.CYAN);
final ClipDrawable newProgressClip = new ClipDrawable(shpDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
//Replace the existing ClipDrawable with this new one
final LayerDrawable layers = (LayerDrawable) progressBar.getProgressDrawable();
layers.setDrawableByLayerId(R.id.my_progress, newProgressClip);
That example set it to a solid color. You may set it to a gradient color by replacing
shpDrawable.getPaint().setColor(Color.CYAN);
with
Shader shader = new LinearGradient(0, 0, 0, progressBar.getHeight(), Color.WHITE, Color.BLACK, Shader.TileMode.CLAMP);
shpDrawable.getPaint().setShader(shader);
Cheers.
-Lance
what's viewUtils in viewUtils.convertDpToPixel(5);
– Nikos Hidalgo
Jul 11 at 15:57
1
Hi @NikosHidalgo - You can see a nearly identical method here: stackoverflow.com/questions/12849366/…. The only difference is that I construct myViewUtils
with a reference toContext
, so that I don't need to pass aContext
when callingconvertDpToPixel()
– lance.dolan
Jul 11 at 16:04
Thank you for your prompt reply, especially on an answer that was posted so long ago!
– Nikos Hidalgo
Jul 11 at 16:06
Haha, I really had to stretch my brain to remember :p
– lance.dolan
Jul 11 at 16:09
add a comment |
The simplest solution I found out is something like this:
<item name="colorAccent">@color/white_or_any_color</item>
Put the above in styles.xml
file under res > values
folder.
NOTE: If you use any other accent color, then the previous solutions should be good to go with.
API 21 or HIGHER
3
This works only for API Level >= 21
– amfcosta
Mar 22 '15 at 0:16
Yes, sorry about not indicating it in the post. Thanks :)
– Sazid
Mar 23 '15 at 2:23
add a comment |
For anyone looking for how to do it programmatically:
Drawable bckgrndDr = new ColorDrawable(Color.RED);
Drawable secProgressDr = new ColorDrawable(Color.GRAY);
Drawable progressDr = new ScaleDrawable(new ColorDrawable(Color.BLUE), Gravity.LEFT, 1, -1);
LayerDrawable resultDr = new LayerDrawable(new Drawable[] bckgrndDr, secProgressDr, progressDr );
//setting ids is important
resultDr.setId(0, android.R.id.background);
resultDr.setId(1, android.R.id.secondaryProgress);
resultDr.setId(2, android.R.id.progress);
Setting ids to drawables is crucial, and takes care of preserving bounds and actual state of progress bar
Nice one - this is exactly what I needed - I didn't have the ID's.
– Ewald
Apr 6 '16 at 12:55
add a comment |
final LayerDrawable layers = (LayerDrawable) progressBar.getProgressDrawable();
layers.getDrawable(2).setColorFilter(color,PorterDuff.Mode.SRC_IN);
1
Try to add a little explanation to your answer. Code only answers are discouraged.
– Alexei
Jun 9 '16 at 6:55
I'd love to get an explanation behind the index 2 in getDrawable, but at least this was the only solution that worked for me, so thanks!
– Nikos Hidalgo
Jul 11 at 16:02
add a comment |
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:
public void setPrimaryProgressColor(int colorInstance)
if (progressBar.getProgressDrawable() instanceof LayerDrawable)
Log.d(mLogTag, "Drawable is a layer drawable");
LayerDrawable layered = (LayerDrawable) progressBar.getProgressDrawable();
Drawable circleDrawableExample = layered.getDrawable(<whichever is your index of android.R.id.progress>);
circleDrawableExample.setColorFilter(colorInstance, PorterDuff.Mode.SRC_IN);
progressBar.setProgressDrawable(layered);
else
Log.d(mLogTag, "Fallback in case it's not a LayerDrawable");
progressBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
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.
add a comment |
ProgressBar color can be changed as follows:
/res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorAccent">#FF4081</color>
</resources>
/res/values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorAccent">@color/colorAccent</item>
</style>
onCreate:
progressBar = (ProgressBar) findViewById(R.id.progressBar);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
Drawable drawable = progressBar.getIndeterminateDrawable().mutate();
drawable.setColorFilter(ContextCompat.getColor(this, R.color.colorAccent), PorterDuff.Mode.SRC_IN);
progressBar.setIndeterminateDrawable(drawable);
add a comment |
If you are in API level 21 or above you can use these XML attributes:
<!-- For indeterminate progress bar -->
android:indeterminateTint="@color/white"
<!-- For normal progress bar -->
android:progressTint="@color/white"
add a comment |
Create a drawable resource what background you need, in my case I named it bg_custom_progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<item>
<shape android:shape="rectangle" >
<corners android:radius="5dp"/>
<gradient
android:angle="180"
android:endColor="#DADFD6"
android:startColor="#AEB9A3" />
</shape>
</item>
<item>
<clip>
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<gradient
android:angle="180"
android:endColor="#44CF4A"
android:startColor="#2BB930" />
</shape>
</clip>
</item>
<item>
<clip>
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<gradient
android:angle="180"
android:endColor="#44CF4A"
android:startColor="#2BB930" />
</shape>
</clip>
</item>
Then use this custom background like
<ProgressBar
android:id="@+id/progressBarBarMenuHome"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="6dp"
android:indeterminate="false"
android:max="100"
android:minWidth="200dp"
android:minHeight="50dp"
android:progress="10"
android:progressDrawable="@drawable/bg_custom_progressbar" />
Its works fine for me
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5745814%2fandroid-change-horizontal-progress-bar-color%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
13 Answers
13
active
oldest
votes
13 Answers
13
active
oldest
votes
active
oldest
votes
active
oldest
votes
I copied this from one of my apps, so there's prob a few extra attributes, but should give you the idea. This is from the layout that has the progress bar:
<ProgressBar
android:id="@+id/ProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:maxHeight="10dip"
android:minHeight="10dip"
android:progress="50"
android:progressDrawable="@drawable/greenprogress" />
Then create a new drawable with something similar to the following (In this case greenprogress.xml):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<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="#33FF33"
android:endColor="#008000"
android:angle="270" />
</shape>
</clip>
</item>
</layer-list>
You can change up the colors as needed, this will give you a green progress bar.
And also i remove this android:indeterminate="false"
– Nishant Shah
Apr 22 '11 at 7:10
2
Please help me. I try to create greenprogress.xml by copy your code but the Android Studio 0.4.6 mark <layer-list> as and error "Element-list must be declared" what should I do?
– UmAnusorn
Feb 25 '14 at 9:22
1
Is it possible to change the colour dynamically?
– async
May 29 '14 at 21:08
can the colour be changed depending on current progress value defined in the actual bar?
– jonney
Nov 21 '14 at 15:41
@Ryan It would be helpful to explain whatandroid:id="@android:id/progress"
,android:id="@android:id/background"
andandroid:id="@android:id/secondaryProgress"
stand for.
– RustamG
May 29 '17 at 7:00
|
show 1 more comment
I copied this from one of my apps, so there's prob a few extra attributes, but should give you the idea. This is from the layout that has the progress bar:
<ProgressBar
android:id="@+id/ProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:maxHeight="10dip"
android:minHeight="10dip"
android:progress="50"
android:progressDrawable="@drawable/greenprogress" />
Then create a new drawable with something similar to the following (In this case greenprogress.xml):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<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="#33FF33"
android:endColor="#008000"
android:angle="270" />
</shape>
</clip>
</item>
</layer-list>
You can change up the colors as needed, this will give you a green progress bar.
And also i remove this android:indeterminate="false"
– Nishant Shah
Apr 22 '11 at 7:10
2
Please help me. I try to create greenprogress.xml by copy your code but the Android Studio 0.4.6 mark <layer-list> as and error "Element-list must be declared" what should I do?
– UmAnusorn
Feb 25 '14 at 9:22
1
Is it possible to change the colour dynamically?
– async
May 29 '14 at 21:08
can the colour be changed depending on current progress value defined in the actual bar?
– jonney
Nov 21 '14 at 15:41
@Ryan It would be helpful to explain whatandroid:id="@android:id/progress"
,android:id="@android:id/background"
andandroid:id="@android:id/secondaryProgress"
stand for.
– RustamG
May 29 '17 at 7:00
|
show 1 more comment
I copied this from one of my apps, so there's prob a few extra attributes, but should give you the idea. This is from the layout that has the progress bar:
<ProgressBar
android:id="@+id/ProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:maxHeight="10dip"
android:minHeight="10dip"
android:progress="50"
android:progressDrawable="@drawable/greenprogress" />
Then create a new drawable with something similar to the following (In this case greenprogress.xml):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<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="#33FF33"
android:endColor="#008000"
android:angle="270" />
</shape>
</clip>
</item>
</layer-list>
You can change up the colors as needed, this will give you a green progress bar.
I copied this from one of my apps, so there's prob a few extra attributes, but should give you the idea. This is from the layout that has the progress bar:
<ProgressBar
android:id="@+id/ProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:maxHeight="10dip"
android:minHeight="10dip"
android:progress="50"
android:progressDrawable="@drawable/greenprogress" />
Then create a new drawable with something similar to the following (In this case greenprogress.xml):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<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="#33FF33"
android:endColor="#008000"
android:angle="270" />
</shape>
</clip>
</item>
</layer-list>
You can change up the colors as needed, this will give you a green progress bar.
edited Aug 14 '13 at 7:58
Ye Lin Aung
7,1486 gold badges39 silver badges49 bronze badges
7,1486 gold badges39 silver badges49 bronze badges
answered Apr 21 '11 at 15:03
RyanRyan
4,2581 gold badge12 silver badges8 bronze badges
4,2581 gold badge12 silver badges8 bronze badges
And also i remove this android:indeterminate="false"
– Nishant Shah
Apr 22 '11 at 7:10
2
Please help me. I try to create greenprogress.xml by copy your code but the Android Studio 0.4.6 mark <layer-list> as and error "Element-list must be declared" what should I do?
– UmAnusorn
Feb 25 '14 at 9:22
1
Is it possible to change the colour dynamically?
– async
May 29 '14 at 21:08
can the colour be changed depending on current progress value defined in the actual bar?
– jonney
Nov 21 '14 at 15:41
@Ryan It would be helpful to explain whatandroid:id="@android:id/progress"
,android:id="@android:id/background"
andandroid:id="@android:id/secondaryProgress"
stand for.
– RustamG
May 29 '17 at 7:00
|
show 1 more comment
And also i remove this android:indeterminate="false"
– Nishant Shah
Apr 22 '11 at 7:10
2
Please help me. I try to create greenprogress.xml by copy your code but the Android Studio 0.4.6 mark <layer-list> as and error "Element-list must be declared" what should I do?
– UmAnusorn
Feb 25 '14 at 9:22
1
Is it possible to change the colour dynamically?
– async
May 29 '14 at 21:08
can the colour be changed depending on current progress value defined in the actual bar?
– jonney
Nov 21 '14 at 15:41
@Ryan It would be helpful to explain whatandroid:id="@android:id/progress"
,android:id="@android:id/background"
andandroid:id="@android:id/secondaryProgress"
stand for.
– RustamG
May 29 '17 at 7:00
And also i remove this android:indeterminate="false"
– Nishant Shah
Apr 22 '11 at 7:10
And also i remove this android:indeterminate="false"
– Nishant Shah
Apr 22 '11 at 7:10
2
2
Please help me. I try to create greenprogress.xml by copy your code but the Android Studio 0.4.6 mark <layer-list> as and error "Element-list must be declared" what should I do?
– UmAnusorn
Feb 25 '14 at 9:22
Please help me. I try to create greenprogress.xml by copy your code but the Android Studio 0.4.6 mark <layer-list> as and error "Element-list must be declared" what should I do?
– UmAnusorn
Feb 25 '14 at 9:22
1
1
Is it possible to change the colour dynamically?
– async
May 29 '14 at 21:08
Is it possible to change the colour dynamically?
– async
May 29 '14 at 21:08
can the colour be changed depending on current progress value defined in the actual bar?
– jonney
Nov 21 '14 at 15:41
can the colour be changed depending on current progress value defined in the actual bar?
– jonney
Nov 21 '14 at 15:41
@Ryan It would be helpful to explain what
android:id="@android:id/progress"
, android:id="@android:id/background"
and android:id="@android:id/secondaryProgress"
stand for.– RustamG
May 29 '17 at 7:00
@Ryan It would be helpful to explain what
android:id="@android:id/progress"
, android:id="@android:id/background"
and android:id="@android:id/secondaryProgress"
stand for.– RustamG
May 29 '17 at 7:00
|
show 1 more comment
A simpler solution:
progess_drawable_blue
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<solid
android:color="@color/disabled" />
</shape>
</item>
<item
android:id="@android:id/progress">
<clip>
<shape>
<solid
android:color="@color/blue" />
</shape>
</clip>
</item>
</layer-list>
1
here @color/disabled is a custom color
– Nitin Saxena
Jan 31 '13 at 18:44
2
Or <solid android:color="@android:color/black" />, or even <solid android:color="#ff33b5e5" />
– superarts.org
Mar 18 '14 at 13:24
This is enough android:indeterminateTint="@color/white" for API lever 21 and above
– Ghanshyam Nayma
Nov 13 '18 at 14:39
add a comment |
A simpler solution:
progess_drawable_blue
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<solid
android:color="@color/disabled" />
</shape>
</item>
<item
android:id="@android:id/progress">
<clip>
<shape>
<solid
android:color="@color/blue" />
</shape>
</clip>
</item>
</layer-list>
1
here @color/disabled is a custom color
– Nitin Saxena
Jan 31 '13 at 18:44
2
Or <solid android:color="@android:color/black" />, or even <solid android:color="#ff33b5e5" />
– superarts.org
Mar 18 '14 at 13:24
This is enough android:indeterminateTint="@color/white" for API lever 21 and above
– Ghanshyam Nayma
Nov 13 '18 at 14:39
add a comment |
A simpler solution:
progess_drawable_blue
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<solid
android:color="@color/disabled" />
</shape>
</item>
<item
android:id="@android:id/progress">
<clip>
<shape>
<solid
android:color="@color/blue" />
</shape>
</clip>
</item>
</layer-list>
A simpler solution:
progess_drawable_blue
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<solid
android:color="@color/disabled" />
</shape>
</item>
<item
android:id="@android:id/progress">
<clip>
<shape>
<solid
android:color="@color/blue" />
</shape>
</clip>
</item>
</layer-list>
answered Jan 31 '13 at 18:43
Nitin SaxenaNitin Saxena
9746 silver badges5 bronze badges
9746 silver badges5 bronze badges
1
here @color/disabled is a custom color
– Nitin Saxena
Jan 31 '13 at 18:44
2
Or <solid android:color="@android:color/black" />, or even <solid android:color="#ff33b5e5" />
– superarts.org
Mar 18 '14 at 13:24
This is enough android:indeterminateTint="@color/white" for API lever 21 and above
– Ghanshyam Nayma
Nov 13 '18 at 14:39
add a comment |
1
here @color/disabled is a custom color
– Nitin Saxena
Jan 31 '13 at 18:44
2
Or <solid android:color="@android:color/black" />, or even <solid android:color="#ff33b5e5" />
– superarts.org
Mar 18 '14 at 13:24
This is enough android:indeterminateTint="@color/white" for API lever 21 and above
– Ghanshyam Nayma
Nov 13 '18 at 14:39
1
1
here @color/disabled is a custom color
– Nitin Saxena
Jan 31 '13 at 18:44
here @color/disabled is a custom color
– Nitin Saxena
Jan 31 '13 at 18:44
2
2
Or <solid android:color="@android:color/black" />, or even <solid android:color="#ff33b5e5" />
– superarts.org
Mar 18 '14 at 13:24
Or <solid android:color="@android:color/black" />, or even <solid android:color="#ff33b5e5" />
– superarts.org
Mar 18 '14 at 13:24
This is enough android:indeterminateTint="@color/white" for API lever 21 and above
– Ghanshyam Nayma
Nov 13 '18 at 14:39
This is enough android:indeterminateTint="@color/white" for API lever 21 and above
– Ghanshyam Nayma
Nov 13 '18 at 14:39
add a comment |
If you only want to change the progress bar color, you can simply use a color filter in your Activity's onCreate() method:
ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressbar);
int color = 0xFF00FF00;
progressbar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
progressbar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
Idea from here.
You only need to do this for pre-lollipop versions. On Lollipop you can use the colorAccent style attribute.
ForSeekBar
it is the same, just add for API 16:seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
. This will change the colour of that round thumb.
– Sufian
Oct 1 '15 at 7:07
1
CallingsetColorFilter()
on a drawable directly will apply the colour everywhere. Since this is what I didn't need, I just calledmutate()
on theDrawable
(i.e likeseekBar.getThumb().mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
– Sufian
Oct 1 '15 at 10:22
2
I've seen this solution elsewhere but all it does (testing on a Gingerbread device) is turn the entire progress bar that one color, making it impossible see the actual progress
– Neil C. Obremski
Feb 27 '16 at 22:23
@NeilC.Obremski yes you're right. It might be ROM specific, or maybe version specific. I had to remove it altogether. Hopefully we will have this ported back to older versions of Android soon (via support lib).
– Sufian
Jun 27 '16 at 7:17
add a comment |
If you only want to change the progress bar color, you can simply use a color filter in your Activity's onCreate() method:
ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressbar);
int color = 0xFF00FF00;
progressbar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
progressbar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
Idea from here.
You only need to do this for pre-lollipop versions. On Lollipop you can use the colorAccent style attribute.
ForSeekBar
it is the same, just add for API 16:seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
. This will change the colour of that round thumb.
– Sufian
Oct 1 '15 at 7:07
1
CallingsetColorFilter()
on a drawable directly will apply the colour everywhere. Since this is what I didn't need, I just calledmutate()
on theDrawable
(i.e likeseekBar.getThumb().mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
– Sufian
Oct 1 '15 at 10:22
2
I've seen this solution elsewhere but all it does (testing on a Gingerbread device) is turn the entire progress bar that one color, making it impossible see the actual progress
– Neil C. Obremski
Feb 27 '16 at 22:23
@NeilC.Obremski yes you're right. It might be ROM specific, or maybe version specific. I had to remove it altogether. Hopefully we will have this ported back to older versions of Android soon (via support lib).
– Sufian
Jun 27 '16 at 7:17
add a comment |
If you only want to change the progress bar color, you can simply use a color filter in your Activity's onCreate() method:
ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressbar);
int color = 0xFF00FF00;
progressbar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
progressbar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
Idea from here.
You only need to do this for pre-lollipop versions. On Lollipop you can use the colorAccent style attribute.
If you only want to change the progress bar color, you can simply use a color filter in your Activity's onCreate() method:
ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressbar);
int color = 0xFF00FF00;
progressbar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
progressbar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
Idea from here.
You only need to do this for pre-lollipop versions. On Lollipop you can use the colorAccent style attribute.
answered Mar 22 '15 at 20:09
amfcostaamfcosta
6376 silver badges17 bronze badges
6376 silver badges17 bronze badges
ForSeekBar
it is the same, just add for API 16:seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
. This will change the colour of that round thumb.
– Sufian
Oct 1 '15 at 7:07
1
CallingsetColorFilter()
on a drawable directly will apply the colour everywhere. Since this is what I didn't need, I just calledmutate()
on theDrawable
(i.e likeseekBar.getThumb().mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
– Sufian
Oct 1 '15 at 10:22
2
I've seen this solution elsewhere but all it does (testing on a Gingerbread device) is turn the entire progress bar that one color, making it impossible see the actual progress
– Neil C. Obremski
Feb 27 '16 at 22:23
@NeilC.Obremski yes you're right. It might be ROM specific, or maybe version specific. I had to remove it altogether. Hopefully we will have this ported back to older versions of Android soon (via support lib).
– Sufian
Jun 27 '16 at 7:17
add a comment |
ForSeekBar
it is the same, just add for API 16:seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
. This will change the colour of that round thumb.
– Sufian
Oct 1 '15 at 7:07
1
CallingsetColorFilter()
on a drawable directly will apply the colour everywhere. Since this is what I didn't need, I just calledmutate()
on theDrawable
(i.e likeseekBar.getThumb().mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
– Sufian
Oct 1 '15 at 10:22
2
I've seen this solution elsewhere but all it does (testing on a Gingerbread device) is turn the entire progress bar that one color, making it impossible see the actual progress
– Neil C. Obremski
Feb 27 '16 at 22:23
@NeilC.Obremski yes you're right. It might be ROM specific, or maybe version specific. I had to remove it altogether. Hopefully we will have this ported back to older versions of Android soon (via support lib).
– Sufian
Jun 27 '16 at 7:17
For
SeekBar
it is the same, just add for API 16: seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
. This will change the colour of that round thumb.– Sufian
Oct 1 '15 at 7:07
For
SeekBar
it is the same, just add for API 16: seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
. This will change the colour of that round thumb.– Sufian
Oct 1 '15 at 7:07
1
1
Calling
setColorFilter()
on a drawable directly will apply the colour everywhere. Since this is what I didn't need, I just called mutate()
on the Drawable
(i.e like seekBar.getThumb().mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
– Sufian
Oct 1 '15 at 10:22
Calling
setColorFilter()
on a drawable directly will apply the colour everywhere. Since this is what I didn't need, I just called mutate()
on the Drawable
(i.e like seekBar.getThumb().mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
– Sufian
Oct 1 '15 at 10:22
2
2
I've seen this solution elsewhere but all it does (testing on a Gingerbread device) is turn the entire progress bar that one color, making it impossible see the actual progress
– Neil C. Obremski
Feb 27 '16 at 22:23
I've seen this solution elsewhere but all it does (testing on a Gingerbread device) is turn the entire progress bar that one color, making it impossible see the actual progress
– Neil C. Obremski
Feb 27 '16 at 22:23
@NeilC.Obremski yes you're right. It might be ROM specific, or maybe version specific. I had to remove it altogether. Hopefully we will have this ported back to older versions of Android soon (via support lib).
– Sufian
Jun 27 '16 at 7:17
@NeilC.Obremski yes you're right. It might be ROM specific, or maybe version specific. I had to remove it altogether. Hopefully we will have this ported back to older versions of Android soon (via support lib).
– Sufian
Jun 27 '16 at 7:17
add a comment |
Just create a style in values/styles.xml
.
<style name="ProgressBarStyle">
<item name="colorAccent">@color/greenLight</item>
</style>
Then set this style as your ProgressBar
theme.
<ProgressBar
android:theme="@style/ProgressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
and doesn't matter your progress bar is horizontal or circular.
That's all.
1
Wow, the simpliest solution, works for me. I wondered why this answer has so many votes.
– comm1x
Dec 27 '17 at 16:54
1
Did not work for me for Indeterminate ProgressBar
– Vadim Kotov
Feb 12 at 15:56
add a comment |
Just create a style in values/styles.xml
.
<style name="ProgressBarStyle">
<item name="colorAccent">@color/greenLight</item>
</style>
Then set this style as your ProgressBar
theme.
<ProgressBar
android:theme="@style/ProgressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
and doesn't matter your progress bar is horizontal or circular.
That's all.
1
Wow, the simpliest solution, works for me. I wondered why this answer has so many votes.
– comm1x
Dec 27 '17 at 16:54
1
Did not work for me for Indeterminate ProgressBar
– Vadim Kotov
Feb 12 at 15:56
add a comment |
Just create a style in values/styles.xml
.
<style name="ProgressBarStyle">
<item name="colorAccent">@color/greenLight</item>
</style>
Then set this style as your ProgressBar
theme.
<ProgressBar
android:theme="@style/ProgressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
and doesn't matter your progress bar is horizontal or circular.
That's all.
Just create a style in values/styles.xml
.
<style name="ProgressBarStyle">
<item name="colorAccent">@color/greenLight</item>
</style>
Then set this style as your ProgressBar
theme.
<ProgressBar
android:theme="@style/ProgressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
and doesn't matter your progress bar is horizontal or circular.
That's all.
edited Mar 13 '18 at 14:09
answered Sep 25 '17 at 11:00
Ali ZeynaliAli Zeynali
1,0969 silver badges19 bronze badges
1,0969 silver badges19 bronze badges
1
Wow, the simpliest solution, works for me. I wondered why this answer has so many votes.
– comm1x
Dec 27 '17 at 16:54
1
Did not work for me for Indeterminate ProgressBar
– Vadim Kotov
Feb 12 at 15:56
add a comment |
1
Wow, the simpliest solution, works for me. I wondered why this answer has so many votes.
– comm1x
Dec 27 '17 at 16:54
1
Did not work for me for Indeterminate ProgressBar
– Vadim Kotov
Feb 12 at 15:56
1
1
Wow, the simpliest solution, works for me. I wondered why this answer has so many votes.
– comm1x
Dec 27 '17 at 16:54
Wow, the simpliest solution, works for me. I wondered why this answer has so many votes.
– comm1x
Dec 27 '17 at 16:54
1
1
Did not work for me for Indeterminate ProgressBar
– Vadim Kotov
Feb 12 at 15:56
Did not work for me for Indeterminate ProgressBar
– Vadim Kotov
Feb 12 at 15:56
add a comment |
for API level 21 or higher just use
android:progressBackgroundTint="@color/yourBackgroundColor"
android:progressTint="@color/yourColor"
add a comment |
for API level 21 or higher just use
android:progressBackgroundTint="@color/yourBackgroundColor"
android:progressTint="@color/yourColor"
add a comment |
for API level 21 or higher just use
android:progressBackgroundTint="@color/yourBackgroundColor"
android:progressTint="@color/yourColor"
for API level 21 or higher just use
android:progressBackgroundTint="@color/yourBackgroundColor"
android:progressTint="@color/yourColor"
answered Dec 3 '16 at 7:42
mohammad mousavimohammad mousavi
1791 silver badge4 bronze badges
1791 silver badge4 bronze badges
add a comment |
add a comment |
There are 3 ways to solve this question:
- How to adjust the progressbar color declaratively
- How to adjust the progressbar color programmatically, but choose the color from various predetermined colors declared before compile-time
- How to adjust the progressbar color programmatically, and also create the color programatically.
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)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/my_progress_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="@+id/my_secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ff171d"
android:centerColor="#80ff1315"
android:centerY="0.75"
android:endColor="#a0ff0208"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="@+id/my_progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#7d2afdff"
android:centerColor="#ff2afdff"
android:centerY="0.75"
android:endColor="#ff22b9ba"
android:angle="270"
/>
</shape>
</clip>
</item>
In your Java:
final Drawable drawable;
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < 16)
drawable = ctx.getResources().getDrawable(R.drawable.my_progressbar);
else
drawable = ContextCompat.getDrawable(ctx, R.drawable.my_progressbar);
progressBar.setProgressDrawable(drawable)
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.
- Layer 1 is the background
- Layer 2 is the secondary progress color
- Layer 3 is the main progress bar color
In the example below I'll change the color of the main progress bar to cyan.
//Create new ClipDrawable to replace the old one
float pxCornerRadius = viewUtils.convertDpToPixel(5);
final float[] roundedCorners = new float[] pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius ;
ShapeDrawable shpDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));
shpDrawable.getPaint().setColor(Color.CYAN);
final ClipDrawable newProgressClip = new ClipDrawable(shpDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
//Replace the existing ClipDrawable with this new one
final LayerDrawable layers = (LayerDrawable) progressBar.getProgressDrawable();
layers.setDrawableByLayerId(R.id.my_progress, newProgressClip);
That example set it to a solid color. You may set it to a gradient color by replacing
shpDrawable.getPaint().setColor(Color.CYAN);
with
Shader shader = new LinearGradient(0, 0, 0, progressBar.getHeight(), Color.WHITE, Color.BLACK, Shader.TileMode.CLAMP);
shpDrawable.getPaint().setShader(shader);
Cheers.
-Lance
what's viewUtils in viewUtils.convertDpToPixel(5);
– Nikos Hidalgo
Jul 11 at 15:57
1
Hi @NikosHidalgo - You can see a nearly identical method here: stackoverflow.com/questions/12849366/…. The only difference is that I construct myViewUtils
with a reference toContext
, so that I don't need to pass aContext
when callingconvertDpToPixel()
– lance.dolan
Jul 11 at 16:04
Thank you for your prompt reply, especially on an answer that was posted so long ago!
– Nikos Hidalgo
Jul 11 at 16:06
Haha, I really had to stretch my brain to remember :p
– lance.dolan
Jul 11 at 16:09
add a comment |
There are 3 ways to solve this question:
- How to adjust the progressbar color declaratively
- How to adjust the progressbar color programmatically, but choose the color from various predetermined colors declared before compile-time
- How to adjust the progressbar color programmatically, and also create the color programatically.
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)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/my_progress_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="@+id/my_secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ff171d"
android:centerColor="#80ff1315"
android:centerY="0.75"
android:endColor="#a0ff0208"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="@+id/my_progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#7d2afdff"
android:centerColor="#ff2afdff"
android:centerY="0.75"
android:endColor="#ff22b9ba"
android:angle="270"
/>
</shape>
</clip>
</item>
In your Java:
final Drawable drawable;
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < 16)
drawable = ctx.getResources().getDrawable(R.drawable.my_progressbar);
else
drawable = ContextCompat.getDrawable(ctx, R.drawable.my_progressbar);
progressBar.setProgressDrawable(drawable)
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.
- Layer 1 is the background
- Layer 2 is the secondary progress color
- Layer 3 is the main progress bar color
In the example below I'll change the color of the main progress bar to cyan.
//Create new ClipDrawable to replace the old one
float pxCornerRadius = viewUtils.convertDpToPixel(5);
final float[] roundedCorners = new float[] pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius ;
ShapeDrawable shpDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));
shpDrawable.getPaint().setColor(Color.CYAN);
final ClipDrawable newProgressClip = new ClipDrawable(shpDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
//Replace the existing ClipDrawable with this new one
final LayerDrawable layers = (LayerDrawable) progressBar.getProgressDrawable();
layers.setDrawableByLayerId(R.id.my_progress, newProgressClip);
That example set it to a solid color. You may set it to a gradient color by replacing
shpDrawable.getPaint().setColor(Color.CYAN);
with
Shader shader = new LinearGradient(0, 0, 0, progressBar.getHeight(), Color.WHITE, Color.BLACK, Shader.TileMode.CLAMP);
shpDrawable.getPaint().setShader(shader);
Cheers.
-Lance
what's viewUtils in viewUtils.convertDpToPixel(5);
– Nikos Hidalgo
Jul 11 at 15:57
1
Hi @NikosHidalgo - You can see a nearly identical method here: stackoverflow.com/questions/12849366/…. The only difference is that I construct myViewUtils
with a reference toContext
, so that I don't need to pass aContext
when callingconvertDpToPixel()
– lance.dolan
Jul 11 at 16:04
Thank you for your prompt reply, especially on an answer that was posted so long ago!
– Nikos Hidalgo
Jul 11 at 16:06
Haha, I really had to stretch my brain to remember :p
– lance.dolan
Jul 11 at 16:09
add a comment |
There are 3 ways to solve this question:
- How to adjust the progressbar color declaratively
- How to adjust the progressbar color programmatically, but choose the color from various predetermined colors declared before compile-time
- How to adjust the progressbar color programmatically, and also create the color programatically.
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)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/my_progress_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="@+id/my_secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ff171d"
android:centerColor="#80ff1315"
android:centerY="0.75"
android:endColor="#a0ff0208"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="@+id/my_progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#7d2afdff"
android:centerColor="#ff2afdff"
android:centerY="0.75"
android:endColor="#ff22b9ba"
android:angle="270"
/>
</shape>
</clip>
</item>
In your Java:
final Drawable drawable;
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < 16)
drawable = ctx.getResources().getDrawable(R.drawable.my_progressbar);
else
drawable = ContextCompat.getDrawable(ctx, R.drawable.my_progressbar);
progressBar.setProgressDrawable(drawable)
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.
- Layer 1 is the background
- Layer 2 is the secondary progress color
- Layer 3 is the main progress bar color
In the example below I'll change the color of the main progress bar to cyan.
//Create new ClipDrawable to replace the old one
float pxCornerRadius = viewUtils.convertDpToPixel(5);
final float[] roundedCorners = new float[] pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius ;
ShapeDrawable shpDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));
shpDrawable.getPaint().setColor(Color.CYAN);
final ClipDrawable newProgressClip = new ClipDrawable(shpDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
//Replace the existing ClipDrawable with this new one
final LayerDrawable layers = (LayerDrawable) progressBar.getProgressDrawable();
layers.setDrawableByLayerId(R.id.my_progress, newProgressClip);
That example set it to a solid color. You may set it to a gradient color by replacing
shpDrawable.getPaint().setColor(Color.CYAN);
with
Shader shader = new LinearGradient(0, 0, 0, progressBar.getHeight(), Color.WHITE, Color.BLACK, Shader.TileMode.CLAMP);
shpDrawable.getPaint().setShader(shader);
Cheers.
-Lance
There are 3 ways to solve this question:
- How to adjust the progressbar color declaratively
- How to adjust the progressbar color programmatically, but choose the color from various predetermined colors declared before compile-time
- How to adjust the progressbar color programmatically, and also create the color programatically.
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)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/my_progress_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="@+id/my_secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ff171d"
android:centerColor="#80ff1315"
android:centerY="0.75"
android:endColor="#a0ff0208"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="@+id/my_progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#7d2afdff"
android:centerColor="#ff2afdff"
android:centerY="0.75"
android:endColor="#ff22b9ba"
android:angle="270"
/>
</shape>
</clip>
</item>
In your Java:
final Drawable drawable;
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < 16)
drawable = ctx.getResources().getDrawable(R.drawable.my_progressbar);
else
drawable = ContextCompat.getDrawable(ctx, R.drawable.my_progressbar);
progressBar.setProgressDrawable(drawable)
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.
- Layer 1 is the background
- Layer 2 is the secondary progress color
- Layer 3 is the main progress bar color
In the example below I'll change the color of the main progress bar to cyan.
//Create new ClipDrawable to replace the old one
float pxCornerRadius = viewUtils.convertDpToPixel(5);
final float[] roundedCorners = new float[] pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius ;
ShapeDrawable shpDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));
shpDrawable.getPaint().setColor(Color.CYAN);
final ClipDrawable newProgressClip = new ClipDrawable(shpDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
//Replace the existing ClipDrawable with this new one
final LayerDrawable layers = (LayerDrawable) progressBar.getProgressDrawable();
layers.setDrawableByLayerId(R.id.my_progress, newProgressClip);
That example set it to a solid color. You may set it to a gradient color by replacing
shpDrawable.getPaint().setColor(Color.CYAN);
with
Shader shader = new LinearGradient(0, 0, 0, progressBar.getHeight(), Color.WHITE, Color.BLACK, Shader.TileMode.CLAMP);
shpDrawable.getPaint().setShader(shader);
Cheers.
-Lance
edited Aug 3 '17 at 23:10
answered Oct 19 '15 at 20:01
lance.dolanlance.dolan
1,16111 silver badges20 bronze badges
1,16111 silver badges20 bronze badges
what's viewUtils in viewUtils.convertDpToPixel(5);
– Nikos Hidalgo
Jul 11 at 15:57
1
Hi @NikosHidalgo - You can see a nearly identical method here: stackoverflow.com/questions/12849366/…. The only difference is that I construct myViewUtils
with a reference toContext
, so that I don't need to pass aContext
when callingconvertDpToPixel()
– lance.dolan
Jul 11 at 16:04
Thank you for your prompt reply, especially on an answer that was posted so long ago!
– Nikos Hidalgo
Jul 11 at 16:06
Haha, I really had to stretch my brain to remember :p
– lance.dolan
Jul 11 at 16:09
add a comment |
what's viewUtils in viewUtils.convertDpToPixel(5);
– Nikos Hidalgo
Jul 11 at 15:57
1
Hi @NikosHidalgo - You can see a nearly identical method here: stackoverflow.com/questions/12849366/…. The only difference is that I construct myViewUtils
with a reference toContext
, so that I don't need to pass aContext
when callingconvertDpToPixel()
– lance.dolan
Jul 11 at 16:04
Thank you for your prompt reply, especially on an answer that was posted so long ago!
– Nikos Hidalgo
Jul 11 at 16:06
Haha, I really had to stretch my brain to remember :p
– lance.dolan
Jul 11 at 16:09
what's viewUtils in viewUtils.convertDpToPixel(5);
– Nikos Hidalgo
Jul 11 at 15:57
what's viewUtils in viewUtils.convertDpToPixel(5);
– Nikos Hidalgo
Jul 11 at 15:57
1
1
Hi @NikosHidalgo - You can see a nearly identical method here: stackoverflow.com/questions/12849366/…. The only difference is that I construct my
ViewUtils
with a reference to Context
, so that I don't need to pass a Context
when calling convertDpToPixel()
– lance.dolan
Jul 11 at 16:04
Hi @NikosHidalgo - You can see a nearly identical method here: stackoverflow.com/questions/12849366/…. The only difference is that I construct my
ViewUtils
with a reference to Context
, so that I don't need to pass a Context
when calling convertDpToPixel()
– lance.dolan
Jul 11 at 16:04
Thank you for your prompt reply, especially on an answer that was posted so long ago!
– Nikos Hidalgo
Jul 11 at 16:06
Thank you for your prompt reply, especially on an answer that was posted so long ago!
– Nikos Hidalgo
Jul 11 at 16:06
Haha, I really had to stretch my brain to remember :p
– lance.dolan
Jul 11 at 16:09
Haha, I really had to stretch my brain to remember :p
– lance.dolan
Jul 11 at 16:09
add a comment |
The simplest solution I found out is something like this:
<item name="colorAccent">@color/white_or_any_color</item>
Put the above in styles.xml
file under res > values
folder.
NOTE: If you use any other accent color, then the previous solutions should be good to go with.
API 21 or HIGHER
3
This works only for API Level >= 21
– amfcosta
Mar 22 '15 at 0:16
Yes, sorry about not indicating it in the post. Thanks :)
– Sazid
Mar 23 '15 at 2:23
add a comment |
The simplest solution I found out is something like this:
<item name="colorAccent">@color/white_or_any_color</item>
Put the above in styles.xml
file under res > values
folder.
NOTE: If you use any other accent color, then the previous solutions should be good to go with.
API 21 or HIGHER
3
This works only for API Level >= 21
– amfcosta
Mar 22 '15 at 0:16
Yes, sorry about not indicating it in the post. Thanks :)
– Sazid
Mar 23 '15 at 2:23
add a comment |
The simplest solution I found out is something like this:
<item name="colorAccent">@color/white_or_any_color</item>
Put the above in styles.xml
file under res > values
folder.
NOTE: If you use any other accent color, then the previous solutions should be good to go with.
API 21 or HIGHER
The simplest solution I found out is something like this:
<item name="colorAccent">@color/white_or_any_color</item>
Put the above in styles.xml
file under res > values
folder.
NOTE: If you use any other accent color, then the previous solutions should be good to go with.
API 21 or HIGHER
edited Mar 23 '15 at 2:24
answered Feb 19 '15 at 13:40
SazidSazid
1,9521 gold badge13 silver badges27 bronze badges
1,9521 gold badge13 silver badges27 bronze badges
3
This works only for API Level >= 21
– amfcosta
Mar 22 '15 at 0:16
Yes, sorry about not indicating it in the post. Thanks :)
– Sazid
Mar 23 '15 at 2:23
add a comment |
3
This works only for API Level >= 21
– amfcosta
Mar 22 '15 at 0:16
Yes, sorry about not indicating it in the post. Thanks :)
– Sazid
Mar 23 '15 at 2:23
3
3
This works only for API Level >= 21
– amfcosta
Mar 22 '15 at 0:16
This works only for API Level >= 21
– amfcosta
Mar 22 '15 at 0:16
Yes, sorry about not indicating it in the post. Thanks :)
– Sazid
Mar 23 '15 at 2:23
Yes, sorry about not indicating it in the post. Thanks :)
– Sazid
Mar 23 '15 at 2:23
add a comment |
For anyone looking for how to do it programmatically:
Drawable bckgrndDr = new ColorDrawable(Color.RED);
Drawable secProgressDr = new ColorDrawable(Color.GRAY);
Drawable progressDr = new ScaleDrawable(new ColorDrawable(Color.BLUE), Gravity.LEFT, 1, -1);
LayerDrawable resultDr = new LayerDrawable(new Drawable[] bckgrndDr, secProgressDr, progressDr );
//setting ids is important
resultDr.setId(0, android.R.id.background);
resultDr.setId(1, android.R.id.secondaryProgress);
resultDr.setId(2, android.R.id.progress);
Setting ids to drawables is crucial, and takes care of preserving bounds and actual state of progress bar
Nice one - this is exactly what I needed - I didn't have the ID's.
– Ewald
Apr 6 '16 at 12:55
add a comment |
For anyone looking for how to do it programmatically:
Drawable bckgrndDr = new ColorDrawable(Color.RED);
Drawable secProgressDr = new ColorDrawable(Color.GRAY);
Drawable progressDr = new ScaleDrawable(new ColorDrawable(Color.BLUE), Gravity.LEFT, 1, -1);
LayerDrawable resultDr = new LayerDrawable(new Drawable[] bckgrndDr, secProgressDr, progressDr );
//setting ids is important
resultDr.setId(0, android.R.id.background);
resultDr.setId(1, android.R.id.secondaryProgress);
resultDr.setId(2, android.R.id.progress);
Setting ids to drawables is crucial, and takes care of preserving bounds and actual state of progress bar
Nice one - this is exactly what I needed - I didn't have the ID's.
– Ewald
Apr 6 '16 at 12:55
add a comment |
For anyone looking for how to do it programmatically:
Drawable bckgrndDr = new ColorDrawable(Color.RED);
Drawable secProgressDr = new ColorDrawable(Color.GRAY);
Drawable progressDr = new ScaleDrawable(new ColorDrawable(Color.BLUE), Gravity.LEFT, 1, -1);
LayerDrawable resultDr = new LayerDrawable(new Drawable[] bckgrndDr, secProgressDr, progressDr );
//setting ids is important
resultDr.setId(0, android.R.id.background);
resultDr.setId(1, android.R.id.secondaryProgress);
resultDr.setId(2, android.R.id.progress);
Setting ids to drawables is crucial, and takes care of preserving bounds and actual state of progress bar
For anyone looking for how to do it programmatically:
Drawable bckgrndDr = new ColorDrawable(Color.RED);
Drawable secProgressDr = new ColorDrawable(Color.GRAY);
Drawable progressDr = new ScaleDrawable(new ColorDrawable(Color.BLUE), Gravity.LEFT, 1, -1);
LayerDrawable resultDr = new LayerDrawable(new Drawable[] bckgrndDr, secProgressDr, progressDr );
//setting ids is important
resultDr.setId(0, android.R.id.background);
resultDr.setId(1, android.R.id.secondaryProgress);
resultDr.setId(2, android.R.id.progress);
Setting ids to drawables is crucial, and takes care of preserving bounds and actual state of progress bar
answered Oct 1 '15 at 9:14
ŠtarkeŠtarke
6046 silver badges8 bronze badges
6046 silver badges8 bronze badges
Nice one - this is exactly what I needed - I didn't have the ID's.
– Ewald
Apr 6 '16 at 12:55
add a comment |
Nice one - this is exactly what I needed - I didn't have the ID's.
– Ewald
Apr 6 '16 at 12:55
Nice one - this is exactly what I needed - I didn't have the ID's.
– Ewald
Apr 6 '16 at 12:55
Nice one - this is exactly what I needed - I didn't have the ID's.
– Ewald
Apr 6 '16 at 12:55
add a comment |
final LayerDrawable layers = (LayerDrawable) progressBar.getProgressDrawable();
layers.getDrawable(2).setColorFilter(color,PorterDuff.Mode.SRC_IN);
1
Try to add a little explanation to your answer. Code only answers are discouraged.
– Alexei
Jun 9 '16 at 6:55
I'd love to get an explanation behind the index 2 in getDrawable, but at least this was the only solution that worked for me, so thanks!
– Nikos Hidalgo
Jul 11 at 16:02
add a comment |
final LayerDrawable layers = (LayerDrawable) progressBar.getProgressDrawable();
layers.getDrawable(2).setColorFilter(color,PorterDuff.Mode.SRC_IN);
1
Try to add a little explanation to your answer. Code only answers are discouraged.
– Alexei
Jun 9 '16 at 6:55
I'd love to get an explanation behind the index 2 in getDrawable, but at least this was the only solution that worked for me, so thanks!
– Nikos Hidalgo
Jul 11 at 16:02
add a comment |
final LayerDrawable layers = (LayerDrawable) progressBar.getProgressDrawable();
layers.getDrawable(2).setColorFilter(color,PorterDuff.Mode.SRC_IN);
final LayerDrawable layers = (LayerDrawable) progressBar.getProgressDrawable();
layers.getDrawable(2).setColorFilter(color,PorterDuff.Mode.SRC_IN);
answered Jun 9 '16 at 6:47
user3135773user3135773
111 bronze badge
111 bronze badge
1
Try to add a little explanation to your answer. Code only answers are discouraged.
– Alexei
Jun 9 '16 at 6:55
I'd love to get an explanation behind the index 2 in getDrawable, but at least this was the only solution that worked for me, so thanks!
– Nikos Hidalgo
Jul 11 at 16:02
add a comment |
1
Try to add a little explanation to your answer. Code only answers are discouraged.
– Alexei
Jun 9 '16 at 6:55
I'd love to get an explanation behind the index 2 in getDrawable, but at least this was the only solution that worked for me, so thanks!
– Nikos Hidalgo
Jul 11 at 16:02
1
1
Try to add a little explanation to your answer. Code only answers are discouraged.
– Alexei
Jun 9 '16 at 6:55
Try to add a little explanation to your answer. Code only answers are discouraged.
– Alexei
Jun 9 '16 at 6:55
I'd love to get an explanation behind the index 2 in getDrawable, but at least this was the only solution that worked for me, so thanks!
– Nikos Hidalgo
Jul 11 at 16:02
I'd love to get an explanation behind the index 2 in getDrawable, but at least this was the only solution that worked for me, so thanks!
– Nikos Hidalgo
Jul 11 at 16:02
add a comment |
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:
public void setPrimaryProgressColor(int colorInstance)
if (progressBar.getProgressDrawable() instanceof LayerDrawable)
Log.d(mLogTag, "Drawable is a layer drawable");
LayerDrawable layered = (LayerDrawable) progressBar.getProgressDrawable();
Drawable circleDrawableExample = layered.getDrawable(<whichever is your index of android.R.id.progress>);
circleDrawableExample.setColorFilter(colorInstance, PorterDuff.Mode.SRC_IN);
progressBar.setProgressDrawable(layered);
else
Log.d(mLogTag, "Fallback in case it's not a LayerDrawable");
progressBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
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.
add a comment |
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:
public void setPrimaryProgressColor(int colorInstance)
if (progressBar.getProgressDrawable() instanceof LayerDrawable)
Log.d(mLogTag, "Drawable is a layer drawable");
LayerDrawable layered = (LayerDrawable) progressBar.getProgressDrawable();
Drawable circleDrawableExample = layered.getDrawable(<whichever is your index of android.R.id.progress>);
circleDrawableExample.setColorFilter(colorInstance, PorterDuff.Mode.SRC_IN);
progressBar.setProgressDrawable(layered);
else
Log.d(mLogTag, "Fallback in case it's not a LayerDrawable");
progressBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
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.
add a comment |
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:
public void setPrimaryProgressColor(int colorInstance)
if (progressBar.getProgressDrawable() instanceof LayerDrawable)
Log.d(mLogTag, "Drawable is a layer drawable");
LayerDrawable layered = (LayerDrawable) progressBar.getProgressDrawable();
Drawable circleDrawableExample = layered.getDrawable(<whichever is your index of android.R.id.progress>);
circleDrawableExample.setColorFilter(colorInstance, PorterDuff.Mode.SRC_IN);
progressBar.setProgressDrawable(layered);
else
Log.d(mLogTag, "Fallback in case it's not a LayerDrawable");
progressBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
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.
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:
public void setPrimaryProgressColor(int colorInstance)
if (progressBar.getProgressDrawable() instanceof LayerDrawable)
Log.d(mLogTag, "Drawable is a layer drawable");
LayerDrawable layered = (LayerDrawable) progressBar.getProgressDrawable();
Drawable circleDrawableExample = layered.getDrawable(<whichever is your index of android.R.id.progress>);
circleDrawableExample.setColorFilter(colorInstance, PorterDuff.Mode.SRC_IN);
progressBar.setProgressDrawable(layered);
else
Log.d(mLogTag, "Fallback in case it's not a LayerDrawable");
progressBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
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.
edited Jun 30 '16 at 15:06
answered Jun 30 '16 at 14:54
Pier BetosPier Betos
4772 silver badges13 bronze badges
4772 silver badges13 bronze badges
add a comment |
add a comment |
ProgressBar color can be changed as follows:
/res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorAccent">#FF4081</color>
</resources>
/res/values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorAccent">@color/colorAccent</item>
</style>
onCreate:
progressBar = (ProgressBar) findViewById(R.id.progressBar);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
Drawable drawable = progressBar.getIndeterminateDrawable().mutate();
drawable.setColorFilter(ContextCompat.getColor(this, R.color.colorAccent), PorterDuff.Mode.SRC_IN);
progressBar.setIndeterminateDrawable(drawable);
add a comment |
ProgressBar color can be changed as follows:
/res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorAccent">#FF4081</color>
</resources>
/res/values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorAccent">@color/colorAccent</item>
</style>
onCreate:
progressBar = (ProgressBar) findViewById(R.id.progressBar);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
Drawable drawable = progressBar.getIndeterminateDrawable().mutate();
drawable.setColorFilter(ContextCompat.getColor(this, R.color.colorAccent), PorterDuff.Mode.SRC_IN);
progressBar.setIndeterminateDrawable(drawable);
add a comment |
ProgressBar color can be changed as follows:
/res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorAccent">#FF4081</color>
</resources>
/res/values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorAccent">@color/colorAccent</item>
</style>
onCreate:
progressBar = (ProgressBar) findViewById(R.id.progressBar);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
Drawable drawable = progressBar.getIndeterminateDrawable().mutate();
drawable.setColorFilter(ContextCompat.getColor(this, R.color.colorAccent), PorterDuff.Mode.SRC_IN);
progressBar.setIndeterminateDrawable(drawable);
ProgressBar color can be changed as follows:
/res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorAccent">#FF4081</color>
</resources>
/res/values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorAccent">@color/colorAccent</item>
</style>
onCreate:
progressBar = (ProgressBar) findViewById(R.id.progressBar);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
Drawable drawable = progressBar.getIndeterminateDrawable().mutate();
drawable.setColorFilter(ContextCompat.getColor(this, R.color.colorAccent), PorterDuff.Mode.SRC_IN);
progressBar.setIndeterminateDrawable(drawable);
answered Sep 25 '16 at 14:22
Milan HlinákMilan Hlinák
1,9101 gold badge18 silver badges28 bronze badges
1,9101 gold badge18 silver badges28 bronze badges
add a comment |
add a comment |
If you are in API level 21 or above you can use these XML attributes:
<!-- For indeterminate progress bar -->
android:indeterminateTint="@color/white"
<!-- For normal progress bar -->
android:progressTint="@color/white"
add a comment |
If you are in API level 21 or above you can use these XML attributes:
<!-- For indeterminate progress bar -->
android:indeterminateTint="@color/white"
<!-- For normal progress bar -->
android:progressTint="@color/white"
add a comment |
If you are in API level 21 or above you can use these XML attributes:
<!-- For indeterminate progress bar -->
android:indeterminateTint="@color/white"
<!-- For normal progress bar -->
android:progressTint="@color/white"
If you are in API level 21 or above you can use these XML attributes:
<!-- For indeterminate progress bar -->
android:indeterminateTint="@color/white"
<!-- For normal progress bar -->
android:progressTint="@color/white"
edited Mar 26 at 13:33
icarovirtual
5268 silver badges26 bronze badges
5268 silver badges26 bronze badges
answered Nov 13 '18 at 14:39
Ghanshyam NaymaGhanshyam Nayma
78510 silver badges17 bronze badges
78510 silver badges17 bronze badges
add a comment |
add a comment |
Create a drawable resource what background you need, in my case I named it bg_custom_progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<item>
<shape android:shape="rectangle" >
<corners android:radius="5dp"/>
<gradient
android:angle="180"
android:endColor="#DADFD6"
android:startColor="#AEB9A3" />
</shape>
</item>
<item>
<clip>
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<gradient
android:angle="180"
android:endColor="#44CF4A"
android:startColor="#2BB930" />
</shape>
</clip>
</item>
<item>
<clip>
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<gradient
android:angle="180"
android:endColor="#44CF4A"
android:startColor="#2BB930" />
</shape>
</clip>
</item>
Then use this custom background like
<ProgressBar
android:id="@+id/progressBarBarMenuHome"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="6dp"
android:indeterminate="false"
android:max="100"
android:minWidth="200dp"
android:minHeight="50dp"
android:progress="10"
android:progressDrawable="@drawable/bg_custom_progressbar" />
Its works fine for me
add a comment |
Create a drawable resource what background you need, in my case I named it bg_custom_progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<item>
<shape android:shape="rectangle" >
<corners android:radius="5dp"/>
<gradient
android:angle="180"
android:endColor="#DADFD6"
android:startColor="#AEB9A3" />
</shape>
</item>
<item>
<clip>
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<gradient
android:angle="180"
android:endColor="#44CF4A"
android:startColor="#2BB930" />
</shape>
</clip>
</item>
<item>
<clip>
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<gradient
android:angle="180"
android:endColor="#44CF4A"
android:startColor="#2BB930" />
</shape>
</clip>
</item>
Then use this custom background like
<ProgressBar
android:id="@+id/progressBarBarMenuHome"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="6dp"
android:indeterminate="false"
android:max="100"
android:minWidth="200dp"
android:minHeight="50dp"
android:progress="10"
android:progressDrawable="@drawable/bg_custom_progressbar" />
Its works fine for me
add a comment |
Create a drawable resource what background you need, in my case I named it bg_custom_progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<item>
<shape android:shape="rectangle" >
<corners android:radius="5dp"/>
<gradient
android:angle="180"
android:endColor="#DADFD6"
android:startColor="#AEB9A3" />
</shape>
</item>
<item>
<clip>
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<gradient
android:angle="180"
android:endColor="#44CF4A"
android:startColor="#2BB930" />
</shape>
</clip>
</item>
<item>
<clip>
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<gradient
android:angle="180"
android:endColor="#44CF4A"
android:startColor="#2BB930" />
</shape>
</clip>
</item>
Then use this custom background like
<ProgressBar
android:id="@+id/progressBarBarMenuHome"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="6dp"
android:indeterminate="false"
android:max="100"
android:minWidth="200dp"
android:minHeight="50dp"
android:progress="10"
android:progressDrawable="@drawable/bg_custom_progressbar" />
Its works fine for me
Create a drawable resource what background you need, in my case I named it bg_custom_progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<item>
<shape android:shape="rectangle" >
<corners android:radius="5dp"/>
<gradient
android:angle="180"
android:endColor="#DADFD6"
android:startColor="#AEB9A3" />
</shape>
</item>
<item>
<clip>
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<gradient
android:angle="180"
android:endColor="#44CF4A"
android:startColor="#2BB930" />
</shape>
</clip>
</item>
<item>
<clip>
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<gradient
android:angle="180"
android:endColor="#44CF4A"
android:startColor="#2BB930" />
</shape>
</clip>
</item>
Then use this custom background like
<ProgressBar
android:id="@+id/progressBarBarMenuHome"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="6dp"
android:indeterminate="false"
android:max="100"
android:minWidth="200dp"
android:minHeight="50dp"
android:progress="10"
android:progressDrawable="@drawable/bg_custom_progressbar" />
Its works fine for me
answered Apr 12 at 8:30
Juyel RanaJuyel Rana
1161 silver badge7 bronze badges
1161 silver badge7 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5745814%2fandroid-change-horizontal-progress-bar-color%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
above answers change the whole background color and it's ambigios that it change the height of progress bar to very max.which unable to change in xml. better way to change only progress bar status that how much is completed i.s 20% red color, 50% yellow color 70% and above green color. you can do it programmatically in java. share your answer if you have any other solution.
– Mubashar
Mar 17 '17 at 9:52