How to clear an ImageView in Android?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
You can also use related methods depending on how the image was assigned:
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.
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:
With Picasso, cancel the request and optionally assign a placeholder or null drawable:
With Coil, clear the request using the image loader or assign null through the extension API:
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.
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.
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
RecyclerViewitems without resetting the old image first. - Holding onto view references after
onDestroyViewin 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.
Related reading
- How to clear navigation Stack after navigating to another fragment in Android
- How to clear or clean specific pod from the local cocoapods cache
- How to clear react-native cache?
- How to click or tap on a TextView text
- How to compare Swift enum with associated values by ignoring its associated value?
- How to compare the performance of Android Apps written in Java and Xamarin C#? Anyway to check quantitative data (code & results)
- How to compare the performance of Android Apps written in Java and Xamarin C? Anyway to check quantitative data code results
- How to compare two strings ignoring case in Swift language?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.