Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change progress bar's progress color in Android

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

like image 379
WhiteTigerK Avatar asked Jan 07 '10 14:01

WhiteTigerK


1 Answers

For a horizontal ProgressBar, you can use a ColorFilter, too, like this:

progressBar.getProgressDrawable().setColorFilter(     Color.RED, android.graphics.PorterDuff.Mode.SRC_IN); 

Red ProgressBar using color filter

Note: This modifies the appearance of all progress bars in your app. To only modify one specific progress bar, do this:

Drawable progressDrawable = progressBar.getProgressDrawable().mutate(); progressDrawable.setColorFilter(Color.RED, android.graphics.PorterDuff.Mode.SRC_IN); progressBar.setProgressDrawable(progressDrawable); 

If progressBar is indeterminate then use getIndeterminateDrawable() instead of getProgressDrawable().

Since Lollipop (API 21) you can set a progress tint:

progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED)); 

Red ProgressBar using progress tint

like image 111
Torben Kohlmeier Avatar answered Nov 08 '22 19:11

Torben Kohlmeier