Delete ImageButton Image Resource

I have a transparent ImageButton. When you click the ImageResource button on this button, the ability to draw in my drawable-hdpi folder is established (basically the image is displayed on top of the transparent ImageButton). I use the method for this ImageButton.setImageResource(). My question is how can I remove the image resource so that the transparent image button reappears. Of course, I need to be able to do this in Java, not XML. I tried the following, which did not work: ImageButton.setImageResource(null);I also looked around a bit and could not find the answer ... Thanks in advance for any help.

EDIT: Thank you all for your answers. Péter Varga's answer did exactly what I needed to get what I was going to.

+5
source share
4 answers

Try to install imageButton.setImageResource(android.R.color.transparent).

+27
source
imageButton.setBackgroundResource(0);

The docs say:

The resource must reference a Drawable or 0 to remove the background.

+13
source

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

null_image.xml:

<?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 the background - just call:

ImageButton.setImageDrawable(R.drawable.null_image);

I don’t know why CSimth did not send a comment as an answer ... It was right, as for me

0
source

I'm not sure if this works, but I think you could recycle ImageButtondrawable.

Try something like:

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

imgBtn.getDrawable().recycle();

I have never tried this before, let me know if this works for you.

0
source

All Articles