Post Date: May 16, 2019
Last Updated: October 26, 2026
Introduction
RecyclerView is an Android UI component used to display large datasets efficiently in a scrollable list or grid. It is a more flexible alternative to the older ListView component and supports different layouts, animations, and item decorations.
RecyclerView uses several components to display data:
- RecyclerView: The main container that displays the list or grid.
- Adapter: Connects the dataset to the RecyclerView and creates item views.
- ViewHolder: Holds references to the views used to display an individual item.
- LayoutManager: Controls how items are arranged, such as vertically, horizontally, or in a grid.
- ItemDecoration: Adds visual enhancements, such as spacing or dividers between items.
When an item scrolls off-screen, RecyclerView can reuse its view to display another item. This reduces unnecessary view creation and helps improve performance.
This guide explains how to implement a basic RecyclerView in an Android application using Kotlin and XML layouts.
Prerequisites
Before implementing RecyclerView, ensure you have:
- Android Studio installed.
- A basic understanding of Kotlin and Android development.
- An Android project configured to use XML layouts.
- The AndroidX RecyclerView dependency added to your application.
Reference Article: How to Install Android Studio on Windows and macOS
Implementation
Step 1: Add the RecyclerView Dependency
Open your app-level Gradle file.
For projects using Kotlin DSL (build.gradle.kts), add the following dependency inside the dependencies block:
implementation("androidx.recyclerview:recyclerview:1.4.0")
For projects using Groovy (build.gradle), use:
implementation 'androidx.recyclerview:recyclerview:1.4.0'
Sync your project with Gradle after adding the dependency.
Note: The version shown is an example. Check the official Android documentation for the latest stable version compatible with your project’s Android Gradle Plugin and SDK configuration.
Step 2: Add RecyclerView to the Layout File
Open activity_main.xml and add the RecyclerView component.
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:clipToPadding="false" />
This creates a RecyclerView that occupies the available screen space.
A LayoutManager must be assigned before the RecyclerView can arrange its items.
Step 3: Create a Layout for Individual Items
Create a new layout file named item_name.xml inside the res/layout directory.
This layout defines how each item appears in the list.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#212121" />
</LinearLayout>
The TextView displays the data associated with each item. You can customize this layout to include images, buttons, or additional text fields.
Step 4: Create the RecyclerView Adapter
The adapter connects the dataset to the RecyclerView. It creates item views, stores their view references, and binds data to them.
Create a Kotlin class named NameAdapter.kt.
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class NameAdapter(
private val names: List<String>
) : RecyclerView.Adapter<NameAdapter.NameViewHolder>() {
class NameViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
val nameTextView: TextView =
itemView.findViewById(R.id.txtName)
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): NameViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_name, parent, false)
return NameViewHolder(view)
}
override fun onBindViewHolder(
holder: NameViewHolder,
position: Int
) {
holder.nameTextView.text = names[position]
}
override fun getItemCount(): Int = names.size
}
The adapter includes three essential methods:
onCreateViewHolder()creates the layout for an individual list item.onBindViewHolder()binds the appropriate data to the item at the specified position.getItemCount()returns the total number of items in the dataset.
The NameViewHolder class stores references to the views used by each item.
Step 5: Configure RecyclerView in the Activity
Open MainActivity.kt and initialize the RecyclerView with a LayoutManager and the adapter.
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recyclerView =
findViewById<RecyclerView>(R.id.recyclerView)
val names = listOf(
"Arun",
"Priya",
"Kumar",
"Meena",
"Ravi"
)
recyclerView.layoutManager =
LinearLayoutManager(this)
recyclerView.adapter = NameAdapter(names)
}
}
In this example:
LinearLayoutManagerarranges the items vertically by default.NameAdapterprovides the names to display.- The RecyclerView displays each name using the
item_name.xmllayout.
Important: This example assumes your project already includes the AndroidX AppCompat dependency. If your project does not use AppCompatActivity, you can use an appropriate activity class supported by your project.
Step 6: Run and Test the Application
Build and run the application in an Android emulator or on a connected device.
Verify that:
- The list of names appears on the screen.
- Each name is displayed in its own item view.
- The list scrolls correctly when the dataset exceeds the visible screen area.
If the RecyclerView does not display any items, verify the layout IDs, adapter initialization, dataset, and LayoutManager configuration.
Step 7: Customize the RecyclerView
RecyclerView supports different layouts and visual enhancements.
Display items in a grid:
recyclerView.layoutManager =
androidx.recyclerview.widget.GridLayoutManager(this, 2)
This arranges items in two columns.
Add spacing or dividers:
You can implement a custom RecyclerView.ItemDecoration to add spacing or dividers between items.
Handle item clicks:
You can define a callback or listener in the adapter to notify the activity or fragment when a user selects an item.
These options allow you to adapt RecyclerView to different application requirements.
Conclusion
RecyclerView provides an efficient and flexible way to display lists and grids in Android applications. By configuring a RecyclerView, creating an item layout, implementing an adapter and ViewHolder, and assigning a LayoutManager, you can build a reusable list component.
You can extend this implementation with click listeners, custom item decorations, multiple view types, and more advanced data-handling techniques as your application grows.
Frequently Asked Questions
1. What is RecyclerView in Android?
RecyclerView is a UI component used to display scrollable collections of items efficiently by reusing item views.
2. What is the purpose of a ViewHolder?
A ViewHolder stores references to the views within an item. This allows the adapter to access and update those views when binding data.
3. What is the difference between RecyclerView and ListView?
RecyclerView provides a more flexible architecture with dedicated adapters, ViewHolders, LayoutManagers, item decorations, and animation support.
4. Why is a LayoutManager required?
The LayoutManager determines how items are positioned and arranged within the RecyclerView. Common options include LinearLayoutManager, GridLayoutManager, and StaggeredGridLayoutManager.
5. Can RecyclerView display different types of items?
Yes. You can override getItemViewType() and implement the appropriate ViewHolder and binding logic to display different item layouts.
Related Articles
- How to Create First project in Android Studio.
- How to Import a Project from GitHub into Android Studio
- How to Create a Project and an Overflow Menu in Android Studio
Need Assistance?
If you need assistance implementing RecyclerView or troubleshooting your Android application, please contact the Pheonix Solutions support team. Include a description of the issue and any relevant error messages or code snippets so our team can investigate and assist you.
For further assistance, please contact our support team.