Display items in an Android RecyclerView from RAW JSON file locally
By: Animesh Roy
Tags: Raw, Parse, Android, Recyclerview, Json

First, start creating a new Android Studio project. After that create a raw directory by Res > New > Directory > Give a name ‘raw’ > Ok

Now create a JSON file name holidays.json and paste it in the JSON file:

[{

"name": "New Year Day",

"date": "1st Jan Wednesday"

}, {

"name": "Guru Govind Singh Jayanti",

"date": "2nd Jan Thursday"

}, {

"name": "Lohri",

"date": "14 Jan Tuesday"

}, {

"name": "Makar Sankranti",

"date": "15 Jan Wednesday"

}, {

"name": "Chinese New Year",

"date": "25 Jan Saturday"

}, {

"name": "Republic Day",

"date": "26 Jan Sunday"

}, {

"name": "Shivaji Jayanti",

"date": "19 Feb Wednesday"

}, {

"name": "Maha Shivaratri/Shivaratri",

"date": "21 Feb Friday"

}, {

"name": "Holi",

"date": "10 Mar Tuesday"

}, {

"name": "March Equinox",

"date": "20 Mar Friday"

}, {

"name": "Rama Navami",

"date": "2 Apr Thursday"

}, {

"name": "Mahavir Jayanti",

"date": "6 Apr Monday"

}, {

"name": "Good Friday",

"date": "10 Apr Friday"

}, {

"name": "Easter Day",

"date": "12 Apr Sunday"

}, {

"name": "Vaisakhi",

"date": "13 Apr Monday"

}, {

"name": "Buddha Purnima/Vesak",

"date": "7 May Thursday"

}, {

"name": "Father's Day",

"date": "21 Jun Sunday"

}, {

"name": "Independence Day",

"date": "15 Aug Saturday"

}, {

"name": "Parsi New Year",

"date": "16 Aug Sunday"

}, {

"name": "Halloween",

"date": "31 Oct Saturday"

}, {

"name": "Diwali/Deepavali",

"date": "14 Nov Saturday"

}, {

"name": "Hanukkah",

"date": "11 Dec Friday"

}]

We will use CardView and RecyclerView. So add the dependency in app-level build.grdle(Module: app):

implementation 'com.android.support:cardview-v7:28.0.0'

implementation 'com.android.support:recyclerview-v7:28.0.0'

Create a list_item.xml (Layout > New > Layout Resource File) layout for RecyclerView:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

xmlns:app="http://schemas.android.com/apk/res-auto"

app:cardCornerRadius="8dp"

android:padding="8dp"

android:layout_margin="5dp"

android:layout_height="wrap_content">



<LinearLayout

android:orientation="vertical"

android:padding="10dp"

android:layout_width="match_parent"

android:layout_height="wrap_content">



<TextView

android:layout_width="match_parent"

android:text="Holiday Name"

android:textStyle="bold"

android:id="@+id/name"

android:textSize="28sp"

android:textColor="@android:color/black"

android:layout_height="wrap_content"/>



<TextView

android:layout_width="match_parent"

android:text="Holiday Date"

android:textSize="23sp"

android:id="@+id/date"

android:layout_height="wrap_content"/>



</LinearLayout>



</android.support.v7.widget.CardView>

Add recycler view in MainActivity.xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">



<android.support.v7.widget.RecyclerView

android:id="@+id/my_recycler_view"

android:scrollbars="vertical"

tools:listitem="@layout/list_item"

android:layout_width="match_parent"

android:layout_height="match_parent"/>



</android.support.constraint.ConstraintLayout>

Now create a Java file Holidays.java:

package com.animesh.roy.exportjson;



public class Holidays {



private final String name;

private final String date;



public Holidays(String name, String date) {

this.name = name;

this.date = date;

}



public String getName() {

return name;

}



public String getDate() {

return date;

}

}

Create another class RecyclerAdapter.java for displaying items:

package com.animesh.roy.exportjson;



import android.content.Context;

import android.support.annotation.NonNull;

import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;



import java.util.List;



public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {



private static final int TYPE = 1;

private final Context context;

private final List<Object> listRecyclerItem;



public RecyclerAdapter(Context context, List<Object> listRecyclerItem) {

this.context = context;

this.listRecyclerItem = listRecyclerItem;

}



public class ItemViewHolder extends RecyclerView.ViewHolder {



private TextView name;

private TextView date;



public ItemViewHolder(@NonNull View itemView) {

super(itemView);

name = (TextView) itemView.findViewById(R.id.name);

date = (TextView) itemView.findViewById(R.id.date);

}

}



@NonNull

@Override

public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

switch (i) {

case TYPE:



default:



View layoutView = LayoutInflater.from(viewGroup.getContext()).inflate(

R.layout.list_item, viewGroup, false);



return new ItemViewHolder((layoutView));

}



}



@Override

public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {



int viewType = getItemViewType(i);



switch (viewType) {

case TYPE:

default:



ItemViewHolder itemViewHolder = (ItemViewHolder) viewHolder;

Holidays holidays = (Holidays) listRecyclerItem.get(i);



itemViewHolder.name.setText(holidays.getName());

itemViewHolder.date.setText(holidays.getDate());

}



}



@Override

public int getItemCount() {

return listRecyclerItem.size();

}

}

Finally, make some changes in MainActivity.java:

package com.animesh.roy.exportjson;



import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.support.v7.widget.LinearLayoutManager;

import android.support.v7.widget.RecyclerView;

import android.util.Log;



import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;



import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;



public class MainActivity extends AppCompatActivity {



private RecyclerView mRecyclerView;

private List<Object> viewItems = new ArrayList<>();



private RecyclerView.Adapter mAdapter;

private RecyclerView.LayoutManager layoutManager;



private static final String TAG = "MainActivity";



@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);



mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);





// use this setting to improve performance if you know that changes

// in content do not change the layout size of the RecyclerView

mRecyclerView.setHasFixedSize(true);



// use a linear layout manager

layoutManager = new LinearLayoutManager(this);

mRecyclerView.setLayoutManager(layoutManager);



// specify an adapter (see also next example)

mAdapter = new RecyclerAdapter(this, viewItems);

mRecyclerView.setAdapter(mAdapter);



addItemsFromJSON();



}



private void addItemsFromJSON() {

try {



String jsonDataString = readJSONDataFromFile();

JSONArray jsonArray = new JSONArray(jsonDataString);



for (int i=0; i<jsonArray.length(); ++i) {



JSONObject itemObj = jsonArray.getJSONObject(i);



String name = itemObj.getString("name");

String date = itemObj.getString("date");



Holidays holidays = new Holidays(name, date);

viewItems.add(holidays);

}



} catch (JSONException | IOException e) {

Log.d(TAG, "addItemsFromJSON: ", e);

}

}



private String readJSONDataFromFile() throws IOException{



InputStream inputStream = null;

StringBuilder builder = new StringBuilder();



try {



String jsonString = null;

inputStream = getResources().openRawResource(R.raw.holidays);

BufferedReader bufferedReader = new BufferedReader(

new InputStreamReader(inputStream, "UTF-8"));



while ((jsonString = bufferedReader.readLine()) != null) {

builder.append(jsonString);

}



} finally {

if (inputStream != null) {

inputStream.close();

}

}

return new String(builder);

}

}

That’s it. After running the app you should have successfully parsed the Raw JSON file in RecyclerView.

Source Code: https://github.com/animeshroydev/ParseJSONRecyclerView

Output: