Android RecyclerView with OnItemClickListener and Search Filter

In this tutorial we are going learn how to implement Android RecyclerView with OnItemClickListener and Search Filter. In this application we will add search icon on the left side of Action bar, when the icon is clicked it will display search view which allows user to search for items in the list as shown in the images below.

Android RecyclerView with OnItemClickListener and Search Filter

Now lets get started with coding. This tutorial is a continuation of my previous tutorial on Android RecyclerView With OnItemClickListener which i have explained how to create RecyclerView with OnItemClickListener, Hence we won’t start a new project we shall continue to add Search Filter, Thus i would advice you to go through the previous tutorial first .

  • Create a folder under res = > xml and name it xml.
  • Inside xml folder create an xml file and name it searchable.xml.
  • Copy and paste the code below in your searchable file.
  • Your file should look like the onces below
  • create menu_main

  • Open your manifest file and configure the search view by adding the following code.
  • Adding Toolbar to our activity
  • Open your activity_main, Copy paste the code below.
  • Now we are done creating layout files
  • Adding Class Filter to FruitAdapter

  • This class will implement Filterable and will request you to override getFilter method.
  • In order to filter our list We need an array of filteredSearch which if empty we show the default item listt.
  • Open your FruitAdapter class and paste the code below.
  • Now open MainActivity.Java
  • This class Attaches our data to recyclerView, Inflate menu and Display SearchView through onCreateOptionsMenu method, Listen character change as user is typing through searchView.setOnQueryTextListener.
  • The entered query will be passed to FruitAdapter class using adapter.notifyDataSetChanged then recyclerView will refresh with filtered data.
  • Copy and Paste the code below in your MainActivity.
  • Well, that’s all try to run your app and you will see the output similar to one below.
  • RecyclerView with Search Filter

  • To filter Click on search icon and type the name of any fruit.
  • To download source code click the link here
  • If you have a question or need help please comment below.