01 Step ( Add Library For Dependencies) build.gradle
implementation "androidx.recyclerview:recyclerview:1.2.1" implementation "androidx.cardview:cardview:1.0.0"
02 Step ( Create Drawable File ) \app\src\main\res\drawable\page.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#FFFFFF" /> <stroke android:width="1dp" android:color="#CCCCCC" /></shape>
03 Step (Create an item view layout)\app\src\main\res\layout\item_view.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/relativeLayout" android:background="@drawable/page" android:orientation="horizontal"> <TextView android:id="@+id/textview1" android:textAlignment="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="File Name" android:textColor="#000" android:textSize="23sp" /> <TextView android:id="@+id/textview2" android:textAlignment="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/textview1" android:layout_margin="10dp" android:text="File Date" android:textColor="#000" android:textSize="15sp" /></RelativeLayout>
04 Step (Create a Jason File View )\app\src\main\res\raw\file.JSON
[{ "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"}]
05 Step (Create a Java Class View )\app\src\main\java\com\example\app\DataList.java
public class DataList { private final String name; private final String date; public DataList(String name, String date) { this.name = name; this.date = date; } public String getName() { return name; } public String getDate() { return date; }}
06 Step (Create a Java Class View )\app\src\main\java\com\example\app\MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private static final int TYPE = 1; private final Context context; private final List<Object> listRecyclerItem; public MyAdapter(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.textview1); date = (TextView) itemView.findViewById(R.id.textview2); } } @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.item_view, 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; DataList holidays = (DataList) listRecyclerItem.get(i); itemViewHolder.name.setText(holidays.getName()); itemViewHolder.date.setText(holidays.getDate()); } } @Override public int getItemCount() { return listRecyclerItem.size(); }}
07 Step (Your View Activity Layout)\app\src\main\res\layout\activity_main.xml
<androidx.recyclerview.widget.RecyclerView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/viewall"/>
08 Step (Your View Activity Java)\app\src\main\java\com\example\app\MainActivity.java
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.viewall); // 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 MyAdapter(this, viewItems); mRecyclerView.setAdapter(mAdapter); addItemsFromJSON(); } //// end 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"); DataList dataList = new DataList(name, date); viewItems.add(dataList); } } 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.file); 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); } //// End}
One Onclick Oistener Item Selected
itemViewHolder.name.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(view.getContext(),"click on item: "+holidays.getName(), Toast.LENGTH_LONG).show(); }});