Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear an ImageView in Android?

People also ask

How to make ImageView empty in android?

For ListView item image you can set ImageView. setVisibility(View. INVISIBLE) or ImageView. setImageBitmap(null) in list adapter for "no image" case.

What is an ImageView?

ImageView class is used to display any kind of image resource in the android application either it can be android. graphics. Bitmap or android.

What is ImageView in Android Studio?

Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling.


I used to do it with the dennis.sheppard solution:

viewToUse.setImageResource(0);

it works but it is not documented so it isn't really clear if it effects something else in the view (you can check the ImageView code if you like, i didn't).

I think the best solution is:

viewToUse.setImageResource(android.R.color.transparent);

I like this solution the most cause there isn't anything tricky in reverting the state and it's also clear what it is doing.


I had a similar problem, where I needed to basically remove ImageViews from the screen completely. Some of the answers here led me in the right direction, but ultimately calling setImageDrawable() worked for me:

imgView.setImageDrawable(null);

(As mentioned in the comment, such usage is documented in the official docs: ImageView#setImageDrawable.)


I know this is old, but I was able to accomplish this with

viewToUse.setImageResource(0);

Doesn't seem like it should work, but I tried it on a whim, and it worked perfectly.


It sounds like what you want is a default image to set your ImageView to when it's not displaying a different image. This is how the Contacts application does it:

if (photoId == 0) {
    viewToUse.setImageResource(R.drawable.ic_contact_list_picture);
} else {
    // ... here is where they set an actual image ...
}

I tried this for to clear Image and DrawableCache in ImageView

ImgView.setImageBitmap(null);
ImgView.destroyDrawingCache();

I hope this works for you !


If none of these solutions are clearing an image you've already set (especially setImageResource(0) or setImageResources(android.R.color.transparent), check to make sure the current image isn't set as background using setBackgroundDrawable(...) or something similar.

Your code will just set the image resource in the foreground to something transparent in front of that background image you've set and will still show.