Android RecyclerView Using Kotlin Example

Hello guys, Welcome to another tutorial. In this tutorial we are going to learn how to implement android recyclerview Using Kotlin which is now the the official language for android. We are going to create a RecyclerView that will show a list of fruit names, fruit images, fruit description which in this case description with be fruit scientific name. To learn more about kotlin click here

Android RecyclerView Using Kotlin

    • Let’s get started with our project.

Creating a new kotlin project

    • To create a new kotlin project, Open your android studio click File => New Project => provide a name in this tutorial i named the project recyclerkotlin => Tick the checkbox below which says include kotlin support.

Starting a new kotlin project

    • Continue by clicking next => Empty Activity and Finish.

Adding RecyclerView Support to your project

    • RecyclerView is not added by default so you will need to add it to our project.
    • Click File => Project Structure => app => Dependencies then add RecyclerView as shown in the image below.

Adding RecyclerView to project

Adding RecyclerView To our Activity

    • Open your activity_main.
    • Copy and Paste the code below to include RecyclerView.

Adding fruits to drawable folder

    • Download fruit images by clicking the link below.
    • Unzip copy and paste the image in the drawable folder.

download fruits

Creating Resource Layout file to hold RecyclerView data

    • Right click layout folder which is under res folder.
    • Select New => Layout Resource File and name it row_data.
    • This is the xml file which will hold the RecyclerView data.

RecyclerView Row_Data

Creating Model kotlin File/Class

    • Create a new kotlin File/Class and name it Fruit, This class will be used for storing and getting our RecyclerView Items.

New Kotlin Project

    • Copy and Paste the code below in Fruit Class.

Creating RecyclerVeiw Adapter

    • Create a new Kotlin File/Class and name it FruitAdapter.
    • This class extends RecyclerView Adapter.
    • It manages and displays our data.
    • Copy and Paste the code below in Your FruitAdapter class.

Setting FruitAdapter to our RecyclerView

    • Open your MainActivity class.
    • Copy and Paste the code below in your file.
    • The code will set our FruitAdapter to MainActivity.

    • Try and run your app, You will get the output similar to one shown below.

Android RecyclerView Using Kotlin

  • Thats all, If need help or having a question please comment below.