Skip to main content

How to create a basic RecyclerView in Kotlin Android

How to create a basic RecyclerView in Kotlin Android.

Here's a step-by-step tutorial on how to create a basic RecyclerView in Kotlin for Android:

Step 1: Set up your project

  • Open Android Studio and create a new project.
  • Choose an empty activity template and give your project a name.
  • Select the language as Kotlin and the minimum SDK version as per your requirements.

Step 2: Add RecyclerView dependency

  • Open the build.gradle(Module:app) file.
  • In the dependencies block, add the following line: implementation 'androidx.recyclerview:recyclerview:1.2.1'
  • Sync your project to download the RecyclerView library.

Step 3: Create a layout for the RecyclerView item

  • Right-click on the res directory in the Project panel and choose New -> Layout Resource File.
  • Give it a name, for example, item_layout.xml.
  • In the layout file, design the UI for a single item in the RecyclerView. This can include text views, image views, buttons, etc.

Step 4: Create the RecyclerView layout

  • Right-click on the res directory and choose New -> Layout Resource File.
  • Give it a name, for example, activity_main.xml.
  • In the layout file, add a RecyclerView element like this:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

Step 5: Create a data model

  • Create a new Kotlin class file, for example, DataModel.kt.
  • Define the properties you want to display in each item of the RecyclerView.
data class DataModel(val name: String, val age: Int)

Step 6: Create a ViewHolder

  • Create a new Kotlin class file, for example, DataViewHolder.kt.
  • Extend the RecyclerView.ViewHolder class and pass the item layout file to it.
class DataViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
// Bind the views from item_layout.xml to variables here
val nameTextView: TextView = itemView.findViewById(R.id.nameTextView)
val ageTextView: TextView = itemView.findViewById(R.id.ageTextView)
}

Step 7: Create an Adapter

  • Create a new Kotlin class file, for example, DataAdapter.kt.
  • Extend the RecyclerView.Adapter class and pass the DataViewHolder class as a type parameter.
  • Implement the required methods: onCreateViewHolder, onBindViewHolder, and getItemCount.
class DataAdapter(private val data: List<DataModel>) : RecyclerView.Adapter<DataViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DataViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
return DataViewHolder(itemView)
}

override fun onBindViewHolder(holder: DataViewHolder, position: Int) {
val currentItem = data[position]
holder.nameTextView.text = currentItem.name
holder.ageTextView.text = currentItem.age.toString()
}

override fun getItemCount() = data.size
}

Step 8: Set up the RecyclerView in the MainActivity

  • Open the MainActivity file and find the onCreate method.
  • Initialize the RecyclerView and set its layout manager and adapter.
class MainActivity : AppCompatActivity() {

private lateinit var recyclerView: RecyclerView
private lateinit var dataAdapter: DataAdapter

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

recyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)

val data = listOf(
DataModel("John Doe", 25),
DataModel("Jane Smith", 30),
DataModel("Bob Johnson", 35)
)

dataAdapter = DataAdapter(data)
recyclerView.adapter = dataAdapter
}
}

That's it! You have successfully created a basic RecyclerView in Kotlin for Android. Run your app to see the list of items displayed in the RecyclerView. You can customize the layout and functionality of the RecyclerView as per your requirements.