Tünde
Tünde

Reputation: 885

change button background at runtime in android

I want to change my button background at RUNTIME

I know how to add a custom background to a button, but

Upvotes: 2

Views: 5283

Answers (3)

Avinesh Sharma
Avinesh Sharma

Reputation: 61

There is a simple answer to this problem:

button.setBackgroundResource(R.drawable.backround);

you can also change back to your default backround by using an if statement and repeating the above statement by just changing the "backround" resource to your default resource. Btw thanks for A2A.

Upvotes: 0

Tünde
Tünde

Reputation: 885

I actually found a solution

You can save the initial background of your button in a Drawable object

Drawable d = button.getBackground();

Now you can modify your button's background as you wish

button.setBackgroundResource(R.drawable.custom_button);

Than you can modify it back

button.setBackgroundDrawable(d);

Upvotes: 5

Dediqated
Dediqated

Reputation: 898

does this work? changeableButton.setBackground(null);

Upvotes: 0

Related Questions