[android] How to clear an ImageView in Android?

I am reusing ImageViews for my displays, but at some point I don't have values to put it.

So how to clear an ImageView in Android?

I've tried:

mPhotoView.invalidate();
mPhotoView.setImageBitmap(null);

None of them have cleared the view, it still shows previous image.

The answer is


As kwasi wrote and golu edited, you can use transparent, instead of white:

File drawable/transparent.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@android:color/transparent"/>
</shape>

Inside an activity, view, etc:

view.setImageResource(R.drawable.transparent);

If you use the support library, you get AppCompatImageView instead of ImageView , which supports setImageResource(0) on all devices, so you should be fine with using it the same way as using setImageDrawable(null) and setImageBitmap(null)

Otherwise, it should work fine starting from some Android version. According to the code of Android and according to my tests, I think it should be safe to use setImageResource(0) from Android API 22 (5.1). You can see the Android code of API 22 vs API 21, here


I was able to achieve this by defining a drawable (something like blank_white_shape.xml):

<shape xmlns:android="http://schemas.android.com/apk/res/android"       
       android:shape="rectangle">
    <solid android:color="@android:color/white"/>
</shape>

Then when I want to clear the image view I just call

 imageView.setImage(R.drawable.blank_white_shape);

This works beautifully for me!


I tried to remove the Image of the ImageView too by using ImageView.setImageRessource(0) but it didn't work for me.
Luckily I got this message in the logs:

Caused by: java.lang.IllegalStateException: The specified child already has a parent. 
You must call removeView() on the child's parent first.

So let's say, layoutmanager is your instance for the layout, than you need to do that:

RelativeLayout layoutManager = new RelativeLayout(this);
ImageView image = new ImageView(this);

// this line worked for me
layoutManager.removeView(image);

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


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.


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 ...
}

Hey i know i am extremely late to this answer, but just thought i must share this,

The method to call when u reset the image must be the same method that u called while u were setting it.

When u want to reset the image source @dennis.sheepard answer works fine only if u are originally setting the image in the bitmap using setImageResource()

for instance,

i had used setImageBitmap() and hence setting setImageResource(0) didn work, instead i used setImageBitmap(null).


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.)


Can i just point out what you are all trying to set and int where its expecting a drawable.

should you not be doing the following?

imageview.setImageDrawable(this.getResources().getDrawable(R.drawable.icon_image));

imageview.setImageDrawable(getApplicationContext().getResources().getDrawable(R.drawable.icon_profile_image));

This works for me.

emoji.setBackground(null);

This crashes in runtime.

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

if you use glide you can do it like this.

Glide.with(yourImageView).clear(yourImageView)

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.


To remove the image in ImageView use:

title_image.setImageResource(-1);

it works for me


I tried this for to clear Image and DrawableCache in ImageView

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

I hope this works for you !


I was facing same issue i changed background color of view to layout background color u can do like this:

 edit_countflag.setBackgroundColor(Color.parseColor("#ffffff"));

//then set the image

edit_countflag.setImageResource(R.drawable.flag_id);

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-imageview

How to set a ripple effect on textview or imageview on Android? Placing a textview on top of imageview in android How to create a circular ImageView in Android? How to download and save an image in Android Android ImageView Fixing Image Size "Bitmap too large to be uploaded into a texture" Android Camera : data intent returns null 'Missing contentDescription attribute on image' in XML Android ImageView Zoom-in and Zoom-Out findViewById in Fragment

Examples related to android-resources

Failed linking file resources Execution failed for task ':app:processDebugResources' even with latest build tools getResources().getColor() is deprecated getColor(int id) deprecated on Android 6.0 Marshmallow (API 23) Android getResources().getDrawable() deprecated API 22 Android Studio cannot resolve R in imported project? Android XXHDPI resources Android ImageView setImageResource in code format statement in a string resource file Lint: How to ignore "<key> is not translated in <language>" errors?

Examples related to android-image

Saving and Reading Bitmaps/Images from Internal memory in Android How to create a circular ImageView in Android? "Bitmap too large to be uploaded into a texture" Reading an image file into bitmap from sdcard, why am I getting a NullPointerException? Open an image using URI in Android's default gallery image viewer Android Text over image How to get Bitmap from an Uri? How do I get the resource id of an image if I know its name? How to clear an ImageView in Android? How to pick an image from gallery (SD Card) for my app?