user427390
user427390

Reputation:

How can I set the drawableRight resource via code?

I am trying to set an image on the right side of my button after the button has been clicked. I want to do this via code.

I have seen how to change the background resource via code but I am not able to find any examples showing how to change the sides via code. Is it possible?

Upvotes: 12

Views: 12663

Answers (2)

luciano.bustos
luciano.bustos

Reputation: 358

Usually you can change using this

Drawable draw = getResources().getDrawable(R.drawable.facebook);
myButton.setCompoundDrawablesWithIntrinsicBounds(null, null, draw, null);

Be aware you can miss the button text.

Upvotes: 8

eyespyus
eyespyus

Reputation: 1576

You need to use the

public void setCompoundDrawables (Drawable left, Drawable top, Drawable right, 
 Drawable bottom)

method with null for any that are not needed.

Upvotes: 28

Related Questions