I have a progressBar using the ProgressBar class.
Just doing this:
progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
I need to change the color of that one, using input value like so:
int color = "red in RGB value".progressBar.setColor(color)
or something like that...
I can't use an XML layout because the progress bar is customizable for users.
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.
I'm using an horizontal progress bar in my Android application, and I want to change its progress color (which is Yellow by default).
This will help much no need to do so much coding :)
ProgressBar spinner = new android.widget.ProgressBar( context, null, android.R.attr.progressBarStyle); spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000,android.graphics.PorterDuff.Mode.MULTIPLY);
In the case that you need to tint the background and the progress bar in different colors.
progress_drawable.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 android:shape="rectangle" > <solid android:color="@color/white" /> </shape> </item> <item android:id="@android:id/progress"> <clip> <shape> <solid android:color="@color/green" /> </shape> </clip> </item> </layer-list>
Its possible, programmatically, to decompound its layer-list items, and tint them separately:
LayerDrawable progressBarDrawable = (LayerDrawable) progressBar.getProgressDrawable(); Drawable backgroundDrawable = progressBarDrawable.getDrawable(0); Drawable progressDrawable = progressBarDrawable.getDrawable(1); backgroundDrawable.setColorFilter(ContextCompat.getColor(this.getContext(), R.color.white), PorterDuff.Mode.SRC_IN); progressDrawable.setColorFilter(ContextCompat.getColor(this.getContext(), R.color.red), PorterDuff.Mode.SRC_IN);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With