Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glide - Load a GIF as a placeholder

I'm using Glide in my Android project, and I have a RecyclerView with a list of CardViews in it. What I wanna do is to load an image for each CardView while showing a GIF loading image until the real image gets loaded. Now if it try to load the GIF as the main image, no problem:

Glide.with(context)
        .load(R.raw.gif_loading_bar)
        .into(imageView);

The code above works fine and the GIF gets loaded. But when I try to load another image and make the GIF a placeholder Android Studio shows an error "Expected resource of type drawable":

Glide.with(mContext)
        .setDefaultRequestOptions(new RequestOptions()
                .placeholder(R.raw.gif_loading_bar))
        .load(imageUrl)
        .into(imageView);

What should I do to fix this?

like image 717
Ameer Taweel Avatar asked Oct 25 '25 11:10

Ameer Taweel


2 Answers

Glide does support GIF as placeholder. I recently faced this situation and solved it like below. Hope it helps someone like me who is facing this situtaion.

    var builder = Glide.with(mContext).load(path).apply(options).thumbnail(Glide.with(mContext).load(R.raw.loader2))
if (listener == null) {
    builder = builder.addListener(object : RequestListener<Drawable> {
        override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {

            //TODO; your code here
            return false
        }

        override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
            //TODO; your code here
            return false
        }
    })
} else {
    builder = builder.addListener(listener)
}
builder.into(myImageView)

What it does

It load your gif as thumbnail until the image is downloaded from the source. Inside the onResourceReady method it will give you the callback when the resource is ready. Make sure to put gif file under R.raw package and not in drawable package. Let me know if you face any issue.

like image 114
Rahul Khurana Avatar answered Oct 27 '25 00:10

Rahul Khurana


Unfortunately, Glide does not support GIF placeholders, as mentioned in this Github issue, and this one.

like image 38
Ameer Taweel Avatar answered Oct 27 '25 01:10

Ameer Taweel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!