Android RecyclerView with Kotlin – Part 3

In the previous tutorial, we created our data class for a User. We can now use that Model to create a lot of users. In this example, we use a for loop to create 101 users. (Remember, counting starts at zero.) This Int can be any number, though. Let’s create our sample data.

If you run the app, you will see that the RecyclerView works! You can scroll up and down the list of users. If you scroll up and down, you will notice that there is a large gap between each row.

The large gap between the top and bottom rows. (Scroll the emulator to see in action.)

To fix that, we need to open row_user.xml and set the parent’s layout_height to wrap_content.

Set the layout_height to wrap_content.

Run the app again and you should see a long list of Welcome Daniel Malone!

Let’s remove the “Welcome, Daniel Malone” text and show only the first name. Modify the row_user.xml file to add an id of firstName. Also, we change the android:text to tools:text, then import the tools by clicking on tools and pressing Alt+Enter on Windows (or Option+Enter on Mac). With these two modifications, we have the following file.

With our android:id set, let’s create a connection between the row_user.xml layout file and the UsersAdapter class. With UsersAdapter.kt open, make the following modifications.

Rename the onBindViewHolder parameters to something more meaningful. Also, create a link between the firstName android:id and the UsersAdapter.kt file.

Inside of onBindViewHolder, add the following line of code. This line gets the users variable passed in from MainActivity.

Set the firstName based upon the user’s position variable. Use the firstName method on the User object to display the firstName according to the MainActivity class.

Run the app again, and we will see “Daniel” written on the screen 101 times.

The Android Emulator.

If we want to confirm that each row is a individual, unique record, open MainActivity and add the i variable to the firstName by using $i.

Use the i variable for testing.
The Android Emulator.

We now have a working RecyclerView! In the next part of this series, we’ll style each row.

Daniel Malone Daniel Malone
Bringing more than a decade of software engineering experience, Daniel Malone is Editor at androidEveryday. An Austin native, Daniel is often found reading technical books, blogging and creating YouTube tutorials. When not working, he likes to listen to pop hits on Google Play Music.

Android RecyclerView Tutorial (Part 1)

  • Most apps contain lists of data using RecyclerView.
  • In this tutorial, use ConstraintLayout and Kotlin to display a list of data.

3 years ago

findViewById() in Kotlin

  • As you may have discovered, findViewByid() is no longer needed.
  • Kotlin adds the ability to directly access Views.

3 years ago

Android Architecture Components Tutorial: ViewModel + LiveData

  • Use Kotlin to build a basic Android app using Android Architecture Components.
  • As part of Jetpack, LiveData and ViewModel support a MVVM app architecture.

3 years ago

Android SharedPreferences Tutorial in Kotlin

  • Use SharedPreferences to store simple data in Android Studio projects.
  • It's good for storing small amounts of data

    3 years ago

Cleaner Architecture for Android Apps

  • Clean Architecture for Android can help developers prepare for change.
  • But app developers can't possibly know what will change.

    3 years ago

Android RecyclerView with Kotlin – Part 2

  • Create the entire RecylerView for our UsersAdapter.
  • Continued from part 1, where we set up the Android Studio project.

3 years ago

Android and RxJava: Using a Single

  • Use RxJava's Single class when the expected type is either a success or error.
  • It's only slightly different from Observable.just(), which we discussed in the last article.

3 years ago

Android and RxJava: Getting Started

  • The powerful library RxJava can help developers build better apps.
  • See how to use Observables and Observers in this tutorial.

3 years ago

Android RecyclerView with Kotlin - Part 1

  • Oftentimes, developers need to display a list of some sort of data.
  • Whatever type of data is displayed, you should use a RecyclerView.

3 years ago

Display Sample Data when Creating a RecyclerView

  • Android Studio's default RecyclerView preview lacks sample data.
  • Adding sample data can be helpful for debugging.

3 years ago

Android Studio Tutorial with Kotlin (2018 Edition) - Part 2

  • Create a UI in Android Studio using the Design mode.
  • In this latest installment for beginners, we'll add a widget to our screen.

3 years ago