What is Coil?
Coil (Coroutine Image Loader) is a lightweight and Kotlin-first image-loading library. It’s designed to be fast, efficient, and easy to use, with built-in support for Kotlin Coroutines and Jetpack Compose.
Why use Coil?
- Kotlin-first & Coroutine-based: Simplifies async image loading.
- Lightweight & fast: Minimal memory footprint.
- Jetpack Compose support: Native AsyncImage composable.
- Automatic caching: Memory and disk caching out of the box.
- Easy transformations: Crop, blur, circle, etc.
Setup
Add Coil dependencies in your build.gradle.
latest_version = choose according to your requirements.
implementation("io.coil-kt:coil:latest_version")
implementation("io.coil-kt:coil-compose: latest_version ") // for Compose
Examples
With ImageView (Classic View)
imageView.load("https://example.com/image.jpg") {
placeholder(R.drawable.placeholder)
error(R.drawable.error)
crossfade(true)
}
With Jetpack Compose
Change imageUrl, size, color, and border according to your requirements.
AsyncImage(
model = ImageRequest.Builder(context)
.data(imageUrl).crossfade(true)
.build(),
error = painterResource(id = R.drawable.placeholder_user),
placeholder = painterResource(id = R.drawable.placeholder_user),
contentDescription = "imageUrl",
contentScale = ContentScale.Crop,
modifier = Modifier
.clip(CircleShape)
.background(color = imageBGColor)
.border(2.dp, strokeColor, CircleShape)
.padding(2.dp)
.size(width = imageWidth, height = imageHeight)
.clip(CircleShape)
)
Summary
Coil is a modern, efficient image loader that fits perfectly in Kotlin, making image loading simple, fast, and smooth.