Android RecyclerView Example Kotlin

Android RecyclerView Example Kotlin | Working Example

RecyclerView is a more flexible version of a ListView with more advance features. There are several components that work together to display the data. If you have a large set of data or data that changes frequently then RecyclerView is the best option to use. So, in this Android RecyclerView Example in Kotlin we will create a RecyclerView in Android Studio using Kotlin Language.

RecyclerView reuses the views when a user scrolls the screen up or down and does not create new views from the start. Only those views are loaded that are currently being displayed on the screen. So in this way RecyclerView saves memory and the application does not become heavier in size.


Android RecyclerView Example Kotlin
Android RecyclerView Example Kotlin

Android RecyclerView Example in Kotlin

So open your Android Studio and create a new Android Project. Name it anything, here in my case the project name is “KotlinRecyclerView”. Select Kotlin as your default language.

Android RecyclerView Example Kotlin
Android RecyclerView Example Kotlin

After that first come to your build.gradle (Module: app) file and add this dependency and click Sync Now on the top right corner.


Now come to the activity_main.xml file and change the default Layout from Constraint Layout to Relative Layout. Remove the default Hello World TextView and then add a RecyclerView with match_parent width and height. 


The final activity_main.xml file will be like this





For designing the look of each row we will make use of a separate XML file. Create a new XML file and name it as row_data. Change the default Layout to Relative Layout. Change height of the Relative Layout to wrap_content and then add an ImageView and TextView with wrap_content width and height. 





The final row_data.xml file will be like this





In our MainActivity.kt file we will make an ArrayList of type String to add some items in our list. So, we add the items in a separate function. We will use LinearLayout Manager to position the views. 



Since, we have not created an Adapter that’s why we will face an error in MainActivity.kt for now.


Android RecyclerView Example In Kotlin: Adapter.kt

Now we create an Adapter class to load data into the views. Right click on your Package name and click New then click Kotlin File/Class and make a new Kotlin class. In my case the name of Adapter class is Adapter.kt. It will take an ArrayList<String> as a parameter. Also, Adapter needs a ViewHolder class to hold the views.



To remover the red line error press ALT+ENTER or click the bulb icon on the left and then we have to override all the three functions.

  • getItemCount
  • onCreateViewHolder
  • onBindViewHolder

getItemCount returns the number of items in the list. onCreateViewHolder inflates our row_data.xml and in onBindViewHolder we are binding the items of the list with TextViews.

So to provide a beautiful look to RecyclerView you can add separators or divider lines after each view. For doing this add this code.


Finally, our complete MainActivity.kt will be like this



So, I hope you understand this Android RecyclerView Example in Kotlin. If you want to find out more details about RecyclerView then check RecyclerView Databinding Kotlin.



Android RecyclerView Example Kotlin
Android RecyclerView Example Kotlin


Android RecyclerView Example Kotlin


More Android Posts

Android Custom Toolbar In Kotlin

Check Internet Availability In Android Programmatically

Kotlin VS Java Which one is Better?

Python Vs Javascript


Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!