Hi I want to have a progress bar for image which will shown while image loading but when image loading will be completed I want to set it to gone. Earlier I was using Picass
If you want to do this in KOTLIN, you can try that way:
Glide.with(context)
.load(url)
.listener(object : RequestListener<Drawable> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
//TODO: something on exception
}
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
Log.d(TAG, "OnResourceReady")
//do something when picture already loaded
return false
}
})
.into(imgView)
Kotlin way
Glide.with(context)
.load(image_url)
.listener(object : com.bumptech.glide.request.RequestListener<Drawable> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable>?,
isFirstResource: Boolean
): Boolean {
return false
}
override fun onResourceReady(
resource: Drawable?,
model: Any?,
target: Target<Drawable>?,
dataSource: DataSource?,
isFirstResource: Boolean
): Boolean {
img_product_banner.visibility = View.VISIBLE
return false
}
}).placeholder(R.drawable.placeholder)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(img_product_banner)
In exception put a condition for show again the ProgressBar
Glide.with(context)
.load(image_url)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
if(e instanceof UnknownHostException)
progressBar.setVisibility(View.VISIBLE);
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
})
.into(imageView);
Update:
Glide.with(this)
.load(imageUrl)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable final GlideException e,
final Object model, final Target<Drawable> target,
final boolean isFirstResource) {
showProgress(false);
mNoContentTextView.setVisibility(View.VISIBLE);
return false;
}
@Override
public boolean onResourceReady(final Drawable resource,
final Object model,
final Target<Drawable> target,
final DataSource dataSource,
final boolean isFirstResource) {
showProgress(false);
mNoContentTextView.setVisibility(View.GONE);
mContentImageView.setImageDrawable(resource);
return false;
}
})
.into(mContentImageView);
My answer was based on out-dated APIs. See here for the more up-to-date answer.
In Kotlin you can do like below
Glide.with(context)
.setDefaultRequestOptions(RequestOptions().placeholder(R.drawable.ic_image_placeholder).error(R.drawable.ic_image_placeholder))
.load(url)
.listener(object : RequestListener<Drawable>{
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
return false
}
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
return false
}
})
.into(imageView)