Jetpack Compose Tips: Load an image from URL using Coil — fast and easy

Step 1. Implement dependency

repositories {
mavenCentral()
}

2. Implement the dependency on build.gradle (app level) . Check the latest version in the repository

dependencies {
implementation "dev.chrisbanes.accompanist:accompanist-coil:<version>"
}

Step 2. Add internet permission

In your app’s AndroidManifest.xml add the internet permission inside the <manifest> tag (preferably, place it above the <application> tag):

<uses-permission android:name="android.permission.INTERNET"/>

Step 3. Put it where you need it

  • Simple Declaration
CoilImage(
data = "https://i.pinimg.com/originals/5a/dd/33/5add3332302c9db5e9a6aeedfeb6b29b.jpg",
contentDescription = "Random adorable cat"
)
  • Fill parent with custom size
Row(
modifier = Modifier
.fillMaxWidth()
.height(300.dp)
.padding(bottom = 15.dp)
) {
CoilImage(
data = "https://i.pinimg.com/564x/a0/6d/27/a06d2778c07cb6b81ec7e9a77933db49.jpg",
contentDescription = "Random cute yorkshire",
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Crop,
}

Read more about ContentScale

More of Coil functionalities:

Why use Coil?

Fast: Coil performs a number of optimizations including memory and disk caching, downsampling the image in memory, re-using bitmaps, automatically pausing/cancelling requests, and more.

Lightweight: Coil adds ~2000 methods to your APK (for apps that already use OkHttp and Coroutines), which is comparable to Picasso and significantly less than Glide and Fresco.

Easy to use: Coil’s API leverages Kotlin’s language features for simplicity and minimal boilerplate.

Modern: Coil is Kotlin-first and uses modern libraries including Coroutines, OkHttp, Okio, and AndroidX Lifecycles.

Coil is an acronym for: Coroutine Image Loader.

Read more : Coil

I personally use it in my project — https://github.com/mjmanaog/adopt-a-pup

Hello, I’m MJ (just a noobie), still learning about Jetpack Compose. I will try to share anything useful that I’ve learned during this process. Leave a clap/comment/suggestions to improve my skills! Thank you :)

Android Developer | Newbie