How to Load ImageURL to Imageview Using Glide in Kotlin 😊

Introduction

 
Android is an open-source operating system based on Linux with a Java programming interface for mobile devices such as your smartphone (touch screen devices that support Android OS) as well as tablets. With over 85% market share worldwide, Android dominates the mobile platform market. Today, I will show you how to load the ImageURL to Imageview using Glide in Kotlin.
 
Requirements
Steps:
 
Follow these steps to load the ImageURL to Imageview using Glide In Kotlin. I have included the source code in the attachment.
 
Step 1
 
Open Android Studio and start a new Android Studio Project.
 
How To Load The ImageURL To Imageview Using Glide In Kotlin
 
Step 2
 
Now, add the activity and click the "Next" button.
 
How To Load The ImageURL To Imageview Using Glide In Kotlin
 
Step 3
 
You can choose your application name and choose where your project is to be stored. Choose Kotlin language for coding the project. Now, select the version of Android and select the target Android devices, and click the "Finish" button.
 
How To Load The ImageURL To Imageview Using Glide In Kotlin
 
Step 4
 
Go to the build.grade file. Add third party dependencies for Glide.
 
How To Load The ImageURL To Imageview Using Glide In Kotlin
 
The third party dependency is given below:
  1. implementation 'com.github.bumptech.glide:glide:4.11.0'    
  2. annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'     
Step 5
 
Go to the manifest file and add the dependency for internet connection.
 
How To Load The ImageURL To Imageview Using Glide In Kotlin
 
The manifest code is given below:
  1. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>    
  2. <uses-permission android:name="android.permission.INTERNET" />     
Step 6
 
Go to the activity_main.xml. This XML file contains the code for your Android app:
 
How To Load The ImageURL To Imageview Using Glide In Kotlin
 
The XML code is given below:
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.    xmlns:app="http://schemas.android.com/apk/res-auto"    
  4.    xmlns:tools="http://schemas.android.com/tools"    
  5.    android:layout_width="match_parent"    
  6.    android:layout_height="match_parent"    
  7.    tools:context=".MainActivity">    
  8.    <androidx.appcompat.widget.AppCompatImageView    
  9.       android:id="@+id/imageview"    
  10.       android:layout_width="wrap_content"    
  11.       android:layout_height="wrap_content"    
  12.       android:src="@drawable/ic_launcher_background"    
  13.       app:layout_constraintBottom_toBottomOf="parent"    
  14.       app:layout_constraintLeft_toLeftOf="parent"    
  15.       app:layout_constraintRight_toRightOf="parent"    
  16.       app:layout_constraintTop_toTopOf="parent" />    
  17. </androidx.constraintlayout.widget.ConstraintLayout>     
Step 7
 
Go to the Main Activity.kt. This Kotlin program is the back-end language for your app:
 
How To Load The ImageURL To Imageview Using Glide In Kotlin
 
The Kotlin code is given below:
  1. package com.example.glide  
  2. import androidx.appcompat.app.AppCompatActivity  
  3. import android.os.Bundle  
  4. import com.bumptech.glide.Glide  
  5. import kotlinx.android.synthetic.main.activity_main.*  
  6. class MainActivity : AppCompatActivity() {  
  7.    override fun onCreate(savedInstanceState: Bundle?) {  
  8.       super.onCreate(savedInstanceState)  
  9.       setContentView(R.layout.activity_main)  
  10.       val media = "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_960_720.jpg"  
  11.       if (media !== null) {  
  12.          Glide.with(this)  
  13.          .load(media)  
  14.          .into(imageview)  
  15.          } else {  
  16.       imageview.setImageResource(R.drawable.ic_launcher_background)  
  17.       }  
  18.    }  
  19. }  
Step 8
 
Then, click the "Run" button or press Shift + F10 to finally run the project. Choose the "virtual machine" option and click OK.
 

Conclusion

 


Similar Articles