Mark Worsnop
Mark Worsnop

Reputation: 4467

android how to change the progressbar color programatically

How can you change the progressbar color from within the app. I already have it set in the XML but as the value gets within preset thresholds I need to change the color of the bar to alert the user. For the buttons I can set it like the example below. Is there a way to do with with the progressbar?

B.setBackgroundDrawable(c.getResources().getDrawable(R.drawable.button2));
B.getBackground().setColorFilter( UserS.ColorBtnBack, PorterDuff.Mode.MULTIPLY);

Upvotes: 1

Views: 5026

Answers (2)

CommonsWare
CommonsWare

Reputation: 1007554

ProgressBar uses a LevelListDrawable with setProgressDrawable() to determine the color of the bar. You can try assembling a LevelListDrawable in Java, though I have never tried that -- I have only defined them via XML.

Upvotes: 1

Arve
Arve

Reputation: 8128

I posted a tip as a comment on your other question, see http://colintmiller.com/2010/10/07/how-to-add-text-over-a-progress-bar-on-android/

Scroll down to "Bonus Tip", which discusses changing color. I guess you want a predefined set of colors, which you could then switch between using setProgressDrawable()

Upvotes: 1

Related Questions