Skip to content
Elvis Chidera

Glide like a Boss — Advanced Android Glide Tips

android1 min read

I will be sharing some tips I learned while using the Glide Android Library for a project.

banner

1. Wifi Only Mode

Sometimes you might want to download images only when the user device is connected to a WiFi network. Maybe this might be an option in the app that the user can turn on or your strategy for reducing the user data consumption when on mobile data. Whatever your use case is, you can easily prevent Glide from making a network call based on a particular state.

First, you will have to create a custom Glide loader class. What this class will do is to always throw an exception whenever an image load is requested thereby disabling a network call from being made. Cached images will still be shown but new images won’t be fetched over the network.

NetworkDisablingLoader.java
1import com.bumptech.glide.Priority;
2import com.bumptech.glide.load.data.DataFetcher;
3import com.bumptech.glide.load.model.stream.StreamModelLoader;
4
5import java.io.IOException;
6import java.io.InputStream;
7
8class NetworkDisablingLoader implements StreamModelLoader<String> {
9
10 @Override
11 public DataFetcher<InputStream> getResourceFetcher(final String model, int width, int height) {
12
13 return new DataFetcher<InputStream>() {
14
15 @Override
16 public InputStream loadData(Priority priority) throws Exception {
17 throw new IOException("Forced Glide network failure");
18 }
19
20 @Override
21 public void cleanup() { }
22
23 @Override
24 public String getId() { return model; }
25
26 @Override
27 public void cancel() { }
28
29 };
30
31 }
32
33}

Next, you will have to attach this loader to Glide whenever you need it. For this, we create a GlideUtils class that we will pass in the required parameters and it will return the correct Glide RequestManager.

GlideUtils.java
1import android.content.Context;
2import android.support.annotation.NonNull;
3
4import com.bumptech.glide.Glide;
5import com.bumptech.glide.RequestManager;
6
7public class GlideUtils {
8
9 public static @NonNull RequestManager getGlide(@NonNull Context context, boolean isOnlyOnWiFi) {
10 RequestManager request;
11
12 if (isOnlyOnWiFi) {
13 request = Glide.*with*(context);
14 request.using(new NetworkDisablingLoader());
15 } else {
16 request = Glide.*with*(context);
17 }
18
19 return request;
20 }
21
22}

To use this in your app or wherever you want to load images, you just use similar code below

GlideWifiOnlyUsage.java
1boolean isInWifiOnlyMode = sharedPreference.getBoolean("is_wifi_only", false);
2
3GlideUtils.getGlide(context, isInWifiOnlyMode).load("http://imageUrl.com").into(imageView);

2. Image height or width is less when loading for the first time

When using Glide with a placeholder, you might notice that sometimes the height or width of the actual image is less when its loaded for the first time. This usually happens when the aspect ratio of the placeholder image and the actual image are not the same and the crossfade animation is switched on (its switched on by default).

When I first saw this I thought it was due to an implementation error with the Glide library and spent some time trying to fix it. This is a known issue and I am sharing it here to save someone from wasting time trying to fix this.

You have three options to fix this:

  1. You can disable animations with .dontAnimate()

  2. Force a fade-in animation with .animate(R.anim.fade_in)

  3. Find a placeholder that matches the actual image’s ratio

© 2024 by Elvis Chidera. All rights reserved.