RecyclerView with Grid Layout in Kotlin

Pasindu Laksara
2 min readApr 2, 2021

Let’s discuss how to use a RecyclerView As GridView. The display of elements in grids is a very common pattern in mobile applications now. The user sees a collection of items and can scroll through them. Such an activity is depicted in the picture below

image source https://uxmovement.com/mobile/list-vs-grid-view-when-to-use-which-on-mobile/

(1). First, add these dependencies into the Build.gradle file

implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'

in this tutorial I use card view so that’s why I add card dependency as well

(2). Add RecyclerView to the activity_main

Be sure to give an id name for the RecyclerView

(3). Create a custom row layout XML file to visualize the item

Created layout

(4). Define a model class to use as the data source

In my case, I used three data image, title, and description

Create Kotlin class and make the changes like this👆

(5). Initiate Adapter

The adapter is populating the data into the RecyclerView and convert an object at a position into a list row item to be inserted

(6)The last part Main Activity

Final result

--

--