9

I have a transparent ImageButton. When clicked, the ImageResource of the button is set to a drawable in my drawable-hdpi folder (basically an image is shown on top of the transparent ImageButton). I'm using the ImageButton.setImageResource() method in order to do that. My question is, how can I remove the image resource so that there is only a transparent image button again. Of course, I need to be able to do this in java, not XML. I tried the following which failed to work: ImageButton.setImageResource(null); I also looked around a bit and couldn't find an answer... Thanks in advance for any help.

EDIT: Thank you all for your answers.. Péter Varga's answer did exactly what I needed so that's what i'm going with.

corecase
  • 1,278
  • 5
  • 18
  • 29
  • You *could* create a 1x1 transparent image resource (i.e. a 1pixel image that is fully transparent) and then assign this resource to your ImageButton. I'm surprised setImageResource(null) didn't work... – CSmith Aug 06 '12 at 20:18
  • Did you try ImageButton.setVisibility(View.INVISIBLE)?? – luanjot Aug 06 '12 at 20:19
  • Have you tried setImageDrawable(null) or setImageBitmap(null)? – mportuesisf Aug 06 '12 at 20:20

4 Answers4

37

Try setting imageButton.setImageResource(android.R.color.transparent).

P Varga
  • 19,174
  • 12
  • 70
  • 108
  • Wouldn't that give an error? The system would treat the Integer as an pointer to an image resource, and that resource doesn't exist, or at least isn't an image. Have you tried this? – RandomSort Aug 06 '12 at 20:27
  • 1
    @RandomSort it works because `setImageResource` accepts any kind of resource, not only an image. – P Varga Aug 06 '12 at 21:59
  • 1
    This works for ImageView as well. @Peter V – Gpak Jan 30 '15 at 05:13
15
imageButton.setBackgroundResource(0);

Docs says:

The resource should refer to a Drawable object or 0 to remove the background

TheModularMind
  • 2,024
  • 2
  • 22
  • 36
1

Normally, people create null_image.xml resource file in drawable folder and use this resource whenever they need to clear background:

null_image.xml content:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="#0000" />
    <size
        android:width="1dp"
        android:height="1dp" />
</shape>

And when you need to clear background - just call:

ImageButton.setImageDrawable(R.drawable.null_image);

Don't know why CSimth did not post his comment as an answer... It was correct as for me

Pavel Dudka
  • 20,754
  • 7
  • 70
  • 83
  • I'm going to try this out, but it can't be the **right** way to do it. Either remove the button from the layout or hide it. Setting it to a dummy image resource just doesn't feel right, though it might be the easiet solution – RandomSort Aug 06 '12 at 20:30
0

I'm not sure if it works, but I think you could recycle the ImageButton's drawable.

Try something like:

ImageButton imbBtn = (ImageButton) findBiewById(R.id.imbBtn);

imgBtn.getDrawable().recycle();

I've never tried this before, let me know if it works to you.

yugidroid
  • 6,640
  • 2
  • 30
  • 45