Remove the image from a imageview Android

JavaAndroidBitmapImageview

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

I always use

imageView.setImageDrawable(null);

Solution 2 - Java

Try:

imageView.setImageResource(0);

This will set the image view to use no resource.

Solution 3 - Java

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.

Solution 4 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChyByView Question on Stackoverflow
Solution 1 - Javatristan2468View Answer on Stackoverflow
Solution 2 - JavaRiboseView Answer on Stackoverflow
Solution 3 - JavaVariagView Answer on Stackoverflow
Solution 4 - JavajamesView Answer on Stackoverflow