Implement search functionality with EditText and RecyclerView in android

Search functionality with recyclerview in android. 

Here is simple demo to make your recyclerview searchable. By following this demo you can implement search functionality to your RecyclerView in android. Here I got country list and display in Recyclerview.Follow these steps to implement search functionality with RecyclerView :

  1. Create new project with Android studio.
  2. Create xml file for MainActivity with RecyclerView and EditText to search.
  3. Create list to add in RecyclerView.
  4. Do ready RecyclerView.
  5. Add “addTextChangedListener” to EditText.
  6. Override “onTextChanged” with adapter.filter(s);
  7. Add filter(CharSequence sequence) to your adapter class.
  8. Now your ready to use your search with RecyclerView.

MainActivity code

private void init() {
        RecyclerView rvItems = (RecyclerView) findViewById(;
        rvItems.setLayoutManager(new LinearLayoutManager(this));
        ArrayList countryList = new ArrayList<>();
        for (String s : Locale.getISOCountries()) {
            countryList.add(new Locale("", s).getDisplayCountry(Locale.ENGLISH));
        final RvSearchDemoAdapter adapter = new RvSearchDemoAdapter(this, countryList);

        EditText etSearch = (EditText) findViewById(;
        etSearch.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {


            public void onTextChanged(CharSequence s, int start, int before, int count) {

            public void afterTextChanged(Editable s) {


Adapter class

public class RvSearchDemoAdapter extends RecyclerView.Adapter {
    private ArrayList countries;
    private ArrayList countriesCopy;
    private LayoutInflater inflater;

    public RvSearchDemoAdapter(Context context, ArrayList countries) {
        this.countries = countries;
        this.countriesCopy = new ArrayList<>();
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    public RvSearchDemoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RvSearchDemoHolder(inflater.inflate(R.layout.item_country, parent, false));

    public void onBindViewHolder(RvSearchDemoHolder holder, int position) {

    public int getItemCount() {
        return countries.size();

    public void filter(CharSequence sequence) {
        ArrayList temp = new ArrayList<>();
        if (!TextUtils.isEmpty(sequence)) {
            for (String s : countries) {
                if (s.toLowerCase().contains(sequence)) {
        } else {

Get detailed project from Github,

Share this content:


More Posts

How does data persistence work in Android?

Data persistence in Android is the ability to save data to the device so that it can be accessed later, even if the app is closed or the device is restarted. There are several ways to achieve data persistence in Android

Fragments and Activities: Two Pillars of Android Development

Fragments and Activities are both important components of Android development. Fragments are a good choice for creating reusable and dynamic UIs, while Activities are a good choice for managing the lifecycle of an app’s UI and implementing core features.

Table of Contents

Send Us A Message