SearchView implementation in ListView using Kotlin in Android Studio


SearchView is a real- time responsive filtration method which helps user to sort required item from list using characters. Android SearchView requires onQueryChange and onQuerySubmit to register inputs. In this tutorial we’ll be creating a ListView which SearchView feature which redirects to next Activity.

Setup Project

Create a new Project in Android Studio, use Kotlin as language. once Gradle is loaded, create a new kotlin activity, and name it SecondActivity. SecondActivity will be opened when user taps on any item of ListView. Now, create a XML layout file inside layout folder for SecondActivity and name it second_activity. Add the code given below in SecondActivity.kt to register your XML layout file.

class SecondActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {

Design your Layout

Open activity_main.xml located in Layout folder, and the code given below

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
app:layout_constraintTop_toTopOf="parent" />


Here, we are adding a ListView with ID lv_Listview. This will format a ListView with text fields in it.

Once done, open second_acitivity.xml and add the code given below.

android:text="Product information"
tools:layout_editor_absoluteY="343dp" />

This will add a simple TextView with text as “Product information”. This will act a static page.

Add Data

There are multiple ways to store data in android, array, list, viewmodel etc. For this tutorial, we are using String resource file. To add data in String Resource file, extend Values folder inside res folder, and open strings.xml. Now add the code given below.

<string name="app_name">SearchInListView</string>
<string-array name="product_array">
<item>Samsung LED TV</item>
<item>LG LED TV</item>
<item>Sony LED TV</item>
<item>LG Laptop</item>
<item>Samsung Music System</item>
<item>LG Laptop</item>

Here, we are creating string-array to store items.

Add SearchView Button

SearchView button requires an image available in drawables folder. There are plenty of in-built clip art already available in Android Studio. We will be considering one of them in this tutorial. Create a new Vector Asset inside drawable folder. Click on Clip art and search “search“. Select the magnifying glass clip art. Name it ic_search.

To add SearchView button on the toolbar, we will first create a folder and name it menu in the res folder directory. Now, create a new menu resource file inside menu folder and name it nav_menu. Once done, open the file and paste the below given code.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android=""

Above code will add search button in toolbar. Next step is to integrate search functionality.

ListView in MainActivity

Open MainActivity.kt and add the code given below.

class MainActivity : AppCompatActivity() {
    private lateinit var adapter: ArrayAdapter<*>
    private lateinit var lv_listView: ListView
    override fun onCreate(savedInstanceState: Bundle?) {
        adapter = ArrayAdapter(this,android.R.layout.simple_list_item_1,resources.getStringArray(R.array.product_array))
        lv_listView = findViewById(
        lv_listView.adapter = adapter
        lv_listView.onItemClickListener =AdapterView.OnItemClickListener{parent, view, position, id -> val intent = Intent(parent.context,
            //Use intent.putExtra to send data from this activity, Learn more about intent.putExtra on

Here, we are creating an ArrayAdapter for listing data in the ListView.

adapter is the reference to ArrayAdapter which registers the data available in strings.xml and assigns it in simple list format.

Now, we have assigned ListView of MainActivity to adapter which then assigns the data.

lv_listView.onItemClickListener will help is getting the selected item from user, to which we have assigned StartActivity() which will start the next activity.

Add SearchView function

Open MainActivity.kt and add the function given below in MainActivity class.

override fun onCreateOptionsMenu(menu: Menu?): Boolean {

val search = menu?.findItem(
val searchView = search?.actionView as SearchView
searchView.queryHint = "Search"

searchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String?): Boolean {
return false

override fun onQueryTextChange(newText: String?): Boolean {
return true
return super.onCreateOptionsMenu(menu)

This function will create search menu in the toolbar.

Here, we are focusing on onQueryTextChange which will filter items from adapter according to newText, which is the keyboard input provided by user.

Now Save and Run.

Where To Go From Here?

Try pushing data using intent putExtra. Read intent putExtra article to learn more.

Written by

XR Developer responsible for end-to-end development of XR solutions spanning multiple domains, by using various XR and WebXR libraries.

1 thought on “SearchView implementation in ListView using Kotlin in Android Studio

Leave a Reply