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 :
- Create new project with Android studio.
- Create xml file for MainActivity with RecyclerView and EditText to search.
- Create list to add in RecyclerView.
- Do ready RecyclerView.
- Add "addTextChangedListener" to EditText.
- Override "onTextChanged" with adapter.filter(s);
- Add filter(CharSequence sequence) to your adapter class.
- Now your ready to use your search with RecyclerView.
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="@+id/etSearch"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:gravity="center"
android:hint="@string/search"/>
<android.support.v7.widget.RecyclerView
app:layout_constraintBottom_toTopOf="@+id/etSearch"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/rvItems"/>
public void filter(CharSequence sequence) { ArrayList<String> 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(); }