Android
ImageView
Clear ImageView
Android Development
Mobile App Development

How to clear an ImageView in Android?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Clearing an ImageView in Android usually means removing its current drawable and, if an image-loading library is involved, cancelling any pending request tied to that view. The exact call depends on how the image was set. If you only assigned a drawable manually, a simple null assignment is enough. If Glide, Picasso, or Coil loaded the image, use the library’s clear API as well.

Remove the Current Drawable

For images set directly on the view, clearing is straightforward. You can set the drawable, bitmap, or resource back to an empty state.

kotlin
val imageView = findViewById<ImageView>(R.id.preview)

imageView.setImageDrawable(null)

You can also use related methods depending on how the image was assigned:

kotlin
imageView.setImageBitmap(null)
imageView.setImageResource(0)

All three approaches remove the visible image. setImageDrawable(null) is usually the clearest because it directly expresses that the view should hold no drawable.

Clear Before Reusing a View

A common case is a recycled view such as a RecyclerView item. If a holder is reused while an old image is still attached, users may briefly see the wrong content.

kotlin
1class PhotoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
2    private val imageView: ImageView = itemView.findViewById(R.id.photo)
3
4    fun bind(photoUrl: String?) {
5        imageView.setImageDrawable(null)
6
7        if (photoUrl != null) {
8            // Start a new image request here.
9        }
10    }
11}

Clearing at the start of bind prevents stale images from flashing while the next request is loading.

Clear Requests from Image Loading Libraries

If Glide or another loader manages the image, the drawable on screen is only part of the story. There may also be an in-flight request, a memory-cache entry, or a target callback still associated with the view.

With Glide, clear the view through the library:

kotlin
Glide.with(imageView).clear(imageView)
imageView.setImageDrawable(null)

With Picasso, cancel the request and optionally assign a placeholder or null drawable:

kotlin
Picasso.get().cancelRequest(imageView)
imageView.setImageDrawable(null)

With Coil, clear the request using the image loader or assign null through the extension API:

kotlin
imageView.clear()
imageView.setImageDrawable(null)

The important point is that clearing the ImageView and cancelling the request are related but different operations.

Clear at the Right Lifecycle Moment

If a fragment or activity is going away, it is good practice to release heavy view references and cancel image work attached to views that are about to disappear.

kotlin
1override fun onStop() {
2    super.onStop()
3    Glide.with(this).clear(findViewById(R.id.preview))
4}

For fragments, also be careful around onDestroyView. The fragment instance may survive while the view tree is destroyed. That makes view cleanup especially important.

Showing an Empty State vs Removing the Image

Sometimes “clear the image” is not the real requirement. The real requirement is “show that no image is available.” In that case, a placeholder is better than a true empty state.

kotlin
imageView.setImageResource(R.drawable.placeholder_avatar)

Use a placeholder when the layout should stay visually stable or when users need feedback that loading failed or no image exists.

Common Pitfalls

  • Clearing the drawable but forgetting to cancel an image-loading request.
  • Reusing RecyclerView items without resetting the old image first.
  • Holding onto view references after onDestroyView in a fragment.
  • Using setImageResource(0) without confirming that the view should really be blank instead of showing a placeholder.
  • Assuming clearing the view also frees every bitmap immediately. Memory release still depends on remaining references and library caches.

Summary

  • Use setImageDrawable(null) for the simplest manual clear operation.
  • In recycled views, clear old content before starting a new image load.
  • If Glide, Picasso, or Coil loaded the image, cancel or clear the library request too.
  • Clean up image views at the correct lifecycle boundary, especially in fragments.
  • Choose between a true empty state and a placeholder based on the UI requirement.

Course illustration
Course illustration

All Rights Reserved.