Android Retrofit, RecyclerView, SearchView Usage

Umit Kose
Huawei Developers
Published in
4 min readDec 30, 2020

Hello friends,
Today, I will do an application that describes Retrofit, RecyclerView, SearchView usage together.

What gonna we learn in this post?

  • We will learn the use of rest api with Retrofit.
  • We will list the data from the service in the RecyclerView.
  • Usage the android component SearchView.
  • In RecyclerView, we’ll see how we can solve click events with the help of the Interface.
  • We will see how to access the data we want by searching with the getFilter function within the listed data.
  • we will use this method within our Adapter classLet’s start building our application now,
    As we always do, let’s add the libraries that we will use in our project to build.gradle.

In the layout section of our MainActivity class, let’s create the RecyclerView structure.

activity_main.xml

Then I create my own Model structure based on the data from the rest api.

I have created User and Links model.

json output:

User.java

In order to access the data, I created a Constant class for the required main url for the rest api.

Now, in order to use the Retrofit structure, let’s create the RetrofitClient class.

Now we have created our interface class and let’s examine this code in detail; With the @GET method, we have indicated that we will request a GET and (“users”) parameter in addition to this method. Thus, we will draw the data with the GET method as follows:

BASE_URL: https://jsonplaceholder.typicode.com/

URL we will access with the GET method:

https://jsonplaceholder.typicode.com/posts

With the getUsers method, we see that the User type returns a list of data. Retrofit also creates the Call <List <User >> form and the structure of the data from the service with the structure of Call is indicated on the Android part.

RetrofitInterface.java

RecyclerView Click Event

As you know, there is no method like SetOnItemClickListener in RecyclerView as in ListView, so we need to perform some operations to detect this by clicking on each iteration on our list. That’s why we’re creating an interface called CustomItemClickListener.

CustomItemClickListener.java

For RecyclerView, we need a adapter structure where we are going to position the data.

CustomAdapter.java

Building into the identity and culture of an agency can also lead to new client work. These projects act as a road map, showing clients exciting new technologies and ideas that will differentiate you from competitors. One of our earliest projects turned our website into a brochure, optimized for the first iPad’s touch interactions. By demonstrating the final product, we went on to win a project to create a similar product for a new client.

In Android Studio, we create menu structure under res folder. Under the menu structure, we created the xml file which we call menu_item and we define the SearchView component.

menu_item.xml

Finally our MainActivity will look like that:

We have coded the Retrofit classes that we have created as follows, and if there is no problem when requesting the service, the onResponse method is triggered. Response.body () also returns the list in User type of service. We do not parse the incoming json type structure into the related models. With this structure we have created, Retrofit is doing this process and returning the data we want to us. OnFailure method is triggered if there are any errors. OnCreateOptionsMenu you will see that we define the SearchView component. The setOnQueryTextListener method triggers when there is any input change at the top. OnQueryTextChange is the method that runs when the input value is changed. onQueryTextSubmit is the method that runs when the call key is pressed after the entered value.

OnQueryTextChange also sends the value entered in the filter function found in the getFilter method.

MainActivity.java

Finally do not forget to give Internet Permission on AndroidManifest.xml

After running our application, our screen output is as follows:

When we type any input:

In this lesson, we examined RecyclerView, SearchView, Retrofit Usage together. I tried to explain this application from the beginning detailed. I hope it helps everyone.

References:

https://developer.android.com/guide/topics/ui/layout/recyclerview

--

--