Justin
Justin

Reputation: 2142

How to set the background image of an ImageButton?

I tried to use the following code to make my ImageButton invisible, but clicking it did not do anything. In other words, the method that it was supposed to call did not do anything (it is supposed to change the image using the setImageResource function).

die1button.setBackgroundColor(android.R.color.transparent);

So, how can I change the background image in Java, or make it completely clear, while still allowing the button to work? I would prefer to change the image, but I have not figured out how to do that yet.

Upvotes: 0

Views: 1735

Answers (2)

user802421
user802421

Reputation: 7505

I would set the image in the xml file.

<ImageButton
    android:id="@+id/die1button"
    android:drawable="@drawable/image" // <-- your image resource
    ...>
</ImageButton>

Upvotes: 1

Justin
Justin

Reputation: 2142

I figured it out.

die1button.setBackgroundResource(R.drawable.image);

I guess I just wasn't quite thinking enough. I would still like of hear of any better ways to do this.

Upvotes: 1

Related Questions