Skip to main content

How to implement a search filter in a ListView in Kotlin Android

How to implement a search filter in a ListView in Kotlin Android.

Here's a step-by-step tutorial on how to implement a search filter in a ListView in Kotlin for Android.

Step 1: Create a new Android project

Start by creating a new Android project in Android Studio. Choose an appropriate name and package for your project.

Step 2: Set up the ListView

In your activity layout XML file, add a ListView widget to display the list of items. For example, you can add the following code to your activity_main.xml file:

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

Step 3: Create a custom adapter

Create a custom adapter class that extends the ArrayAdapter class. This adapter will be responsible for populating the ListView with data. In the example below, we'll create a custom adapter called CustomAdapter:

class CustomAdapter(context: Context, items: List<String>) :
ArrayAdapter<String>(context, 0, items) {

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var view = convertView
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false)
}

val item = getItem(position)
val textView = view?.findViewById<TextView>(R.id.textView)
textView?.text = item

return view!!
}
}

Step 4: Set up the search filter

Add a search filter to your activity class. This filter will be used to filter the data in the ListView based on user input. Here's an example of how you can set up the search filter:

class MainActivity : AppCompatActivity() {

private lateinit var listView: ListView
private lateinit var adapter: CustomAdapter
private lateinit var searchView: SearchView

private var dataList = mutableListOf<String>()
private var filteredList = mutableListOf<String>()

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

// Initialize the list view and adapter
listView = findViewById(R.id.listView)
adapter = CustomAdapter(this, dataList)
listView.adapter = adapter

// Add items to the data list
dataList.add("Apple")
dataList.add("Banana")
dataList.add("Cherry")
dataList.add("Durian")
dataList.add("Elderberry")
dataList.add("Fig")
dataList.add("Grape")
dataList.add("Honeydew")

// Set up the search view
searchView = findViewById(R.id.searchView)
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}

override fun onQueryTextChange(newText: String?): Boolean {
newText?.let {
filterList(it)
}
return true
}
})
}

private fun filterList(query: String) {
filteredList.clear()
dataList.forEach {
if (it.contains(query, ignoreCase = true)) {
filteredList.add(it)
}
}
adapter = CustomAdapter(this, filteredList)
listView.adapter = adapter
}
}

Step 5: Create a layout file for the list item

Create a layout file called list_item.xml to define the layout for each item in the ListView. For example, you can add the following code to your list_item.xml file:

<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="16sp"/>

Step 6: Run the app

Build and run your app on an emulator or physical device. You should see a ListView with the initial list of items. As you type in the search view, the ListView should update to show only the items that match the search query.

That's it! You have successfully implemented a search filter in a ListView in Kotlin for Android.