Showing a Progress bar while loading image using Coil?
Asked Answered
H

5

11

How to show a progress bar while fetching image from URL in Coil.?

Handclasp answered 26/8, 2021 at 12:16 Comment(0)
L
4

Use ImageRequest.Listener

Example:

val imageRequest = ImageRequest.Builder(context)
    .data(url)
    .listener(
        onStart = {
            // set your progressbar visible here
        },
        onSuccess = { request, metadata ->
            // set your progressbar invisible here
        }
    )
    .build()

imageLoader.enqueue(request)
Lane answered 14/9, 2021 at 9:1 Comment(0)
M
3

I tried only in Compose but I think you can use it like Glide

Set the progress bar as placeholder

val circularProgressDrawable = CircularProgressDrawable(this)
circularProgressDrawable.strokeWidth = 5f
circularProgressDrawable.centerRadius = 30f
circularProgressDrawable.start()

imageView.load("https://www.example.com/image.jpg") {
    crossfade(true)
    placeholder(circularProgressDrawable)
    transformations(CircleCropTransformation())
}
Mn answered 26/8, 2021 at 15:31 Comment(0)
N
3

If you are creating app with jetpack compose then you don't need to do manual stuff,

you can use the builtin SubcomposeAsyncImage provided by the library from version 2.0.0-rc01

SubcomposeAsyncImage(
    model = image,
    contentDescription = "",
    loading = { 
        CircularProgressIndicator(color = Color.Black) 
    },
)
Novah answered 30/3, 2023 at 4:56 Comment(2)
SubcomposeAsyncImage has bad performance and is not recommended for the the ListsLamoureux
thank you @AnmolSinghSahi for informationNovah
R
2

I didn't work with Coil, but I suggest you may do the next:

// Before request run on the needed method
yourProgressbar.visibility = View.VISIBLE

val request = ImageRequest.Builder(context)
.data("https://www.example.com/image.jpg")
.target { drawable ->
    yourProgressbar.visibility = View.INVISIBLE
}.build()   

val disposable = imageLoader.enqueue(request)

Here is official doc's example

Recording answered 26/8, 2021 at 12:24 Comment(0)
C
1

Here what i am doing, it is working fine as expected

 binding.circularProgressBar.visibility = View.VISIBLE
 binding.ivTeamPic.load(url) {
      target {
           binding.circularProgressBar.visibility = View.GONE
           binding.ivTeamPic.setImageDrawable(it)
      }
 }
Cooperman answered 20/4, 2023 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.