How to Filter a RecyclerView with SearchView – Android Studio Tutorial

In this video we will learn how to use the SearchView widget to filter a RecyclerView in real time.
For this we will add the SearchView as an item to our options menu and make it expandable with the collapseActionView attribute. This way we can display it as an icon in our app bar, which expands to an input field when we click on it.
We implement the Filterable interface into our RecyclerView Adapter and create our own Filter, where we handle the filter logic in the asynchronous performFiltering method, which publishes the search results to the publishResults method on the UI thread.
In our Activity we then connect our SearchView with the Filter by setting an OnQueryTextListener onto our SearchView and listening for the text input in onQueryTextChange.

Example code:

❗ Subscribe to the channel:

⏯ Watch more tutorials:

⭐ Become a channel member for exclusive tutorials and other perks:

㊙ Help translating the videos:

💚 Support the content:

💬 Join the Discord chat:

🎧 Audiobooks and podcasts for programmers:

👶 How to get started with Android development:

📣 Follow Coding in Flow on social media:






Xem thêm bài viết khác:

25 thoughts on “How to Filter a RecyclerView with SearchView – Android Studio Tutorial

  1. My book or library project

    I have activity_main that includes -listView –

    Row_itme includes textView

    Activity_web.xml includes webView

    Now I have master list titles, sub-menu titles, then documents or text

    The files I have are in the -assets folder, there are also subfolders

    Documents, texts, or book content are files – html-

    For example 1.html – 2.html -3.html ….

    Request: Add a search engine that accesses -html pages

    Note that I applied a search engine, but it only works under -listView –

  2. All works Fine… But when I search and delete that item then whole recycler view items get removed, why ❓❓❓

  3. I'm using a edittext to get the work done and it is working okay with TEXT WATCHER but how to get the full list if I clear the edittext?

  4. Wie kann man das benutzen, wenn man ein MVVM-Modell hat?
    Du hast ja schon eine Reihe dazu gemacht, aber wie kann ich da die Such-Funktion hinzufügen?

  5. private List<upload> mUploads;

    private List<upload> getmUploads;
    public image_adapter(Context context, List<upload> uploads) {

    mContext = context;

    mUploads = uploads;

    getmUploads=new ArrayList<>(uploads);
    i am not getting data inside "getmUploads" ,its showing null exception, getmUploads.size() is 'o'.
    i don't no why, can any one help me please

  6. @Coding in Flow I have a question. I want to use this search view in separate activity. Suppose there is MainActivity which shows & implement Recyclerview and there is another acitivity named searchactivity which has one Edittext and I want user to type something in that Edittext and I want to use it as query to search in my listview which in in MainActivity. How can I achieve this? If you have some video about it, please share the name or link of that video. Thanks alot

  7. Finally. A great explanation and to the point kind of video after a long time. Thumbs up. BTW, I wonder how much time you put on this video? You know, I am also a beginner kind of content creater so this interests me.

  8. How can this be implemented with FirestoreRecyclerAdapter. I have tried with ArrayList and List but there re constructor errors. Please suggest

  9. You deserve much more than just a like ✌🏻✌🏻
    Whenever i need help i automatically come to your channel and search for the solution
    Thank you so much man

  10. I am getting this error
    java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.getActionProvider()
    on this line
    val searchView: SearchView = searchItem.actionProvider as SearchView
    i tried using
    import android.widget.SearchView
    import androidx.appcompat.widget.SearchView

  11. Thanks a lot man! Clear and simple! I wonder if there is a way to filter diacritics from RecyclerView, do you have such an example? Thank you in advance!

  12. I didn't get the ( app: actionViewClass = "" ). Anyone can help me?

  13. i just copied the code and got this error;

    Getting this Error….please help me with this;

    java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference

  14. Thanks so much for the video.
    However, I have an issue…. when i search then it shows result, but if I try to use backspace or hit back button, the original RecyclerView is lost. And if suppose there are no matching results then the screen is just blank. Even if I press back button the screen is blank.
    I understand that the original view is getting changed when I do notify dataset changed . How can I return to the original list.
    I am using Room database and Live Data, which is made based on your tutorial of Arhitecture component.
    It will be a real help if you can provide any solution. I have been trying to solve for last 3-4 days but couldn't do it.
    Thanks in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *