Reputation: 207828
I am reusing ImageView
s 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.
Upvotes: 311
Views: 272550
Reputation: 1
I tried and they all work (one in 3 cases):
imageView.setImageResource(0);
imageView.setImageResource(android.R.color.transparent);
imageView.setImageBitmap(null);
Or you can hide it:
imageView.setVisibility(View.GONE);
Upvotes: 0
Reputation: 1299
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);
Upvotes: 0
Reputation: 1376
if you use glide you can do it like this.
Glide.with(yourImageView).clear(yourImageView)
Upvotes: 12
Reputation: 10542
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.
Upvotes: 613
Reputation: 41
This works for me.
emoji.setBackground(null);
This crashes in runtime.
viewToUse.setImageResource(android.R.color.transparent);
Upvotes: 3
Reputation: 27431
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.)
Upvotes: 147
Reputation: 1384
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);
Upvotes: 0
Reputation: 116322
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
Upvotes: 1
Reputation: 109
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.
Upvotes: 12
Reputation: 61
To remove the image in ImageView use:
title_image.setImageResource(-1);
it works for me
Upvotes: 0
Reputation: 1515
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);
Upvotes: 2
Reputation: 253
I tried this for to clear Image and DrawableCache in ImageView
ImgView.setImageBitmap(null);
ImgView.destroyDrawingCache();
I hope this works for you !
Upvotes: 15
Reputation: 1826
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)
.
Upvotes: 6
Reputation: 67
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!
Upvotes: 3
Reputation: 8363
For ListView item image you can set ImageView.setVisibility(View.INVISIBLE)
or ImageView.setImageBitmap(null)
in list adapter for "no image" case.
Upvotes: 2
Reputation: 49
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));
Upvotes: -1
Reputation: 2595
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.
Upvotes: 78
Reputation: 87420
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 ...
}
Upvotes: 15