Android Academy · Lesson

Loading Images with Coil

Load and cache network images efficiently with Coil. Apply transformations (CircleCrop, RoundedCorners), handle placeholders and errors.

Lesson 2 of 611 steps

Loading Images with Coil is a free Android Academy lesson on CoddyKit. This is lesson 2 of 6. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 6 lessons in total.

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 corners
  • BlurTransformation(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.

Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
36
Lessons
152

Frequently Asked Questions

Is the “Loading Images with Coil” lesson free?

Yes — any 3 lessons of the Android Academy learning path, including “Loading Images with Coil”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 6 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 6.

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.

All lessons in this course

  1. Retrofit & REST APIs
  2. Loading Images with Coil
  3. Error Handling & UX
  4. Push Notifications
  5. WorkManager & Background Tasks
  6. Publishing to Play Store
← Back to Android Academy