TheJadav

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(R.id.rvItems);
        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);
        rvItems.setAdapter(adapter);

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

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                adapter.filter(s);
            }

            @Override
            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<>();
        countriesCopy.addAll(countries);
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public void onBindViewHolder(RvSearchDemoHolder holder, int position) {
        holder.bind(countries.get(position));
    }

    @Override
    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)) {
                    temp.add(s);
                }
            }
        } else {
            temp.addAll(countriesCopy);
        }
        countries.clear();
        countries.addAll(temp);
        notifyDataSetChanged();
        temp.clear();
    }
}

Get detailed project from Github,

Share this content:

Share:

More Posts

Introduction to Kotlin: A Versatile and Modern Programming Language

Kotlin, a versatile and modern programming language, offers developers a concise, safe, and interoperable coding experience. With features like null safety, extension functions, and coroutines, Kotlin enhances productivity and readability, making it an attractive choice for Android development and beyond. Whether you’re new to programming or an experienced developer, exploring Kotlin opens up a world of possibilities for building robust and expressive applications.

Mastering the Android Activity Lifecycle: A Comprehensive Guide

The Android Activity Lifecycle is a fundamental concept for Android developers. It defines how an Activity behaves during its lifecycle, from creation to destruction. Understanding the Activity Lifecycle is crucial for managing resources efficiently and delivering a smooth user experience. In this blog post, we’ll briefly introduce the different states an Activity can be in and the main callback methods associated with each state. Let’s dive in and explore this important aspect of Android app development!

Table of Contents

Send Us A Message