RecyclerView with CardView using Kotlin in Android

RecyclerView is superior widget than ListView with advanced layout management like LinerLayoutManager, GridLayoutManager and StaggeredGridLayoutManager. In this tutorial I will give you detail how you can create Recyclerview with CardView using Kotlin in Android.

You have to prepare the following steps.

  1. Declare RecyclerView Main Layout
  2. Declare or Prepare RecyclerView’s item layout with CardView
  3. Prepare RecyclerView Adapter
  4. Prepare Data/Model class for the RecyclerView as Album in our tutorial.
  5. Create the MainActivity attach the adapter to recyclerView and set the layout.

1: Declare RecyclerView Main Layout

You have to create main xml layout in which you need to add the RecyclerView widget.

Now you have to follow the next step we already listed above.

2: Prepare RecyclerView’s item layout

We are going to show 3 parts in the recycler view item.

  • First Image of the Album
  • Title of the Album
  • How many songs are in the album

Now you are about ready to test your kotlin skills in the android. Because XML part is about ready.

3: Prepare RecyclerView Adapter

Now you have to inherit the RecyclerView adapter as the follow.

4: Model class for the RecyclerView

Now it’s time to prepare the data which you want to show in your recyclerview. You have to prepare a model class as the blow Album class.

Now see the real beauty of the Kotlin over the Java.

5: Attach the Adapter to RecyclerView

Now everything is ready. Your adapter, model class, item layout and the main layout is ready. Now you have to attach the adapter to your RecyclerView.

Now your RecyclerView with Card using Kotlin is ready to run in Android. Here are the othe XML resources as the strings, dimens and colors.

colors.xml

dimens.xml

strings.xml

You can download all images from here