Amit
Amit

Reputation: 11

How to set the background color for button in Android during run time?

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

Upvotes: 1

Views: 7279

Answers (6)

SANCY DEVASSY
SANCY DEVASSY

Reputation: 5

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

Upvotes: 0

Chinmay Dabke
Chinmay Dabke

Reputation: 5140

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

Upvotes: 0

Voicu
Voicu

Reputation: 17850

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.

Upvotes: 2

Comic Sans MS Lover
Comic Sans MS Lover

Reputation: 1729

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

Upvotes: 0

Prasham
Prasham

Reputation: 6686

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.

Upvotes: 0

harish
harish

Reputation: 1765

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

Upvotes: 3

Related Questions