Android Volley and RecyclerView tutorial

In this RecyclerView tutorial we’ll be creating an Android application which fetch popular movies from an API provided by TMDB. You can jump straight to the code by clicking¬†here.

I am writing this article after having taken an Android developer course over at Edureka. It is a great course, which I highly recommend. It was even recently certified by Google itself.. ūüôā

Prior to the RecyclerView in Android, one almost always used the ListView.
The Android RecyclerView is an improved and more flexible version of the ListView, and comes with greater performance, which has proven to be great for large data sets.

As a rule of thumb, whenever you want to present large data sets or data that frequently changes through a list, you should make use of the RecyclerView.

What is the RecyclerView in Android?

Referencing the Google Developer documentation, the RecyclerView is “a¬†flexible view for providing a limited window into a large data set.”
Translated to plain english, this simply means that the RecyclerView is a view you would use whenever you want to present data in a list.

What makes the RecyclerView better for performance than its predecessor (the ListView), is in the way that the elements in the list is rendered. We’ll be illustrating this with an example where you have a list containing 50 objects. Keep in mind that all the elements in a RecyclerView is connected to a¬†ViewHolder. We’ll elaborate on this later!

Now, assume that the user scrolls down through the list. When the list is scrolled, the views that move off screen (outside of the layout-container), will automatically be saved. If our user wants to scroll back up, the previously saved elements can be used, instead of instantiating new ViewHolders.

The RecyclerView will automatically save elements which has been scrolled of screen, so that they can be reused later.

One other feature of the RecyclerView in Android which makes it really powerful,  is the reuse of ViewHolders. If we assume that our user keeps scrolling through the list (and not going back up), the previously instantiated ViewHolders will be reused when presenting new elements.

The RecyclerView will reuse it’s ViewHolders when the user scrolls down through the list. This minimizes the amount of memory needed to present large data sets.

Now that we have a general understanding of how the RecyclerView works, let’s go about implementing it.

We’ll implement the RecyclerView in a sample application which shows a list of movies, which are fetched through the TMDB API.

You can apply for an API key by clicking the “API” link from the left hand sidebar within your account settings page. You need to have a legitimate business name, address, phone number and description to apply for an API key.
If you don’t have a business name, simply fill in your initials. I did this, and my application got accepted right away.

Using Volley and RecyclerView in Android

The first thing we’ll do is to create an empty Android Studio Project. Fire up Android Studio, and do the following:

File –> New –> New Project

After this, you can simply enter the same configuration as described in the images below.

If you're serious about Android development, I highly recommend the Google-certified Android App Development Certification Course

Use the coupon code LIMITED25 to get 25% off ūüėČ
Creating a new Android Project for a RecyclerView

Setting project and artifact name

RecyclerView project example in Android

Setting minimum SDK to Android 23

Choosing setup for a RecyclerView in Android

Initializing our application with an empty activity

Creating android recyclerview example

Autogenerating a MainActivity.java and corresponding layout

Now, hit¬†finish,¬†and you’re ready to start developing a real life android application containing a RecyclerView.

The first thing we’ll do is to create packages, to make our source code organized.¬†The packages we’ll need are the following:

  • model:¬†This is where we will be placing all classes which represents entities from the API (like a movie).
  • loader:¬†This package will contain classes responsible for fetching and reading data from the API.
  • activities:¬†Package containing all activities.
  • adapter:¬†This is the package where we place the adapter for our RecyclerView.

After creating the packages and placing MainActivity under the activities package, your project structure should look like this:

Project Structure of the RecyclerView example application

First of all, we need to be able to connect to the Internet through the application. To be able to do so, add this to your AndroidManifest.xml:

Our application will also need some extra added dependencies. Simple add the following dependencies to your build.gradle file:

Before we dive into the code of the RecyclerView, we create the other necessary classes. If you’re only interested in the RecyclerView, you can simply copy / paste everything outside of the adapter package.

We’ll start off by creating a¬†Movie¬†class in the¬†model¬†package.

Now that we’ve created a class representing a movie, we need a way to download the movies from the API. We’ll do this by using the Volley HTTP library. We will not go into the details on how to use Volley. However, if you want to learn how to use it, you can read more about it in my Android Volley HTTP library tutorial.

Let’s create the class¬†TmdbLoader, which will download popular movies from the API, and place them in an ArrayList. The TmdbLoader class will also contain a local interface, which will be used to pass the downloaded movies back to the calling activity. Note that the documentation for the TMDB API can be found here.

Now that we’ve created the “back-end” of the application, let’s implement the¬†RecyclerView (finally!).

First of all, our application will simply display the movie list in the MainActivity¬†on startup. Let’s head over to res/activity_main.xml, and include a RecyclerView in the layout.

When creating a custom RecyclerView, one also has to provide a custom layout. Let’s add a custom layout to our project,¬†res/recycler_movie_list_item.xml.¬†This layout defines how each row in the RecyclerView will be designed.

We’re almost there! The next thing to do is to create a custom Adapter for the RecyclerView. The Adapter will tell which layout to use (recycler_movie_list_item.xml), and will also contain an inner class – which will be the ViewHolder.

That’s pretty much it. The inner class¬†RecyclerListHolder simply contains the Views in the layout we’ve created. The method #onBindViewHolder() binds the layout elements to the actual values in the Movie-objects.

The last thing we’ll need to do to get the application up and running is to update the¬†MainActivity.java. In the activity, we’ll bind the RecyclerView in¬†activity_main.xml¬†to our custom¬†MovieRecyclerAdapter, and download the movies with the¬†TmdbLoader¬†class.

Again, if you wish to take a deep dive into Android programming, I recommend checking out the Android developer course hosted by Edureka. If you don’t feel like paying for a course, you can always head over to Google and go through their documentation, too!

That’s it!¬†You now have a fully working application which shows the 20 most popular movies on TheMovieDB.org !

Leave a Reply

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