Skip to main content

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

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

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

Step 1: Set up the project

  • Create a new Android project in Kotlin.
  • Open the build.gradle file and add the following dependency for the ListView:
implementation 'androidx.recyclerview:recyclerview:1.2.1'

Step 2: Design the layout

  • Open the activity_main.xml layout file and add a ListView and a SearchView widget:
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

Step 3: Create a data model

  • Create a new Kotlin class called Item to represent the items in the ListView:
data class Item(val name: String)

Step 4: Create the adapter

  • Create a new Kotlin class called ItemAdapter that extends ArrayAdapter:
class ItemAdapter(
context: Context,
private val layoutResourceId: Int,
private val items: List<Item>
) : ArrayAdapter<Item>(context, layoutResourceId, items) {

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view = convertView ?: LayoutInflater.from(context).inflate(layoutResourceId, parent, false)
val item = items[position]

view.findViewById<TextView>(R.id.textView).text = item.name

return view
}
}

Step 5: Initialize the ListView

  • In the MainActivity.kt file, initialize the ListView and the data source:
class MainActivity : AppCompatActivity() {
private lateinit var listView: ListView
private lateinit var searchView: SearchView
private lateinit var itemAdapter: ItemAdapter
private lateinit var items: List<Item>

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

listView = findViewById(R.id.listView)
searchView = findViewById(R.id.searchView)

items = listOf(
Item("Apple"),
Item("Banana"),
Item("Cherry"),
Item("Grapes"),
Item("Orange"),
Item("Watermelon")
)

itemAdapter = ItemAdapter(this, R.layout.list_item, items)
listView.adapter = itemAdapter
}
}

Step 6: Implement search functionality

  • Add the following code inside the onCreate method to handle search functionality:
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
return false
}

override fun onQueryTextChange(newText: String): Boolean {
val filteredItems = items.filter { it.name.contains(newText, ignoreCase = true) }
itemAdapter.clear()
itemAdapter.addAll(filteredItems)
itemAdapter.notifyDataSetChanged()
return true
}
})

Step 7: Run the app

  • Build and run the app on an emulator or a physical device.
  • Test the search functionality by typing in the SearchView and observing the filtered results in the ListView.

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