Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to Use View Binding in RecyclerView Adapter Class in Android?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

View Binding is a part of Android Jetpack which provides the views to bind with the classes by replacing the findViewById() method in a null safe way. In this article, we will see its implementations in the Adapter class of RecyclerView. Note that we are going to implement this project using the Kotlin language. 

Step by Step Implementation

Step 1: Create a new project

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 view binding dependency

Go to build.gradle(app) and the following dependency inside the android tag and click sync now.

 buildFeatures {

       viewBinding true


Step 3: Working with the activity_main.xml file

Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file. It has only a single Recycler view which we will use to show our data.


<?xml version="1.0" encoding="utf-8"?>
    <!--Add recycler view to main activity-->
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

Step 4: Create a new layout file and name it as single_item.xml file

Go to the single_item.xml file and refer to the following code. Below is the code for the single_item.xml file. It is the single item layout that we will use in RecyclerView. 


<?xml version="1.0" encoding="utf-8"?>
          <!--Add image view, We will not set any data here.-->
            app:layout_constraintTop_toTopOf="parent" />
          <!--Text view for showing the language name-->
            app:layout_constraintTop_toTopOf="parent" />
          <!--Text View for showing the exp-->
            android:text="Exp : 3 years"
            app:layout_constraintTop_toBottomOf="@id/tv_lang_name" />

Step 5: Create a new Model class 

Create a new class Language.kt we will use data of custom generic “Language” to pass in the list that will be shown in RecyclerView. 


// this is the Language model class
class Language(
    val name : String ="",
    val exp : String =""

Step 6: Working with the Adapter class

Create a new class RvAdapter.kt this will act as an Adapter class for the recycler view. Using View binding we use the generated class of the layout single_item.xml ie SingleItemBinding to add data and view in the recycler view of MainActivity.kt in our case. Comments are added before the code for better understanding. 


import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.geeksforgeeks.rvadapterviewbinding.databinding.SingleItemBinding
class RvAdapter(
    var languageList: List<Language>,
) : RecyclerView.Adapter<RvAdapter.ViewHolder>() {
    // create an inner class with name ViewHolder
    // It takes a view argument, in which pass the generated class of single_item.xml
    // ie SingleItemBinding and in the RecyclerView.ViewHolder(binding.root) pass it like this
    inner class ViewHolder(val binding: SingleItemBinding) : RecyclerView.ViewHolder(binding.root)
    // inside the onCreateViewHolder inflate the view of SingleItemBinding
    // and return new ViewHolder object containing this layout
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = SingleItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    // bind the items with each item
      // of the list languageList
      // which than will be
    // shown in recycler view
    // to keep it simple we are
      // not setting any image data to view
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
                binding.tvLangName.text =
                binding.tvExp.text = this.exp
    // return the size of languageList
    override fun getItemCount(): Int {
        return languageList.size

Step 7: 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. 


import android.os.Bundle
import com.geeksforgeeks.rvadapterviewbinding.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
    // view binding for the activity
    private var _binding : ActivityMainBinding? = null
    private val binding get() = _binding!!
    // create reference to the adapter and the list
    // in the list pass the model of Language
    private lateinit var rvAdapter: RvAdapter
    private lateinit var languageList : List<Language>
    override fun onCreate(savedInstanceState: Bundle?) {
        _binding = ActivityMainBinding.inflate(layoutInflater)
        // load data to language list
        // create  layoutManager
        val layoutManager: RecyclerView.LayoutManager = LinearLayoutManager(this)
        // pass it to rvLists layoutManager
        // initialize the adapter,
          // and pass the required argument
        rvAdapter = RvAdapter(languageList)
        // attach adapter to the recycler view
        binding.rvList.adapter = rvAdapter
    // add items to the list manually in our case
    private fun loadLanguage() {
        languageList = listOf(
            Language("Java" , "Exp : 3 years"),
            Language("Kotlin" , "Exp : 2 years"),
            Language("Python" , "Exp : 4 years"),
            Language("JavaScript" , "Exp : 6 years"),
            Language("PHP" , "Exp : 1 years"),
            Language("CPP" , "Exp : 8 years"),
    // on destroy of view make
      // the binding reference to null
    override fun onDestroy() {
        _binding = null


My Personal Notes arrow_drop_up
Last Updated : 30 Nov, 2021
Like Article
Save Article
Similar Reads