How to set the background color for button in Android during run time?
I have to set the background color duri开发者_如何学JAVAng run time, i am using "setBackgroundColor(R.color.focusColor)" . but its nots working.
use setBackgroundColor(Color.color)" or setBackgroundColor("#000000");
You can use the PorterDuff.Mode
enum with its properties.
((Button)findViewById(R.id.yourButtonResId)).getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
Here's the result containing an unchanged default Pass button and a Submit button on which I applied the line above:
More details on this matter here.
May be this question can help you a bit.
In a nutshell you have to use this.getResources().getColor(R.color.orange)
in argument.
**EDIT: ** Pasted a wrong code. Edited
Try this.
To Google travelers, if this is a Drawable update (e.g. you are going to update your button that already have a drawable, such as button.getBackground.doStuff()
) you need to use Button.invalidateSelf().
public void invalidateSelf()
You can use button.setBackgroundColor(Color.BLUE);
BLUE can be replaced by the color you want if its available!
its simplest way ((Button)findViewById(R.id.yourButtonResId)).setBackground(#ffffff);
精彩评论