1

I have to set the background color during run time, i am using "setBackgroundColor(R.color.focusColor)" . but its nots working.

Amit
  • 11
  • 2
  • 4

6 Answers6

3

use setBackgroundColor(Color.color)" or setBackgroundColor("#000000");

harish
  • 1,755
  • 6
  • 22
  • 36
2

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:

enter image description here

More details on this matter here.

Community
  • 1
  • 1
Voicu
  • 16,921
  • 10
  • 60
  • 69
0

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()

Comic Sans MS Lover
  • 1,729
  • 5
  • 26
  • 52
0

You can use button.setBackgroundColor(Color.BLUE); BLUE can be replaced by the color you want if its available!

Chinmay Dabke
  • 5,070
  • 11
  • 40
  • 63
0

its simplest way ((Button)findViewById(R.id.yourButtonResId)).setBackground(#ffffff);

0

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.

Community
  • 1
  • 1
Prasham
  • 6,646
  • 8
  • 38
  • 55