Android RecyclerView → Android RecyclerView MaterialSearchBar Search Filter – Camposha

Android RecyclerView

Android RecyclerView

Android RecyclerView

Android RecyclerView MaterialSearchBar Search Filter


Android RecyclerView MaterialSearchBar Search Filter

We had done a tutorial some months later about Search/Filter with recyclerView. However, that was with the searchview as our search form. Today we see how to search filter with material searchbar as the search component. User will perform realtime search filter. As they type we filter the cards based on the search term. Our recyclerview comprises of material cardviews. We populate our recyclerview via an arraylist of strings. Let’s go.


  • Here’s the screenshot of the project.

Android RecyclerView Material Seatchbar Search Filter.

Project Demo

Android Material SearchBar RecyclerView example. : Project Structure.

Project Structure

Common Questions this example explores

  • Android RecyclerView Search/Filter with searchbar.
  • Android toolbar search filter.
  • Search filter recyclerview cardviews.
  • Android material searchbar recyclerview.

Tools Used

This example was written with the following tools:

  • Windows 8
  • AndroidStudio IDE
  • Genymotion Emulator
  • Language : Java
  • Topic : RecyclerView Events, Android SimpleRecyclerView

Libaries Used

  • We use Android SimpleRecyclerView library.

Source Code

Lets jump directly to the source code.

Build.Gradle Project Level

  • Our project level build.gradle.
  • We add repositories for fetching our dependencies here.
  • The dafult,jccenter() is already specified.
  • However, we add the maven and specify the url we’ll use to fetch our third part library here.
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
    dependencies {
        classpath ''

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
//add maven for fetching our materialsearchbar library.
allprojects {
    repositories {
        maven { url "" }

task clean(type: Delete) {
    delete rootProject.buildDir

Build.Gradle App Level

  • Our module level build.gradle file.
  • We specify compilesdk,minimum sdk,target sdk and dependencies.
  • Note that the minimum sdk for this project is API level 16 as the material serahbar library we use requires that.
  • We also add dependencies using ‘compile’ statement.
  • Our activity shall derive from the appCompatActivity to make it target earlier android versions.
  • We also specify design support library as well as CardView.
  • Add dependencies including com.github.mancj:MaterialSearchBar:0.7.1.
  • This is the material searchbar we will use in our project.
apply plugin: ''

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.tutorials.hp.searchbarrecyclerview"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner ""
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), ''
//Add our dependencies including materialsearchbar
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile ''
    compile ''
    compile ''
    compile ''
    compile 'com.github.mancj:MaterialSearchBar:0.7.1'

  • Our MyAdapter class.
  • Derives from RecyclerView.Adapter.
  • Implements android.widget.filterable interface.
  • We include our MyViewHolder as an inner class.
  • This adapter layout will be responsible inflating model layout and binding data to resulting views.
package com.tutorials.hp.searchbarrecyclerview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements Filterable {
    ArrayList<String> galaxies;
    ArrayList<String> currentList;

    public MyAdapter(ArrayList<String> galaxies) {
        this.galaxies = galaxies;

    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,parent,false);
        return new MyViewHolder(v);

    public void onBindViewHolder(MyViewHolder holder, int position) {
    public int getItemCount() {
        return galaxies.size();

    public void setGalaxies(ArrayList<String> filteredGalaxies)

    public Filter getFilter() {
        return FilterHelper.newInstance(currentList,this);

    - Our MyViewHolder class
    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView nametxt;
        public MyViewHolder(View itemView) {
            nametxt= itemView.findViewById(;


  • Our FilterHelper class.
  • Derives from android.widget.filter.
  • We perform filtering here and publish results back to the adapter which is then refreshed.
package com.tutorials.hp.searchbarrecyclerview;

import android.widget.Filter;

import java.util.ArrayList;
public class FilterHelper extends Filter {
    static ArrayList<String> currentList;
    static MyAdapter adapter;

    public static FilterHelper newInstance(ArrayList<String> currentList, MyAdapter adapter) {
        return new FilterHelper();

    - Perform actual filtering.
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults filterResults=new FilterResults();

        if(constraint != null && constraint.length()>0)
            //CHANGE TO UPPER

            //HOLD FILTERS WE FIND
            ArrayList<String> foundFilters=new ArrayList<>();

            String galaxy;

            for (int i=0;i<currentList.size();i++)
                galaxy= currentList.get(i);

                    //ADD IF FOUND


        return filterResults;

    protected void publishResults(CharSequence charSequence, FilterResults filterResults) {

        adapter.setGalaxies((ArrayList<String>) filterResults.values);

  • Our MainActivity class.
  • Derives from AppCompatActivity which is a Base class for activities that use the support library action bar features.
  • Methods: onCreate(),getData().
  • Inflated From activity_main.xml using the setContentView() method.
  • The views we use are RecyclerView.
  • Reference RecyclerView from our layout specification using findViewById().
  • getData() will be our data source.


  • Our activity_main.xml layout
  • Will be inflated to our mainactivity.
  • Add MaterialSearchBar in the appbar.


  • Add recyclerview here.


  • As the name suggests, this layout models our viewitem.
  • We define how each Card shall appear in our List.
  • So at the root level we have a CardView widget.
  • We can also customize our CardView by specifying cardCornerRadius,cardElevation,width,height etc.
  • Each Card shall comprise a textview.

Video Tutorial

  • We have a YouTube channel with almost a thousand tutorials, this one below being one of them.


  • You can Download the full Project below. Source code is well commented.


How To Run

  1. Download the project above.
  2. You’ll get a zipped file,extract it.
  3. Open the Android Studio.
  4. Now close, already open project.
  5. From the Menu bar click on File >New> Import Project.
  6. Now Choose a Destination Folder, from where you want to import project.
  7. Choose an Android Project.
  8. Now Click on “OK“.
  9. Done, your done importing the project,now edit it.


  • Visit our channel for more examples like these.


Leave a Reply

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

Join Us