308

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.

braX
  • 11,506
  • 5
  • 20
  • 33
Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • 1
    i suggest to load the default image when you have no value. – Praveen May 18 '10 at 16:53
  • 3
    I have no default image, just a background. I need to have just the background. – Pentium10 May 18 '10 at 16:54
  • I think accepted answer needs to be changed here. Is there any provision on stackoverflow to change/move accepted answer to actual answer(or more better answer) from a 'workaround' answer? Because lot of users(like me) can go for accepted rather than better answer – Shirish Herwade Dec 09 '15 at 17:56
  • 1
    @ShirishHerwade ok changed – Pentium10 Dec 10 '15 at 07:19
  • I was facing the same issue i do it like this: https://stackoverflow.com/a/50448536/5697474 – Syed Danish Haider May 21 '18 at 12:09
  • I would just create a default image resource and under an if-statement, where the image is suppose to be set in the first place, I would just write the following under the else-clause: imageView.setImageResource(R.drawable.default_resource.jpg); – goldenmaza Jan 16 '19 at 13:48

18 Answers18

607

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.

Fran Marzoa
  • 4,293
  • 1
  • 37
  • 53
Mario Lenci
  • 10,422
  • 5
  • 39
  • 50
  • 3
    I think using setImageResource with a color identifier will give you crashing issues on Android 2.2.1 – Blundell Aug 15 '12 at 08:13
  • that is possible. I don't have a device to test it. did you test it? in case write here and I'll edit the answer ( and my code :) ). I tested it on an xperia U20i with a 2.1-update1 and works as expected. – Mario Lenci Sep 19 '12 at 17:39
  • 2
    for me works the viewToUse.setImageResource(android.R.color.transparent); code – Javier Jan 09 '13 at 17:20
  • This won't work if you've set a drawable instead of a uri or a resource – pablisco Apr 23 '14 at 13:50
  • are you talking about the first solution? That's possible, but i'm sure the second solution works in every case – Mario Lenci Apr 23 '14 at 15:48
  • 3
    beware of this, if you actually have a resource with an id of 0 this will infact set that drawable as yourmage view. Not sure what the chances are of having that as an id though.. – Chris Sep 12 '14 at 12:14
  • 1
    I had problems with the setImageResource(0)--didn't always work. But setting it to transparent is very reliable. Thanks! – SMBiggs Sep 18 '14 at 16:43
  • 6
    `viewToUse.setImageResource(0);` does not worked,but the `viewToUse.setImageResource(android.R.color.transparent);` worked. – zionpi May 04 '15 at 07:57
  • If you are trying to clear a background images instead of src then use viewToUse.setBackgroundResource(android.R.color.transparent); – SingleWave Games Oct 08 '15 at 14:03
  • Yeah dont use the 0 trick - some times Android looks for a resource with id 0 and then it blows up. Oddly this is random behaviour - so hard to spot. – slott Oct 27 '15 at 12:54
  • setting a transparent background might cause to overdraw problem. even transparent pixels are being rendered – Nativ Sep 11 '16 at 09:16
  • Didn't work for me inside a `ListView` Try this `image.setImageBitmap(null);` – Zain Dec 27 '16 at 21:39
  • 1
    About `viewToUse.setImageResource(0)` , this should work only if you use the support library (which automatically gives you `AppCompatImageView` instead of `ImageView`), or if you are on Android 5.1 (API 22) and above. I wrote about it here: https://stackoverflow.com/a/50100342/878126 – android developer Apr 30 '18 at 13:32
  • viewToUse.setImageResource(0) does not work on Kitkat but works on higher android versions – Suraj Aug 27 '19 at 07:42
146

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

LionHere
  • 9
  • 1
  • 2
Kon
  • 27,113
  • 11
  • 60
  • 86
  • 3
    This works reliably at least in my app. setImageResource(0) works sometimes, but not others. – Hong Apr 03 '13 at 12:50
  • 5
    If you've set the image using setImageBitmap this is what you want – Kevin Parker May 15 '13 at 17:43
  • This is in my opinion the best answer! It's clean, valid and very short. No need to define a blank drawable and apply it or stuff like that. – Potaito Mar 10 '15 at 18:15
  • This answer actually works reliably! And it is the earliest post stating this code as a solution. Hence I upvoted this one and not other posts with the same answer. – The Original Android Jul 05 '15 at 19:36
  • This did not work for me on Android M. I had to set View to be Invisible. – likejudo Dec 16 '15 at 18:59
  • 4
    This answer is actually documented on the official docs: https://developer.android.com/reference/android/widget/ImageView.html#setImageDrawable(android.graphics.drawable.Drawable) –  Feb 01 '18 at 20:05
78

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.

Peter Ajtai
  • 56,972
  • 13
  • 121
  • 140
dennis.sheppard
  • 2,565
  • 3
  • 19
  • 16
  • 2
    Actually, setting it to ANY invalid image resource works (-1, 45, etc) – dvallejo Mar 01 '11 at 18:01
  • 8
    No, setting to -1 gives you android.content.res.Resources$NotFoundException. 0 seems to work. – NateS Apr 10 '11 at 07:10
  • 28
    This case, when you scrolling your listview. somehow the imageview that set it as 0 will show the wrong image (it will show image from another imageview) so the resolution below can do it viewToUse.setImageResource(android.R.color.transparent); – Sruit A.Suk Jul 15 '12 at 13:31
  • 3
    I got this error once, when trying this method: "android.content.res.Resources$NotFoundException: Resource ID #0x0", so it seems that setting an empty image is the best way to "clear" the ImageView – Pavel Alexeev Apr 12 '13 at 15:47
15

I tried this for to clear Image and DrawableCache in ImageView

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

I hope this works for you !

Nandha
  • 253
  • 2
  • 10
15

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 ...
}
Dan Lew
  • 85,990
  • 32
  • 182
  • 176
  • 68
    No, I don't want this. I want just to clear the contents. The user may select new image after. – Pentium10 May 18 '10 at 17:08
  • I don't understand then. You don't want any image to be shown, but you don't have a default image to show to the user when no image is shown. There's either something you *do* want to show the user or not. You could set the background to be the image resource even. – Dan Lew May 18 '10 at 17:11
  • If you are familiar with Contacts there is a contact card background frame replacer, I have that in my code. `` This is a simple gray background into which comes the photo. – Pentium10 May 18 '10 at 17:22
  • 3
    The Contacts app is open source (http://android.git.kernel.org/?p=platform/packages/apps/Contacts.git;a=tree). I investigated the source code. It turns out that they are using a QuickContactBadge (a subclass of ImageView), and when they don't have a photo, they use setImageResource to set a default image. Check com.android.contacts.ContactsListActivity#ContactItemListAdapter for the details, especially in getView(). – Dan Lew May 18 '10 at 17:37
  • 9
    Why did you checked that answer as the right one if it is not what you want? – arniotaki Nov 14 '14 at 08:13
  • 3
    Daniel There may be many reasons as to why people want the image clear. – basickarl Mar 12 '15 at 11:13
  • 1
    He does not want a default image. He wants it to be cleared. – EpicPandaForce Jul 27 '16 at 08:22
12

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.

Brad English
  • 109
  • 1
  • 9
11

if you use glide you can do it like this.

Glide.with(yourImageView).clear(yourImageView)
Mojtaba Haddadi
  • 1,346
  • 16
  • 26
6

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

Ajay
  • 1,796
  • 3
  • 18
  • 22
  • 1
    You are right Ajay,I observed the same. I used setImageBitmap()/setImageResource() alternatively for same imageview depending on the conditions. Hence I had to use setImageResource(0)/setImageBitmap(null) alternativley to clear the imageview. Thanks! – Monica M Mar 20 '14 at 11:16
3

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!

duggu
  • 37,851
  • 12
  • 116
  • 113
kwasi
  • 67
  • 4
3

This works for me.

emoji.setBackground(null);

This crashes in runtime.

viewToUse.setImageResource(android.R.color.transparent);  
karel
  • 5,489
  • 46
  • 45
  • 50
sbchitra
  • 51
  • 3
2

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

Arvis
  • 8,273
  • 5
  • 33
  • 46
2

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);
Community
  • 1
  • 1
KitKat
  • 1,495
  • 14
  • 15
1

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

android developer
  • 114,585
  • 152
  • 739
  • 1,270
0

To remove the image in ImageView use:

title_image.setImageResource(-1);

it works for me

Tobore Igbe
  • 61
  • 1
  • 4
0

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);
Syed Danish Haider
  • 1,334
  • 11
  • 15
0

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);
TornaxO7
  • 1,150
  • 9
  • 24
-1

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));
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Aaron Newton
  • 49
  • 1
  • 2
-2

editText.setText("");

imageView.setImageResource(0);