138

I'm trying to make an ImageView that holds a gallery of images. By touching the user request to load the next image. If the next image isn't found in the server or takes time to load I need the old image to be empty.

setVisibility(View.GONE) or setVisibility(View.INVISIBLE) don't work for me because when invisible/gone I stop the onTouch() detecting (and the user is locked to current image).

How can I make the ImageView to load a empty bitmap or clear (remove) current bitmap?

HitOdessit
  • 7,198
  • 4
  • 36
  • 59
ChyBy
  • 1,655
  • 2
  • 13
  • 15

4 Answers4

456

I always use

imageView.setImageDrawable(null);
Nova Entropy
  • 5,727
  • 1
  • 19
  • 32
  • 1
    strange! its not working. In fact setImageResouce(0) and setBackground(null) is also not working. Any idea what could be the reason or any trick to clear the cache or something. – RamKr Feb 23 '15 at 13:57
  • Are you calling this from the main (UI) Thread? – Nova Entropy Feb 23 '15 at 14:38
  • 1
    If an image is not loaded into imageView and still one tries to clear it, the program stops unexpectedly. Is there a way to tackle this ? – Krithi07 May 29 '15 at 10:11
  • @Krithi07 Check for image resource before clearing it. – Ahmad Reza Enshaee Jan 30 '18 at 20:41
  • @RamKr , what are you writing while you are setting ? If you use imageview.setBackgroundResource() and you should use image.setBackgroundResource(0), If you use imageView.setImageDrawable() than you should use imageView.setImageDrawable(null). – Arda Kaplan Jun 19 '18 at 13:21
16

Try:

imageView.setImageResource(0);

This will set the image view to use no resource.

Ribose
  • 2,223
  • 16
  • 22
15

From what I've noticed, the "working" or not of certain method when clearing image depends on the method used to populate ImageView.

So if you set img.setImageBitmap(bmp) then to clear you should use img.setImageBitmap(null). When you img.setImageResource(resId) then to clear you should use img.setImageResouce(0). Etc.

Variag
  • 1,056
  • 10
  • 25
6

Certainly imageView.setImageResource(0) works. It has never failed for me and I have used it many times.

setImageResource is usually passed the reference R.drawable,(the reference for the picture), which is stored as an int, but displayed in the R.java class as a hex value, 0xf2fs... So assuming this reference exist it will show a picture, if you later pass that same imageview a reference which does not exist the old picture will no longer show. So, if you pass it 0, or 5 or an int which does not match a resource referenced in your R.java class it will remove the picture completely from the src of the imageView. So if you are passing 0 to the old reference of the imageView.

james
  • 71
  • 1
  • 2
  • 2
    `imageView.setImageResource(0)` didn't work for me on a Samsung S5 running 4.4.2. However `imageView.setImageDrawable(null);` works. – rlay3 May 13 '15 at 16:02
  • @rlay3 If you use the support library, you get `AppCompatImageView` instead of `ImageView` , which supports `setImageResource(0)` on all devices. – android developer Apr 30 '18 at 11:27