How to Use Universal Image Loader Library in Android?
Last Updated :
04 Feb, 2025
UIL (Universal Image Loader) is a similar library to that of Picasso and Glide which performs loading images from any web URL into ImageView of Android. This image loading library has been created to provide a powerful, flexible, and customizable solution to load images in Android from Server. This image loading library is being created by an indie developer and it is present in the top list of GitHub. Features of UIL (universal Image Loader) library:
- This library provides Multi-thread image loading.
- Image caching can be done in memory and on the user's device.
- Listening loading process (including downloading progress).
- Many customizable features are available for every display image call.
Step by Step Implementation UIL
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Note that select Java/Kotlin as the programming language.
Step 2: Add dependency of UIL Image library in build.gradle file.
Navigate to the gradle scripts and then to build.gradle(Module) level. Add below line in build.gradle file in the dependencies section.
implementation ("com.nostra13.universalimageloader:universal-image-loader:1.9.5")
Step 3: Add google repository in the build.gradle file of the application project if by default it is not there
repositories {
google()
mavenCentral()
}
Step 4: Add internet permission in the AndroidManifest.xml file
Navigate to the app > Manifest to open the Manifest file.
<uses-permission android:name="android.permission.INTERNET" />
Step 5: Create a new ImageView in your activity_main.xml.
Navigate to the app > res > layout to open the activity_main.xml file. Below is the code for the activity_main.xml file.
activity_main.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">
<!--ImageView is created below-->
<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 6: Initialize your ImageView and use UIL(Universal Image Loader) in the MainActivity file
Navigate to the app > kotlin+java > {package name} > MainActivity.kt/MainActivity.java file. Below is the code for the MainActivity file in both Java and Kotlin. Comments are added inside the code to understand the code in more detail.
MainActivity File:
Java
package org.geeksforgeeks.demo;
import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class MainActivity extends AppCompatActivity {
private ImageView img;
DisplayImageOptions options;
ImageLoader imageLoader;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize image loader before using
imageLoader = ImageLoader.getInstance();
imageLoader.init(
ImageLoaderConfiguration.createDefault(
getApplicationContext()));
// initialize imageview from activity_main.xml
img = findViewById(R.id.idImageView);
// URL for our image that we have to load..
String imageUri
= "https://www.geeksforgeeks.org/wp-content/uploads/gfg_200X200-1.png";
// with below method we are setting display option
// for our image..
options = new DisplayImageOptions
.Builder()
// stub image will display when your
// image is loading
.showStubImage(
R.drawable.ic_launcher_foreground)
// below image will be displayed when
// the image url is empty
.showImageForEmptyUri(
R.drawable.ic_launcher_background)
// cachememory method will caches the
// image in users external storage
.cacheInMemory()
// cache on disc will caches the image
// in users internal storage
.cacheOnDisc()
// build will build the view for
// displaying image..
.build();
// below method will display image inside our image
// view..
imageLoader.displayImage(imageUri, img, options,
null);
}
}
Kotlin
package org.geeksforgeeks.demo
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.nostra13.universalimageloader.core.DisplayImageOptions
import com.nostra13.universalimageloader.core.ImageLoader
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration
class MainActivity : AppCompatActivity() {
private lateinit var img: ImageView
private lateinit var options: DisplayImageOptions
private lateinit var imageLoader: ImageLoader
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// initialize image loader before using
imageLoader = ImageLoader.getInstance()
imageLoader.init(
ImageLoaderConfiguration.createDefault(
applicationContext
)
)
img = findViewById(R.id.imageView)
val imageUri = "https://www.geeksforgeeks.org/wp-content/uploads/gfg_200X200-1.png"
// with below method we are setting display option
// for our image..
options = DisplayImageOptions.Builder() // stub image will display when your
// image is loading
.showStubImage(
R.drawable.ic_launcher_foreground
) // below image will be displayed when
// the image url is empty
.showImageForEmptyUri(
R.drawable.ic_launcher_background
) // cachememory method will caches the
// image in users external storage
.cacheInMemory() // cache on disc will caches the image
// in users internal storage
.cacheOnDisc() // build will build the view for
// displaying image..
.build()
// below method will display image inside our image
// view..
imageLoader.displayImage(
imageUri, img, options,
null
)
}
}
Note: All drawables are present in the drawable folder. You can add the drawable in the drawable folder. To access the drawable folder. Navigate to app > res > drawables, this folder is having all your drawables.
Output:
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read