Gmail Like Inbox By JSON Parsing Using RecyclerView CardView and Volley

Gmail Like Inbox By JSON Parsing Using RecyclerView CardView and Volley

Hello guys, today’s tutorial is about fetching data from JSON file and display it with RecyclerView CardView and to perform Network Request we will use volley.

RecyclerView – RecyclerView got introduced in android Lollipop. Basically The RecyclerView widget is a more advanced and flexible version of ListView. When we need to display a scrolling list of elements based on large data sets we use Recyclerview. As the name suggests, anrdoid RecyclerView is used to reuse cells when scrolling up and down by recycling the items in the list.

CardView – Android CardView UI component shows information inside cards. This component is generally used to show contact information. In this tutorial we will show some information inside the CardView using RecyclerView.

Volley – Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. By default all the volley network calls works asynchronously, so we don’t have to worry about using asynctask anymore.

So lets start..

Gmail Like Inbox By JSON Parsing Using RecyclerView CardView and Volley

Lets start by creating a new project i am giving it name RecyclerViewJson.

Sample JSON File

  • I have created JSON file which contains data which we want to show on our RecyclerView.
  • In realtime this json should be generated from a database using any server side language.
  • You can see JSON here – http://androidcodefinder.com/RecyclerViewJson.json
  • We will use this in this tutorial.

Json File Screen Shot

Creating Android Project

  • Create a new project in Android Studio File–>New–>New Project.
  • Enter Application Name, Package Name, Select Empty Activity and click on finish.

Adding RecyclerView CardView and Volley

  • Now we will add RecyclerView, CardView and Volley to our project by adding their Dependencies on our project.
  • Add Dependencies on gradle.

  • Now sync your project.

Android Manifest File

  • Open your AndroidManifest.xml file and add Internet permission.

Creating RecyclerView Layouts

RecyclerView

  • Create a RecyclerView inside your activity_main.xml.
  • Give it a id myRecyclerView.
  • We will also add ProgressBar to show progress.

Add Vector Drawable

  • Add vector drawable which we are going to use in our project.
  • Right click on Drawable folder then New–>Vector Asset.
  • Click on Icon then Select Icon and add it to on drawable folder.
  • Search and Select Star Icon which we will use use in our project.

Make Circle Shape TextView

  • Right click on drawable folder–>New–>Drawable resource file.
  • Write File Name (circle_shape.xml) and click Ok.
  • We have to add this file to Background property of TextView.

RecyclerView Item Layout using CardView

  • We need to create separate layout for for items for RecyclerView.
  • For that we will use CardView.
  • So we can create a desired design inside CardView.
  • CardView is not mandatory. CardView gives a nice elevated look that’s why we are using it in our project.
  • In our project we are designing Gmail Inbox like layout.
  • Create a new layout resource file named list_view.xml and use the following xml code.

  • Our layout will look like this –

Gmail RecyclerView

Creating Model Class

  • Here we don’t have a single item for our list, and that is why it can’t be stored in a String array. We need a class to store all the attributes that we have for item in our list.
  • So to store these attributes we need to create a class let’s name it ModelActivity.java.
  • Write the following code.

Creating RecyclerView Adapter

  • To manage and display the data of our RecyclerView we need a class that will extend RecyclerView.Adapter. Inside this class we need RecyclerView.ViewHolder. Now what are these two?
  • RecyclerView.ViewHolder represents the views of our RecyclerView and the RecyclerView.Adapter represents the data that is to be shown with the ViewHoder.
  • We have 3 methods inside RecyclerView.Adapter that we need to override.
    1. RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
      • This method returns a new instance of our ViewHolder.
    2. void onBindViewHolder(ProductViewHolder holder, int position)
      • This method binds the data to the view holder.
      • we have also randomly color each circle in this method.
    3. int getItemCount()
      • This returns the size of the List.
  • So lets create our Adapter. For this create a new class named AdapterActivity.java and write the following code.

Setting Adapter to RecyclerView

  • The last step to show RecyclerView. Come inside MainActivity and write the following code.

RECENT POST

Leave a Reply

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