In my app I download an image from an URL and set it into an ImageView through Glide, however, I'm trying to remove a few unnecessary layouts, so is it possible to use Glide to download an image and set into to TextView?
try {
Glide.with(holder.logo.getContext())
.load(standingObjectItems.get(position).getImgId()).diskCacheStrategy(DiskCacheStrategy.ALL)
.error(R.mipmap.ic_launcher)
.placeholder(R.mipmap.ic_launcher)
.into(holder.logo);
} catch (IllegalArgumentException | IndexOutOfBoundsException e) {
e.printStackTrace();
}
To simply load an image to LinearLayout, we call the with() method of Glide class and pass the context, then we call the load() method, which contains the URL of the image to be downloaded and finally we call the into() method to display the downloaded image on our ImageView.
Glide is an Image Loader Library for Android developed by bumptech and is a library that is recommended by Google. It has been used in many Google open source projects including Google I/O 2014 official application. It provides animated GIF support and handles image loading/caching.
Here is a simple example of how to do using Kotlin.
GlideApp.with(context)
.load("url")
.placeholder(R.drawable.your_placeholder)
.error(R.drawable.your_error_image)
.into(object : CustomTarget<Drawable>(100, 100) {
override fun onLoadCleared(drawable: Drawable?) {
header.companyLogoJob.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null)
}
override fun onResourceReady(res: Drawable, transition: com.bumptech.glide.request.transition.Transition<in Drawable>?) {
header.companyLogoJob.setCompoundDrawablesWithIntrinsicBounds(res,null,null,null)
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With