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
  • 11,551
  • 28
  • 75
  • 141

4 Answers4

2

you can use imageButton.setImageResource(0)

Blackbelt
  • 152,872
  • 27
  • 286
  • 297
2

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
  • 28,756
  • 7
  • 51
  • 55
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,888
  • 4
  • 33
  • 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,076
  • 2
  • 22
  • 48