Search Functionality in RecyclerView using EditText

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 …

Search Functionality in RecyclerView using EditText

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.

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.

  • The above code will produce the following layout.

search functionality in recyclerview

  • 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.

Creating RecyclerView Adapter

  • Now we will create Adapter for our RecyclerView. So create a class named CustomAdapter and write the following code.

  • Now come to MainActivity.java and write the following code.

  • Now run the application and you will see your RecyclerView.

search functionality in 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().

  • Now we need to create the method filter().

  • Lastly we need to create the method filter inside CustomAdapter class. So come inside CustomerAdapter.java and define the following method.

  • Now run the application and try searching.

search functionality in recyclerview

  • 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.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x