Recycler View with View Binding in Android Kotlin.

Harshita Bambure
2 min readOct 24, 2021

I am creating a recycler view with an adapter using view binding in android kotlin.

So first of all we need to add a build feature in the build.Gradle(:app) file.

buildFeatures {
viewBinding true
}

Now, let us start with the design 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_recycler"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/item_recyler"/>
</androidx.constraintlayout.widget.ConstraintLayout>

I am creating a list of items for my recycler view.

item_recyler.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
app:cardCornerRadius="5dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name "
android:textAppearance="?attr/textAppearanceHeadline6" />

<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textAppearance="?attr/textAppearanceHeadline6" />


</LinearLayout>

</com.google.android.material.card.MaterialCardView>

Now we will create a data class for recycler items.

Data. kt

package com.example.recyclerview

data class Data(val name:String,val email:String)

Let us create an adapter class.

UserAdapter.kt

package com.example.recyclerview

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.recyclerview.databinding.ItemRecylerBinding

class UserAdapter(var datalist:List<Data>):RecyclerView.Adapter<UserAdapter.UserHolder>() {


class UserHolder(val binding: ItemRecylerBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(get: Data) {
binding.email.text = get.email
binding.name.text = get.name

}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserHolder {
val binding = ItemRecylerBinding
.inflate(LayoutInflater.from(parent.context), parent, false)
return UserHolder(binding)
}



override fun onBindViewHolder(holder: UserHolder, position: Int) {
holder.bind(datalist.get(position))
}

override fun getItemCount(): Int {
return datalist.size
}

}

Now we need to code for Main Activity.

MainActivity.kt

package com.example.recyclerview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.recyclerview.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding
private lateinit var adapter: UserAdapter
private lateinit var user :List<Data>


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding= ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
userList()
binding.rvRecycler.layoutManager = LinearLayoutManager(this)
adapter = UserAdapter(user)
binding.rvRecycler.adapter = adapter

}
private fun userList(){
user = listOf(
Data("Harshita","Harshita@gmail.com"),
Data("minu","minu@gmail.com"),
Data("ram","ram@gmail.com"),
Data("sham","sham@gmail.com"),
Data("raja","raja@gmail.com"),
Data("harsh","harsh@gmail.com"),
Data("harshu","harshu@gmail.com"),
Data("xyz","xyz@gmail.com"),
Data("abc","abc@gmail.com"),
Data("pqr","pqr@gmail.com")
)
}

}
recycler view using view binding in android kotlin.

The source code.

That's it. Happy Coding!!

--

--

Harshita Bambure

Android Developer || WomenTech Global Ambassador at WomenTech Network. || Yoga Teacher || Member @WTM .