All CategoriesAndroid App Development

How to use Universal Image Loader Library in Android

Universal Image Loader: An Introduction:
Universal Image Loader is one of the library for loading the remote images. It is free and open source. We can easily get the library and include it in our project to create an application.
We can configure and set up the options according to the need to show the images.
Its alternatives are Picasso and Volley.
Advantages of Universal Image Loader
⦁ It loads, caches, and displays the images.
⦁ Performs multithreading, which means it loads the multiple images asynchronously.
⦁ It also caches images in memory or SD card.
Get the Library:
To implement this feature in our application, we have to download the Universal Image Loader library from the official website and add it inside our project.
Example with Code:
Here, in this example, we will display the images in GridView.
Let’s do it programmatically.
⦁ Create an application UniversalImageLoaderExampleAndroid.
⦁ Add the library in our project.
Requirements: A class to setup the default configuration for Universal Image Loader.,,, activity_main.xml, grid_item_layout.xml.
Let’s do it one by one.
Create this class and extend it with Application. It is a class which is used to configure the setup options for Universal Image Loader and use it from anywhere in the project.
Add the code in onCreate() method:

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.displayer(new FadeInBitmapDisplayer(300)).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
.memoryCache(new WeakMemoryCache())

Initialize the image loader with configuration.

We can also set the other options here and modify it to display the images.
Create the modal class for item.
Add the code here:
private String image;
public String getImage() {
return image;
public void setImage(String image) {
this.image = image;

Android Programmingactivity_main.xml:
Create this XML file for our main layout.
Add the code here

android:verticalSpacing="5dp" />

Create this XML file for layout item, here we show the image.
Add the code here:

android:scaleType="centerCrop" />
Create this Custom Adapter class to load and display the images. It inflates the view of our item.
Add the code in getView() method:
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) row.findViewById(;
UniversalImageItem item = mGridData.get(position);
ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
imageLoader.displayImage(item.getImage(), holder.imageView, options);

Here, imageLoader is used to get the images and set the properties accordingly.
This is our MainActivity. Inside this we will set the custom adapter, take the reference of GridView, parse the JSON to show the images using URL.

private GridView mGridView;
private UniversalImageAdapter mGridAdapter;
private ArrayList<UniversalImageItem> mGridData;
private String FEED_URL = "";

Add the code in onCreate(){..} method:

mGridView = (GridView) findViewById(;
mGridData = new ArrayList<>();
mGridAdapter = new UniversalImageAdapter(this, R.layout.grid_item_layout, mGridData);
new AsyncHttpTask().execute(FEED_URL);
Now, we execute the task with URL and update the UI in onPostExecute() method after parsing.
Add the code for parsing:
JSONObject response = new JSONObject(result);
JSONArray array = response.getJSONArray("backdrops");
UniversalImageItem item;
for (int i = 0; i < array.length(); i++) {
JSONObject imageobject = array.getJSONObject(i);
String image_url = imageobject.getString("file_path");
item = new UniversalImageItem();

Next, update the UI


This was all about the Universal Image Loader. Here we learned how to get the library and use it in our application. It was a simple example to show the images in a GridView, the loading, caching and displaying process. We can customize it and create the many applications by using this powerful library. It is quite popular in Android development.

Download Source Code: GitHub


Hope you find this blog section informative, keep visiting for more updates on the courses.

Android Programming

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles