How to Filter a RecyclerView with a SearchView in Android

In this android programming source code example, we are going to explore how to filter a recyclerview with a searchview in android.

Below is a step by step source code to filter a recyclerview with a searchview.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <android.support.v7.widget.SearchView
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/search"/>

</RelativeLayout>

MainActivity.java

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.bluapp.androidview.R;

import java.util.ArrayList;
import java.util.List;

public class SearchViewActivity1 extends AppCompatActivity {
    private RecyclerView recyclerlist;
    private SearchView search;
    private adapter recyclerAdapter;
    ArrayList<String> name;
    ArrayList<String> listname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_view1);
        recyclerlist = (RecyclerView) findViewById(R.id.recyclerlist);
        search = (SearchView) findViewById(R.id.search);
        //Data
        listname = getListCountry();
        name = new ArrayList<>();
        name.addAll(listname);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerlist.setLayoutManager(layoutManager);
        recyclerAdapter = new adapter(SearchViewActivity1.this, name);
        recyclerlist.addItemDecoration(new DividerItemDecoration(recyclerlist.getContext(), layoutManager.getOrientation()));
        recyclerlist.setAdapter(recyclerAdapter);


        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }
            @Override
            public boolean onQueryTextChange(String searchText) {
                recyclerAdapter.getFilter().filter(searchText);
                return false;
            }
        });
    }

    private class adapter extends RecyclerView.Adapter<adapter.myViewHolder> implements Filterable {
        Context context;
        List<String> mData;
        NewFilter mfilter;

        public adapter(Context context, List<String> data) {
            this.context = context;
            this.mData = data;
            mfilter = new NewFilter(adapter.this);
        }

        @Override
        public adapter.myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.recyclerview_adapter1, parent, false);
            return new myViewHolder(view);
        }

        @Override
        public void onBindViewHolder(adapter.myViewHolder holder, int position) {
            holder.country.setText(mData.get(position));
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        @Override
        public Filter getFilter(){
            return mfilter;
        }

        public class myViewHolder extends RecyclerView.ViewHolder {
            TextView country;
            public myViewHolder(View itemView) {
                super(itemView);
                country = (TextView) itemView.findViewById(R.id.name);
            }
        }
        public class NewFilter extends Filter {
            public adapter mAdapter;
            public NewFilter(adapter mAdapter){
                super();
                this.mAdapter = mAdapter;
            }

            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                name.clear();
                final FilterResults results = new FilterResults();
                if(charSequence.length() == 0){
                    name.addAll(listname);
                }else{
                    final String filterPattern =charSequence.toString().toLowerCase().trim();
                    for(String listname : listname){
                        if(listname.toLowerCase().startsWith(filterPattern)){
                            name.add(listname);
                        }
                    }
                }
                results.values = name;
                results.count = name.size();
                return results;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                this.mAdapter.notifyDataSetChanged();
            }

        }
    }

    public ArrayList<String> getListCountry(){
        ArrayList<String> listname = new ArrayList<>();
        listname.add("Emmanuel");
        listname.add("Olayemi");
        listname.add("Henrry");
        listname.add("Mark");
        listname.add("Steve");
        listname.add("Ayomide");
        listname.add("David");
        listname.add("Anthony");
        listname.add("Adekola");
        listname.add("Adenuga");
        return listname;
    }

}

recyclerview_adapter1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="10dp"
        android:lineSpacingMultiplier="0.6"
        android:layout_margin="10dp"
        android:text="Country"
        android:textColor="#000000"
        android:textSize="18sp"
        android:textStyle="bold" />


</RelativeLayout>

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment

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