Kotlin RecyclerView Example – Building a RecyclerView with Kotlin

Lets learn building a RecyclerView using Kotlin in this Kotlin RecyclerView Example. Learning the small things will help you to migrate to Kotlin. And yes Kotlin will be surely easy and fun once you’ll get used to it. So lets see this Kotlin RecyclerView Example.

Kotlin RecyclerView Example Video

  • You can also go through this video explanation.

Kotlin RecyclerView Example

Creating a New Project

  • So first create a new Android Studio project using Kotlin.
  • Once the project is loaded come inside the activity_main.xml and remove the Hello World TextView.

Adding RecyclerView

  • Now search for RecyclerView and drag it to your activity_main.xml (See the image for help).

Kotlin RecyclerView Example

  • When you will drag RecyclerView it will ask you to add the RecyclerView dependency.

Kotlin RecyclerView Example

  • So the xml code for our activity_main.xml will be.

Creating List Layout using CardView

  • Now for the list items we will create a new layout. So create a new layout resource file and name it list_layout.xml.
  • Now again to the same search CardView and drag it to the layout you just created. It will again ask you to add CardView dependency.

Kotlin RecyclerView Example

  • Now for the layout use the following xml code. Here I am going to display name and address only. But you can also design anything you want for your list.

  • Now thats it for the List Layout. The above xml code will generate the following layout.
Kotlin RecyclerView Example
Kotlin RecyclerView Example
  • Now lets create data class for list model.

Creating Model

  • The list is about user so we will create a User class.
  • Create a new Kotlin file and write the following code.

  • Now lets build the Adapter.

Creating RecyclerView Adapter

  • If you have already created a RecyclerView in java, then you may already know that we need an Adapter to display the data in RecyclerView.
  • So create a new Kotlin class named CustomAdapter.kt and write the following code.

  • Now the last step is creating the RecyclerView itself.

Creating RecyclerView

  • Now come to MainActivity.kt and write the following code.

  • Now thats it for the coding. Lets try running the application.
Kotlin RecyclerView Example
Kotlin RecyclerView Example
  • As you can see it is working absolutely fine.

So thats all for this Kotlin RecyclerView Example friends. If you are having any confusions and queries then lets discuss it on the comment section. And I will keep posting more tutorials using Android Development with Kotlin as well. And for your side I want you to SHARE this post if you think it is useful. Thank You 🙂

14 thoughts on “Kotlin RecyclerView Example – Building a RecyclerView with Kotlin”

  1. Hi Belal,

    Thank you for posting this tutorial for us, I have some doubt for example When we take kotlin file or class i am little bit confused kindly help me to understand

    Thank you

    Reply
  2. I am new to Android development and wanted to experiment with some already written code. I downloaded the code and tried to make it work on Android Studio 3.0 beta 7.

    Can you port the code to work on the recent android studio.

    Reply
  3. am using android studio 2.3.3 am I’ve installed all required kotlin plugins but am not able run your code amd am always getting Caused by: java.lang.ClassNotFoundException:

    Reply
  4. Thank you! I can not find this but with a search engine in the nav bar … would you have something like that? Thank you!

    Reply
  5. Exellent example, the best.
    Any chance for a tutorial converting this one to image and text recyclerview?
    It would be nice to be able to change this to whatever we want,and have it as template.

    Thanks

    Reply
  6. A really good example, I implemented en androidStudio 4.0 with androidx, there are no errors but when I run it on and Samsun grand prime (5.1.1) the screen is show all white.
    The minSdkVersion is 21 (5.0.0)

    Could you help me?

    Thanks in advance

    Luis.

    Reply
  7. how to shift this recyclerview at the bottom of the screen ? i want to do other activity on the top.. like i want to make employee profile page

    Reply

Leave a Comment