Progress Bar While Loading Image Using Glide

Progress bar while loading image using Glide

Edit: This is super simple now with the CircularProgressDrawable

build.gradle

implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

MyGlideModule.kt

@GlideModule
class MyGlideModule : AppGlideModule()

MainActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

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

GlideApp.with(applicationContext)
.load("https://raw.githubusercontent.com/bumptech/glide/master/static/glide_logo.png")
.placeholder(circularProgressDrawable)
.into(a_main_image)
}

These are some other Glide snippets


Old answer: You could also create a normal ProgressBar, and then hide it on Glide's onResourceReady().

The method that will be called when the resource load has finished.


Example:

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final ImageView imageView = (ImageView) findViewById(R.id.img_glide);
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress);

Glide.with(this)
.load("https://raw.githubusercontent.com/bumptech/glide/master/static/glide_logo.png")
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}

@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
})
.into(imageView);
}

activity_main.xml (layout):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ProgressBar
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="100dp"
android:visibility="visible" />

<ImageView
android:id="@+id/img_glide"
android:layout_width="match_parent"
android:layout_height="100dp" />

</RelativeLayout>

Result:

Sample Image

Set visibility of progress bar gone on completion of image loading using Glide library

Question is rather old, and I don't know what was the situation with glide in those times, but now it can be easily done with listener (not as proposed in the answer chosen as correct).

progressBar.setVisibility(View.VISIBLE);
Glide.with(getActivity())
.load(args.getString(IMAGE_TO_SHOW))
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}

@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
})
.into(imageFrame)
;

You return true if want to handle things like animations yourself and false if want glide to handle them for you.

Show progress Bar when loading image with Glide in DataBinding

If you want using 2nd method:

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:progressBar="@{progressImageSlider}"
android:src="@{imageUrl}" />

But I think you should using placeholder in this case, please see my example

@BindingAdapter(value = { "android:src", "placeholder" }, requireAll = false)
public static void loadImage(ImageView view, String url, Drawable placeHolder) {
if (!TextUtils.isEmpty(url)) {
Glide.with(view.getContext()).load(url).placeholder(placeHolder).into(view);
}
}

And call it in your xml

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:placeholder="@{@drawable/your_place_holder}"
android:src="@{imageUrl}" />

hope this helps !

How to show progressbar using Volley/ Glide/Picasso or other , while image downloading from server

Try this using glide

progressBar = (ProgressBar)findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
Glide.with(context)
.load(imageUrl)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
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);

with Picasso

Picasso.with(context)
.load(imageUrl)
.into(imageview, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
//do something when picture is loaded successfully
progressBar.setVisibility(View.GONE);

}

@Override
public void onError() {
//do something when there is picture loading error
}
});

with AsyncTask

public class DownloadImage extends AsyncTask<String, Integer, Bitmap> {
ImageView bmImage;
ProgressBar progressBar;

public DownloadImage(ImageView bmImage, ProgressBar progressBar) {
this.bmImage = bmImage;
this.progressBar = progressBar;
}

protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.d("Error", e.getStackTrace().toString());

}
return mIcon11;
}

@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progressBar.setProgress(values[0]);
}

protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}

use like this

 new DownloadImage(imamgeview,progressBar).execute(url);

Circular progress bar as Glide Placeholder

In view pager you can use the following method inside instantiate method. The first method is for loading gif images and the second method is for simple image loading with progress bar.

////  for loading gif images RequestOptions requestOptions = new RequestOptions();        requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);        requestOptions.placeholder(R.drawable.itemes_bg);        requestOptions.error(R.drawable.itemes_bg);

Glide.with(context).applyDefaultRequestOptions(requestOptions).asGif().load(gifList.get(position)).listener(new RequestListener<GifDrawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) { Toast.makeText(context, "No Internet Connection...", Toast.LENGTH_SHORT).show(); return false; }
@Override public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
return false; } }).into(holder.imageView);
//// for loading simple images
RequestOptions requestOptions = new RequestOptions(); requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL); requestOptions.placeholder(R.drawable.progress);
Glide.with(activity).applyDefaultRequestOptions(requestOptions).load(imageList.get(position).getImgUrl().trim()).listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { progressBar.setVisibility(View.GONE); Toast.makeText(activity, "No Internet Connection", Toast.LENGTH_SHORT).show(); return false; }
@Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { progressBar.setVisibility(View.GONE); return false; } }).into(imageView);


Related Topics



Leave a reply



Submit