RecyclerView using glide library Android Kotlin.
Let's start the code for the recycler view using the glide library in android Kotlin.
In build.Gradle(: app) file I am using glide dependency.
implementation 'com.github.bumptech.glide:glide:4.12.0'
kapt 'com.github.bumptech.glide:compiler:4.12.0'
Add plugin in build.Gradle(: app) file.
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}
I am using view binding in this project so we need to add a build feature in the build.Gradle(:app) file.
android {
buildFeatures {
viewBinding = true
}
}
We also need to add internet permission in the manifest file.
<uses-permission android:name="android.permission.INTERNET"/>
Now let's start with the designing part.
activity_main.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"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_design"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:layout_marginEnd="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:listitem="@layout/item_recycler" />
</androidx.constraintlayout.widget.ConstraintLayout>
We need to add recycle items. so here I create item_recycler.xml file
<?xml version="1.0" encoding="utf-8"?>
<ImageView 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:id="@+id/iv_glide"
android:layout_marginTop="15dp"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:scaleType="fitXY">
</ImageView>
Now we need to add AppGlideModule.kt file.
package com.example.gliderecyclerview
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.module.AppGlideModule
@GlideModule
class AppGlideModule : AppGlideModule()
Now we need to code for the recycler adapter class.
RecyclerAdapter.kt
package com.example.gliderecyclerview
import android.content.Context
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.load.resource.bitmap.FitCenter
class RecyclerAdapter(
private val context: Context
) :
RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
val view = LayoutInflater.from(viewGroup.context)
.inflate(R.layout.item_recycler, viewGroup, false)
return ViewHolder(view)
}
override fun onBindViewHolder(viewHolder: ViewHolder, i: Int) {
var requestOptions = RequestOptions()
requestOptions = requestOptions.transforms(FitCenter(), RoundedCorners(16))
Glide.with(context)
.load("https://openai.com/content/images/2021/01/2x-no-mark-1.jpg")
.apply(requestOptions)
.skipMemoryCache(true)//for caching the image url in case phone is offline
.into(viewHolder.img_android)
}
override fun getItemCount(): Int {
return 10
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var img_android: ImageView
init {
img_android =
view.findViewById<View>(R.id.iv_glide) as ImageView
}
}
}
Now we need to code for MainActivity.kt file.
package com.example.gliderecyclerview
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.gliderecyclerview.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
var recyclerView: RecyclerView? = null
var Manager: GridLayoutManager? = null
var adapter: RecyclerAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
recyclerView = findViewById<View>(R.id.rv_design) as RecyclerView
Manager = GridLayoutManager(this,2)
recyclerView!!.layoutManager = Manager
adapter = RecyclerAdapter(this@MainActivity)
recyclerView!!.adapter = adapter
}
}
That’s it.