tags:

views:

549

answers:

3

I am resusing 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.

+1  A: 

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 ...
}
Daniel Lew
No, I don't want this. I want just to clear the contents. The user may select new image after.
Pentium10
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.
Daniel Lew
If you are familiar with Contacts there is a contact card background frame replacer, I have that in my code. `<ImageView android:id="@+id/photo" android:layout_width="54dip" android:layout_height="57dip" android:layout_marginLeft="0dip" android:background="@drawable/quickcontact_photo_frame" />` This is a simple gray background into which comes the photo.
Pentium10
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().
Daniel Lew
A: 

Try mPhotoView.setAlpha(0) to make the image completely transparent (restore with 255).

molnarm