Android – RecyclerView using GridLayoutManager with Kotlin
Last Updated :
25 Jul, 2022
RecyclerView is an improved version of List View in which we can add customization to each item of our recycler view according to our requirements. We can change the orientation of our recycler view depending on our requirements. We can create a simple grid layout, vertical and horizontal recycler view, and a staggered grid view. In this article, we will take a look at building a RecyclerView with a GridLayoutManager in Android using Kotlin.
Note: If you are looking to implement Grid Layout Manager in your Recycler View in Android using Java. Check out the following article: RecyclerView using Grid Layout Manager in Android using Java.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
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: Create a Card Layout for Recycler View Card Items
Go to the app > res > layout> right-click > New >Layout Resource File and name the file as course_rv_item. In this file, all XML code related to card items in the RecyclerView is written. Below is the code for the course_rv_item.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< androidx.cardview.widget.CardView
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_gravity = "center"
android:layout_margin = "5dp"
app:cardCornerRadius = "5dp"
app:cardElevation = "4dp" >
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:orientation = "vertical" >
< ImageView
android:id = "@+id/idIVCourse"
android:layout_width = "80dp"
android:layout_height = "80dp"
android:layout_gravity = "center"
android:layout_margin = "8dp"
android:padding = "5dp"
android:src = "@mipmap/ic_launcher" />
< TextView
android:id = "@+id/idTVCourse"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_gravity = "center"
android:layout_margin = "5dp"
android:padding = "4dp"
android:text = "@string/app_name"
android:textAlignment = "center"
android:textColor = "@color/black"
android:textStyle = "bold"
tools:ignore = "RtlCompat" />
</ LinearLayout >
</ androidx.cardview.widget.CardView >
|
Step 3: Create a Java class for Modal Data
Go to the app > java > Right-Click on your app’s package name > New > Kotlin Class and name the file as CourseRVModal. Add the below code to it. Comments are added in the code to get to know in detail.
Kotlin
package com.gtappdevelopers.kotlingfgproject
data class CourseRVModal(
var courseName: String,
var courseImg: Int
)
|
Step 4: Create a new Kotlin class for the Adapter
Similarly, create a new Kotlin Class and name the file as CourseRVAdapter. The adapter is the main class that is responsible for RecyclerView. It holds all methods which are useful in RecyclerView. Adapter class is used to set the data to each item of our recycler view.
Kotlin
package com.gtappdevelopers.kotlingfgproject
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class CourseRVAdapter(
private val courseList: ArrayList<CourseRVModal>,
private val context: Context
) : RecyclerView.Adapter<CourseRVAdapter.CourseViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): CourseRVAdapter.CourseViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(
R.layout.course_rv_item,
parent, false
)
return CourseViewHolder(itemView)
}
override fun onBindViewHolder(holder: CourseRVAdapter.CourseViewHolder, position: Int) {
holder.courseNameTV.text = courseList.get(position).courseName
holder.courseIV.setImageResource(courseList.get(position).courseImg)
}
override fun getItemCount(): Int {
return courseList.size
}
class CourseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val courseNameTV: TextView = itemView.findViewById(R.id.idTVCourse)
val courseIV: ImageView = itemView.findViewById(R.id.idIVCourse)
}
}
|
Step 5: Working with the activity_main.xml file
This is the main screen that displays all data in the form of a grid. Here we have to implement Recycler View. Below is the code snippet of the XML layout in the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout
android:id = "@+id/container"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
tools:context = ".MainActivity" >
< TextView
android:id = "@+id/idTVHeading"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_margin = "8dp"
android:gravity = "center"
android:padding = "4dp"
android:text = "Grid Layout Manager"
android:textAlignment = "center"
android:textColor = "@color/purple_200"
android:textSize = "18sp"
android:textStyle = "bold" />
< androidx.recyclerview.widget.RecyclerView
android:id = "@+id/idRVCourses"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:layout_below = "@id/idTVHeading" />
</ RelativeLayout >
|
Step 6: Working with the MainActivity.kt file
This is the main java file where we will set LayoutManager, adapter, and set data to RecyclerView which is to be displayed in RecyclerView. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.gtappdevelopers.kotlingfgproject
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
lateinit var courseRV: RecyclerView
lateinit var courseRVAdapter: CourseRVAdapter
lateinit var courseList: ArrayList<CourseRVModal>
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
courseRV = findViewById(R.id.idRVCourses)
courseList = ArrayList()
val layoutManager = GridLayoutManager( this , 2 )
courseRV.layoutManager = layoutManager
courseRVAdapter = CourseRVAdapter(courseList, this )
courseRV.adapter = courseRVAdapter
courseList.add(CourseRVModal( "Android Development" , R.drawable.android))
courseList.add(CourseRVModal( "C++ Development" , R.drawable.c))
courseList.add(CourseRVModal( "Java Development" , R.drawable.java))
courseList.add(CourseRVModal( "Python Development" , R.drawable.python))
courseList.add(CourseRVModal( "JavaScript Development" , R.drawable.js))
courseRVAdapter.notifyDataSetChanged()
}
}
|
Now run your application to see the output of it.
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...