1

I have this xml

<ImageView
    android:id="@+id/ImgOn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@drawable/icon_on" />

I want to remove this image sometimes via code. I've seen many posts like this, but in my case imageButton.setBackgroundResource(0); leaves the default image which appears in my xml layout.

How would you remove it programmatically? I heard transparent color crashes some old devices.

update

setImageResource(0) left the xml's original src as well and not null as i want it to be

Community
  • 1
  • 1
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157

4 Answers4

3

Try this..

Your setting setBackgroundResource that's for android:background="@drawable/icon_on" not for android:src="@drawable/icon_on" background and src both are different use

imageButton.setImageResource(0);

EDIT

imageButton.setImageDrawable(null);
Hariharan
  • 24,741
  • 6
  • 50
  • 54
2

you can use imageButton.setImageResource(0)

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

The background (backgroundresource) and foreground (src) image are two different things, thus the one does not influence the other.

Remove the foreground image using:

imageButton.setImageDrawable(null);
FD_
  • 12,947
  • 4
  • 35
  • 62
0

if u want to null imageview then u have to call imageButton.setImageResource(0); BackgroundResource only null that background of image dude

Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49