Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change progressbar color through CODE ONLY in Android

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.

like image 645
hico Avatar asked Jun 08 '12 15:06

hico


People also ask

How do I change the color of my ProgressBar on Kotlin?

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.

What is the default color of progress bar?

I'm using an horizontal progress bar in my Android application, and I want to change its progress color (which is Yellow by default).


2 Answers

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); 
like image 97
Mit Bhatt Avatar answered Sep 30 '22 16:09

Mit Bhatt


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); 
like image 25
brbsBruno Avatar answered Sep 30 '22 17:09

brbsBruno