In this post we will learn adding Search Functionality in RecyclerView. If a list contain many items then giving a search option is really necessary for making it user friendly. So lets see how to add Search Functionality in RecyclerView.
Table of Contents
Creating a RecyclerView
Creating new Project
- The very first thing we need is the RecyclerView itself. Only then we can add the search. So lets create a new Android Studio Project.
Adding RecyclerView and CardView
- Once you have created a new Android Studio Project add RecyclerView and CardView to it. If you don’t know adding RecyclerView and CardView you can refer to the previous RecyclerView Tutorial.
Creating Layouts
- Now once you have added the project come inside activity_main.xml and create a RecyclerView. The code for my activity_main.xml is below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <?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="net.simplifiedlearning.recyclerviewsearch.MainActivity"> <EditText android:id="@+id/editTextSearch" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="Enter search term" tools:layout_constraintTop_creator="1" tools:layout_constraintRight_creator="1" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" app:layout_constraintRight_toRightOf="parent" android:layout_marginTop="16dp" tools:layout_constraintLeft_creator="1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" /> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="0dp" android:layout_height="0dp" tools:layout_constraintTop_creator="1" tools:layout_constraintRight_creator="1" tools:layout_constraintBottom_creator="1" android:layout_marginStart="8dp" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginEnd="8dp" app:layout_constraintRight_toRightOf="parent" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/editTextSearch" tools:layout_constraintLeft_creator="1" android:layout_marginBottom="8dp" app:layout_constraintLeft_toLeftOf="parent" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintVertical_bias="1.0"> </android.support.v7.widget.RecyclerView> </android.support.constraint.ConstraintLayout> |
- The above code will produce the following layout.
- As you can see have a RecyclerView and an EditText. When we type something on EditText the RecyclerView will get filtered.
- Now we will create a layout for our List Item. So create a new Layout Resource file named list_layout.xml and write the following xml code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v7.widget.CardView android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textViewName" android:padding="15dp" android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.v7.widget.CardView> </LinearLayout> |
Creating RecyclerView Adapter
- Now we will create Adapter for our RecyclerView. So create a class named CustomAdapter and write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package net.simplifiedlearning.recyclerviewsearch; 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.ArrayList; /** * Created by Belal on 6/6/2017. */ public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { private ArrayList<String> names; public CustomAdapter(ArrayList<String> names) { this.names = names; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.list_layout, parent, false); return new ViewHolder(v); } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.textViewName.setText(names.get(position)); } @Override public int getItemCount() { return names.size(); } class ViewHolder extends RecyclerView.ViewHolder { TextView textViewName; ViewHolder(View itemView) { super(itemView); textViewName = (TextView) itemView.findViewById(R.id.textViewName); } } } |
- Now come to MainActivity.java and write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | package net.simplifiedlearning.recyclerviewsearch; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { RecyclerView recyclerView; EditText editTextSearch; ArrayList<String> names; CustomAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); names = new ArrayList<>(); names.add("Ramiz"); names.add("Belal"); names.add("Azad"); names.add("Manish"); names.add("Sunny"); names.add("Shahid"); names.add("Deepak"); names.add("Deepika"); names.add("Sumit"); names.add("Mehtab"); names.add("Vivek"); recyclerView = (RecyclerView) findViewById(R.id.recyclerView); editTextSearch = (EditText) findViewById(R.id.editTextSearch); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(this)); adapter = new CustomAdapter(names); recyclerView.setAdapter(adapter); } } |
- Now run the application and you will see your RecyclerView.
- As you can see we have the RecyclerView. Now we will implement Search Functionality in RecyclerView.
Implementing Search Functionality in RecyclerView
- Come to MainActivity.java and add a TextChangedListener as shown below. You have to do this inside onCreate().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | //adding a TextChangedListener //to call a method whenever there is some change on the EditText editTextSearch.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { //after the change calling the method and passing the search input filter(editable.toString()); } }); |
- Now we need to create the method filter().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | private void filter(String text) { //new array list that will hold the filtered data ArrayList<String> filterdNames = new ArrayList<>(); //looping through existing elements for (String s : names) { //if the existing elements contains the search input if (s.toLowerCase().contains(text.toLowerCase())) { //adding the element to filtered list filterdNames.add(s); } } //calling a method of the adapter class and passing the filtered list adapter.filterList(filterdNames); } |
- Lastly we need to create the method filter inside CustomAdapter class. So come inside CustomerAdapter.java and define the following method.
1 2 3 4 5 6 7 8 9 | //This method will filter the list //here we are passing the filtered data //and assigning it to the list with notifydatasetchanged method public void filterList(ArrayList<String> filterdNames) { this.names = filterdNames; notifyDataSetChanged(); } |
- Now run the application and try searching.
- Bingo! It is working absolutely fine.
So we have the Search Functionality in RecyclerView. Thats all for this tutorial. If you are having any confusions or queries then lets have a discussion on comment section. Also don’t forget to SHARE the post if you found it useful. Thank You 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.
hello sir,Nice tutorial its working fine.
I want to do search option for the data which is loaded from the database.
i can send u the code send me ur mail id
i m sending the mai id..plz send the code..sujaffaralikhan@gmail.com
can u send the same code to me my email id is-hirenpatel.a3@gmail.com
pls send me also the code jkmanliguez@gmail.com
Brother Kindly send me code also ? ?
Email : muhammadadnanijaz01@gmail.com
it is slow when i put 10000 words
Yes obviously this approach is not good for huge data sets.
Sir i want that code too(Filter data in recyclerview using firebase) so can you send me the code of that as soon as possible.
Email-Id:amioza1099@gmail.com
Me too I need to search data directly from database
Send me the code please on : brunelndayiragije1995@gmail.com
Me too I need to search data directly from database msql
Send me the code please on : brunelndayiragije1995@gm
dear i need this code also
Thankx a million (Y)
ur tutorials really helpful , keep it up
Hello, thanks for the excellent tutorial. It’s working. Now I would like to know how to make the same with pair list like this one List<Pair>, ie: example of recyclerView giving the name and some description.
Can you please make a tutorial on how to make search view like google play and populating the suggestions from firebase?
What if am performing search from a List array with values such as first name, second name, etc .. your example is ding it from a single array list with single value names.. how would go about it? Your response would rely be appreciated.. Thanx
Please, how to do a search filter on the actionbar with recyclerveiw + volley?
thank, sir
I implement search filter with recyclerview + volley that’s work very well
If list view contains number then how to search only numbers instead of characters
hello
how to implement highlight text for this tutorial
Belal sir ..if the data coming from server using volley what will be the code..plz help
hai sir
can u give me idea
how to search data by using multiple strings
means
for example rajasekar,bangalore like that
addTextChangeListener allow only one String even it should not allow space
I have written same code for filter but while entering value to edit text by custom buttons it lag .can you help to get out from it
I want to do search option for the data which is loaded from the mysql database.
Please can you send me the code vou have already sent pavithra.
My e-mail: matanana@web.de
Hello Sir, I am populating my recyclerView from mysql database. So, the searching is not working.
private void filter(String text) {
//new array list that will hold the filtered data
List filterdNames = new ArrayList();
//looping through existing elements
for (ModelUpdateModerator s : updateModeratorList) {
//if the existing elements contains the search input
if (s.toLowerCase().contains(text.toLowerCase())) {
//adding the element to filtered list
filterdNames.add(s);
}
}
//calling a method of the adapter class and passing the filtered list
adapterUpdateModerator.filterList(filterdNames);
}
error in toLowerCase(). I know because its not string. But I can’t find any solution.
Please sent me this come on Rahilmithani007@gmail.com
Its awesome, can i get a search tutorial from a mysql table
Working Like a charm
it is slow when i put 10000 words
Very Helpful Tutorial!! This tutorial searches in one RecyclerView in the same Activity using EditText..
Can you please suggest how to use the EditText or SearchView to search for items in different Activities..
I have seven RecyclerViews, in seven separate Activities.. I want to filter the results for items in all the RecyclerViews of my Activities..
What changes i have to make to the CustomAdapter class or to the filter() method, and also to the MainActivity class as suggested by you in this tutorial..
Hope you understood my problem… If you could mail me the suggestion .. it would be of great help to me…
my mail id is ab5593@gmail.com ..
here the method of adapter can not be called within mainActivity.
i have created a object for adapter in mainActivity but it doesnot call the adapter filter method.please help…..
Even all the anroid tutorial could not let me know how can i implement search functionlaity in Recyclerview. You did it man. Its is workig.
Million Thanks to you.
BTW — I am working on a project. Myself is an android developer building my own app and to set up my company. If you have in depth knowledge and want to work as a freelancer kindly contact me. rajeshwindows8@gmail.com
Awsome:) Saved my day!!