How to Add Dividers in Android RecyclerView?

In the article, we will learn how to implement Recyclerview in an android application. Recyclerview is the most common use in every application. 

Display data in the application while using recyclerview also recyclerview provides horizontal and vertical views of the application.  

Recyclerview also provides item decoration on the mobile application. Let's discuss an example of recyclerview.



Here is the step by step Implementation process:


Step 1: Create a new project in Android  or you can use an already created project.  


Step 2: Add dependency build.gradle file on Gradle scripts>Build.gradle app module and add the below dependencies

 implementation “androidx.recyclerview:recyclerview:1.3.2”

Step 3: Add permission to the internet  in app> Androidmanifest.xml file and below code.(optional).

 <uses-permission android:name="android.permission.INTERNET" />

Step 4: Create a RecyclerViewData.kt file in your package folder .

 implement the same invoke the following code inside Recyclerviewdata.kt file.

  data class RecyclerViewData(
val text1: String,
val text2: String
  )
Step 5: Create a MyRecyclerviewadapter.kt file in your package folder .

 implement the same invoke the following code inside MyRecyclerviewadapter.kt file.


 import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class MyRecyclerViewAdapter(private val items: List<RecyclerViewData>) :
RecyclerView.Adapter<MyRecyclerViewAdapter.MyRecyclerViewDataHolder>() {

inner class MyRecyclerViewDataHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyRecyclerViewDataHolder {
val view: View =
LayoutInflater.from(parent.context).inflate(R.layout.recycler_data_view, parent, false)
return MyRecyclerViewDataHolder(view)
}

override fun onBindViewHolder(holder: MyRecyclerViewDataHolder, position: Int) {
val currentItem: RecyclerViewData = items[position]

val tvNumber: TextView = holder.itemView.findViewById(R.id.tvNumber)
tvNumber.text = currentItem.text1

val tvNumbersInText: TextView = holder.itemView.findViewById(R.id.tvNumbersInText)
tvNumbersInText.text = currentItem.text2
}

override fun getItemCount(): Int {
return items.size
}
}
Step 6: Create an activity_main.xml file in your layout folder .

 implement the same invoke the following code inside activity_main.xml file.


 <?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:id="@+id/idRLContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="80dp"
android:text="Candidroot solutions"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.842"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtTitle" />


 </androidx.constraintlayout.widget.ConstraintLayout>
Step 7: Create an main_activity.kt file in your package folder.
 import RecyclerViewItemDecoration
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivityone : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_activityone)

val recyclerView: RecyclerView = findViewById(R.id.recyclerView)

// create list of RecyclerViewData
var recyclerViewData = listOf<RecyclerViewData>()
recyclerViewData += RecyclerViewData("1", "Tiger")
recyclerViewData += RecyclerViewData("2", "Panther")
recyclerViewData += RecyclerViewData("3", "Hyena")
recyclerViewData += RecyclerViewData("4", "Gorilla")
recyclerViewData += RecyclerViewData("5", "Wild Boar")
recyclerViewData += RecyclerViewData("6", "Crocodile")
recyclerViewData += RecyclerViewData("7", "Porcupine")
recyclerViewData += RecyclerViewData("8", "Otter")
recyclerViewData += RecyclerViewData("9", "Squirrel")
recyclerViewData += RecyclerViewData("10", "Mole")
recyclerViewData += RecyclerViewData("11", "Oryx")
recyclerViewData += RecyclerViewData("12", "Monkey")
recyclerViewData += RecyclerViewData("13", "Panda")
recyclerViewData += RecyclerViewData("14", "Possum")
recyclerViewData += RecyclerViewData("15", "Monitor Lizard")

// create a vertical layout manager
val layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)

// MyRecyclerViewAdapter
val myRecyclerViewAdapter = MyRecyclerViewAdapter(recyclerViewData)

// adapter to the recycler view
recyclerView.adapter = myRecyclerViewAdapter

recyclerView.layoutManager = layoutManager

recyclerView.addItemDecoration(RecyclerViewItemDecoration(this, R.drawable.divider))

// for the recycler view
myRecyclerViewAdapter.notifyDataSetChanged()
}
}

 

Step 8: Output of above example



Happy coding!


















365Bloggy May 23, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive