Implement Universal Image Loader Library in Android using Kotlin
Last Updated :
13 Jun, 2022
Universal Image Loader library is also referred to as (UIL). It is similar to Picasso and Glide which is used to load the images from the URL within our image view inside our android application. 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. In this article, we will take a look at the implementation of the UIL library within the android application using Kotlin.
Note: If you are looking to use the UIL library in android application using Java. Check out the following article: How to use UIL Image loader library in Android using Java
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select 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’
After adding this dependency simply sync your project to install it.
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
XML
<?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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!--on below line we are creating
a simple text view for heading-->
<TextView
android:id="@+id/idTVHead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="20dp"
android:gravity="center"
android:padding="8dp"
android:text="Universal Image Loader"
android:textAlignment="center"
android:textColor="@color/purple_200"
android:textSize="20sp"
android:textStyle="bold" />
<!--on below line we are creating an
image view for displaying image from url-->
<ImageView
android:id="@+id/idIVimage"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true" />
</RelativeLayout>
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.gtappdevelopers.kotlingfgproject
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() {
// on below line we are creating
// a variable for our image view.
lateinit var imageView: ImageView
// on below line we are creating
// a variable for display options.
lateinit var displayOptions: DisplayImageOptions
// on below line we are creating
// a variable for image loader.
lateinit var imageLoader: ImageLoader
// on below line we are creating a variable for image url
var imgUrl = "https://www.geeksforgeeks.org/wp-content/uploads/gfg_200X200-1.png"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// on below line we are initializing
// our image view with its id.
imageView = findViewById(R.id.idIVimage)
// on below line we are
// initializing our image loader.
imageLoader = ImageLoader.getInstance()
// on below line we are initializing image loader with its configuration.
imageLoader.init(ImageLoaderConfiguration.createDefault(applicationContext))
// on below line we are initializing our display options
displayOptions = DisplayImageOptions.Builder()
// on below line we are adding a
// stub image as error image.
.showStubImage(R.drawable.ic_error)
// on below line we are adding an error
// image this image will be displayed
// when the image url is empty
.showImageForEmptyUri(R.drawable.ic_error)
// on below line we are calling
// cache in memory and then calling build.
.cacheInMemory().build();
// on below line we are calling image loader
// to display our image in our image view from image url
imageLoader.displayImage(imgUrl, imageView, displayOptions, null)
}
}
Now run your application to see the output of it.
Output:
Similar Reads
Android Image Slider using ViewPager in Kotlin Image slider is seen in most e-commerce applications that display advertisements on the home screen. This slider displays the advertisement banners which users can slide to view the others. In this article, we will take a look at Implementing the image slider using ViewPager in Android using Kotlin.
5 min read
Overlay Multiple Images in a Single ImageView in Android In Android, an image can be displayed inside an ImageView in the layout. ImageView accepts only a single image at a time as input. However, there can be a need to display two or more images in the same ImageView. Let us say, our application shows the status of airplane mode. When airplane mode is en
3 min read
How to Use COIL Image Loader Library in Android Apps? COIL is an acronym for Coroutine Image Loader. COIL is one of the famous image loading libraries from URLs in Android. It is a modern library for loading images from the server. This library is used to load images from servers, assets folder as well as from the drawable folder in Android project. Th
4 min read
JSON Parsing in Android Using Volley Library with Kotlin JSON is a JavaScript object notation which is a format to exchange the data from the server. JSON stores the data in a lightweight format. With the help of JSON, we can access the data in the form of JsonArray, JsonObject, and JsonStringer. In this article, we will specifically take a look at the im
5 min read
Generate PDF File in Android using Kotlin Most of the applications provide users with the facility of bills to be downloaded from the mobile applications. This type of application gets the data from the APIS or data within the application and this data is used to generate the PDF files. PDF files within android are generated using canvas. I
7 min read
Android - JSON Parsing using Retrofit Library with Kotlin JSON is a format with the help of which we can exchange the data from the server within our application or a website. For accessing this data from the server within android applications. There are several libraries that are available such as Volley and Retrofit. In this article, we will take a look
6 min read