Loading Images with Coil
Load and cache network images efficiently with Coil. Apply transformations (CircleCrop, RoundedCorners), handle placeholders and errors.
Loading Images with Coil is a free Android Academy lesson on CoddyKit — lesson 2 of 6. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 6 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why an Image Library?
Loading images from the internet is surprisingly complex:
- Download on a background thread
- Cache to disk and memory
- Decode efficiently (avoid OOM crashes)
- Show a placeholder while loading
- Handle errors (broken link, no network)
Coil handles all of this in one line of code.
What Is Coil?
Coil (Coroutine Image Loader) is a modern Android image loading library built for Kotlin and Coroutines.
- Lightweight — smaller than Glide or Picasso
- Kotlin-first with coroutine support
- Lifecycle-aware — cancels loads automatically
- Supports GIF, SVG, video frames
Adding Coil
Add the dependency in app/build.gradle:
// app/build.gradle
dependencies {
implementation 'io.coil-kt:coil:2.6.0'
}
// Also make sure internet permission is in AndroidManifest.xml:
// <uses-permission android:name="android.permission.INTERNET" />Basic Image Loading
Load an image into an ImageView with one line:
import coil.load
// In your Activity or Adapter:
binding.ivAvatar.load("https://picsum.photos/200")
// With a placeholder and error image:
binding.ivAvatar.load("https://picsum.photos/200") {
placeholder(R.drawable.ic_placeholder)
error(R.drawable.ic_error)
crossfade(true) // smooth fade-in animation
}Loading in a RecyclerView
Coil cancels pending loads automatically when a ViewHolder is recycled — preventing wrong images from appearing in the wrong rows:
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val post = getItem(position)
holder.tvTitle.text = post.title
holder.ivCover.load(post.imageUrl) {
placeholder(R.drawable.ic_image_placeholder)
error(R.drawable.ic_broken_image)
crossfade(300) // 300ms fade-in
size(200, 200) // resize to 200×200 pixels
}
}Transformations
Apply transformations to loaded images:
CircleCropTransformation()— round image (perfect for avatars)RoundedCornersTransformation(radius)— rounded cornersBlurTransformation(context, radius)— blur effect
Transformation Example
Display a circular user avatar:
import coil.load
import coil.transform.CircleCropTransformation
import coil.transform.RoundedCornersTransformation
// Circular avatar
binding.ivAvatar.load(user.profileImageUrl) {
transformations(CircleCropTransformation())
placeholder(R.drawable.ic_default_avatar)
error(R.drawable.ic_default_avatar)
}
// Rounded corners on a card image
binding.ivCover.load(post.coverUrl) {
transformations(RoundedCornersTransformation(16f))
crossfade(true)
}Caching Behavior
Coil caches images automatically at two levels:
- Memory cache — fastest, cleared when the app is killed
- Disk cache — persists between app launches, default 250 MB
If the same URL is loaded twice, the second load comes from cache — no network request.
Loading from Different Sources
Coil can load images from various sources, not just URLs:
// From a URL (most common)
imageView.load("https://example.com/image.jpg")
// From a URI (e.g., gallery image)
imageView.load(uri)
// From a drawable resource
imageView.load(R.drawable.my_image)
// From a File
imageView.load(File("/sdcard/photo.jpg"))
// From a bitmap
imageView.load(bitmap)Quick Check
Which Coil transformation would you use to display a user profile photo as a circle?
Recap: Loading Images with Coil
You can now display network images efficiently:
- Add Coil with one Gradle dependency
imageView.load(url) { }— load with one line- Placeholder and error fallback images
- Transformations: CircleCrop, RoundedCorners, Blur
- Automatic caching at memory and disk level
- Works with URLs, URIs, Files, drawables, and bitmaps
Next: handle errors gracefully and give users great feedback.
Frequently asked questions
Is the “Loading Images with Coil” lesson free?
Yes — the full text of “Loading Images with Coil” is free to read here on the web, and the Android Academy course includes 6 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.
What will I learn in “Loading Images with Coil”?
Load and cache network images efficiently with Coil. Apply transformations (CircleCrop, RoundedCorners), handle placeholders and errors. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Android Academy?
No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 6, so you can start here or from the beginning and move at your own pace.
How long does the “Loading Images with Coil” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Android Academy lesson?
Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.