Search Item in RecyclerView

Aayush Puranik
2 min readJun 13, 2020

This article contains basic implementation of a search in RecyclerView. The code below shows implementation for search view in main activity. First thing is to implement SearchView.OnQueryTextListener, this is an interface that helps to fetch query which as given by user as input for filtering the list. This interface gives us two methods onQueryTextSubmit and onQueryTextChange with parameter as the query string provided by user and as the name suggest onQueryTextChange is called as soon as user starts typing in search view and onQueryTextSubmit is called when user press the search button.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<androidx.appcompat.widget.SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:divider="@color/colorPrimaryDark"
app:queryHint="@string/search_hint"
app:iconifiedByDefault="false"
app:showDividers="middle"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>
MainActivity.kt
class MainActivity : AppCompatActivity(),SearchView.OnQueryTextListener {

private var mainList = listOf<String>()
private lateinit var adapter: Adapter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

fetchList()

adapter = Adapter(this, ArrayList(mainList))

recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.addItemDecoration(DividerItemDecoration(this, RecyclerView.VERTICAL))

searchView.setOnCloseListener {
adapter.updateList(ArrayList(mainList))
true
}

searchView.setOnQueryTextListener(this)
}

private fun fetchList() {
val list = arrayListOf<String>()
for (i in 0..50) {
list.add("$i item")
}
mainList = list
}

/**
* This function deals with the logic of search as soon as user clicks search button.
* The list will show filtered data as soon as user clicks submit button, the list is be
* unchanged when user types anything.
*/
override fun onQueryTextSubmit(searchString: String?): Boolean {
// searchLogic(searchString)
return true
}

/**
* This function deals with the logic of search as soon as user types something in
* search view text. The list will show filtered data as soon as user types anything to search
*/
override fun onQueryTextChange(searchString: String?): Boolean {
searchLogic(searchString)
return false
}

/**
* This function deals with the logic of search the if condition represents if search
* view is having a value to search then check if it exists in list else deals with a
* condition when search view is empty then reset the list
*/
private fun searchLogic(searchString: String?) {
if (searchString?.isNotEmpty() == true) {

val list = mainList.filter {
it
.contains(searchString, true)
}
adapter.updateList(ArrayList(list))
} else {
adapter.updateList(ArrayList(mainList))
}
}
}

--

--