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
-
Android Studio version 3.6.1
-
Little bit XML and KOTLIN knowledge
-
Android Emulator (or) Android mobile
-
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.
Step 2
Now, add the activity and click the "Next" button.
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.
Step 4
Go to the build.grade file. Add third party dependencies for Glide.
The third party dependency is given below:
- implementation 'com.github.bumptech.glide:glide:4.11.0'
- annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
Step 5
Go to the manifest file and add the dependency for internet connection.
The manifest code is given below:
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- <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:
The XML code is given below:
- <?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"
- tools:context=".MainActivity">
- <androidx.appcompat.widget.AppCompatImageView
- android:id="@+id/imageview"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/ic_launcher_background"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </androidx.constraintlayout.widget.ConstraintLayout>
Step 7
Go to the Main Activity.kt. This Kotlin program is the back-end language for your app:
The Kotlin code is given below:
- package com.example.glide
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import com.bumptech.glide.Glide
- import kotlinx.android.synthetic.main.activity_main.*
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- val media = "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_960_720.jpg"
- if (media !== null) {
- Glide.with(this)
- .load(media)
- .into(imageview)
- } else {
- imageview.setImageResource(R.drawable.ic_launcher_background)
- }
- }
- }
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