Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove image resource of ImageButton [duplicate]

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.

like image 448
corecase Avatar asked Aug 06 '12 20:08

corecase


People also ask

How do I remove background from ImageButton?

How do I remove background from ImageButton? You can use android:background="@null" for your ImageButton. Save this answer.

What is SRC in ImageView?

<ImageView android:id="@+id/simpleImageView" android:layout_width="fill_parent" android:layout_height="wrap_content" /> 2. src: src is an attribute used to set a source file or you can say image in your imageview to make your layout attractive.


2 Answers

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

like image 103
Pavel Dudka Avatar answered Oct 20 '22 11:10

Pavel Dudka


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

like image 42
ᅙᄉᅙ Avatar answered Oct 20 '22 09:10

ᅙᄉᅙ