This article demonstrates how to create searchable RecyclerView in an Android applications. For example, if you take a chat Application when you type the name on search, it will show similar contact names in list view. Let us see how to build it.
Prerequisite
Prior to this, you need to know about
- RecyclerView was introduced in an Android Lollipop (Android 5.0 API Level 21) and is available for use on the devices, which are running API Level 7 and above through the Support Libraries.
- A view which is previously used to display the data for a specific adapter position may be placed in a cache for later reuse to display the same type of data again later.
- RecyclerView and CardViews widgets are helpful to build the complex lists.
Add Dependencies
- compile 'com.android.support:cardview-v7:25.0.1'
- compile 'com.android.support:design:25.0.1'
- }
Setting up the layouts
- <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
- </style>
Create a new resource file in layout directory. Name it as a toolbar and give Root element android.support.v7.widget.toolbar.
Now, go to the toolbar.xml file and copy the code given below.
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.v7.widget.Toolbar
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="?android:attr/actionBarSize"
- android:background="?attr/colorPrimary"
- app:theme="@style/ThemeOverlay.AppCompat.Dark"
- android:id="@+id/toolbar">
- </android.support.v7.widget.Toolbar>
Creating Menu items
In an Action bar, we need a search view, so create new resource directory in an app folder and add an XML layout file for the menus and copy the code given below.
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto">
- <item
- android:id="@+id/actionsearch"
- android:title="Search"
- android:icon="@drawable/ic_action_search"
- app:actionViewClass="android.support.v7.widget.SearchView"
- app:showAsAction="always|collapseActionView">
- </item>
- </menu>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/activity_main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context="com.example.saravanans.searchablerecycler.MainActivity">
- <include
- layout="@layout/toolbar"></include>
- <LinearLayout
- android:layout_height="match_parent"
- android:layout_width="match_parent">
- <android.support.v7.widget.RecyclerView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/recyclerView">
- </android.support.v7.widget.RecyclerView>
- </LinearLayout>
- </LinearLayout>
We need a container to hold the data, so create a new layout for CardView and copy the code given below.
- <?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"
- android:layout_height="75dp"
- android:layout_margin="5dp">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:weightSum="7">
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="5"
- android:background="@drawable/unnamed"
- android:padding="@dimen/cardview_default_radius"
- android:layout_margin="@dimen/activity_vertical_margin" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/name"
- android:text="Name"
- android:textSize="18sp"
- android:textStyle="bold"
- android:gravity="center_vertical"
- android:layout_weight="2"/>
- </LinearLayout>
- </android.support.v7.widget.CardView>
Go to Java model class. Create Getter, Setter Methods, constructor for the name variable and the class respectively.
- public class Name {
- String name_val;
- Name(String name_val)
- {
- this.setName_val(name_val);
- }
- public String getName_val() {
- return name_val;
- }
- public void setName_val(String name_val) {
- this.name_val = name_val;
- }
- }
Recycler Adapter
Create another Java class for RecyclerAdapterView and copy the code given below. This code is having an array list and a view holder class for data binding.
- public class Adapter extends RecyclerView.Adapter <Adapter.vholder>{
- ArrayList<Name>nameArrayList=new ArrayList<>();
- Adapter(ArrayList nameArrayList)
- {
- this.nameArrayList=nameArrayList;
- }
- @Override
- public vholder onCreateViewHolder(ViewGroup parent, int viewType) {
- View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.name_layout,parent,false);
- return new vholder(view);
- }
- @Override
- public void onBindViewHolder(vholder holder, int position) {
- holder.nametxt.setText(nameArrayList.get(position).getName_val());
- }
- @Override
- public int getItemCount() {
- return nameArrayList.size();
- }
- public static class vholder extends RecyclerView.ViewHolder{
- TextView nametxt;
- public vholder(View itemView) {
- super(itemView);
- nametxt=(TextView) itemView.findViewById(R.id.name);
- }
- }
- public void filter(ArrayList<Name>newList)
- {
- nameArrayList=new ArrayList<>();
- nameArrayList.addAll(newList);
- notifyDataSetChanged();
- }
- }
Go to the MainActivity.java. Copy the code given below. Here, I just used a foreach loop to read the strings from cotacts_name array and set it to the adapter view.

Raichand RayPosted Dec 14, 2017, 7:19 AM
This is a good tutorial of crud operation wth Recycler view and search view https://raichand-java.blogspot.in/2017/12/createretrieveupdate-and-delete-row-in.html
Gokhul VarmanPosted Apr 11, 2017, 3:04 PM
Nice da.very good..keep posting more articles..all the best