How to Implement Android SearchView with Example
Last Updated :
12 Jul, 2025
The SearchView widget is used to provide a search interface to the user so that the user can enter his search query and submit a request to the search provider and get a list of query suggestions or results.
Class Syntax: public class SearchView extends LinearLayout implements CollapsibleActionView Class Hierarchy: java.lang.Object âģ android.view.View âģ android.view.ViewGroup âģ android.widget.LinearLayout âģ android.widget.SearchView Example to Demonstrate SearchView: In this article, you will create a basic search application with a search view and a list view. The user will type a search query in the search view which is present in the action bar. Here are explained steps:
Step-by-Step Implementation Android SearchView Step 1: Create a New Project in Android Studio To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio .
The code for that has been given in both Java and Kotlin Programming Language for Android.
Step 2: Working with the XML Files Next, go to the activity_main.xml file , which represents the UI of the project. It consists of a Relative Layout with ListView in it from which data is to be searched. Here is the complete code for activity_main.xml:
activity_main.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:background= "#168BC34A"
tools:context= ".MainActivity" >
<SearchView
android:id= "@+id/searchview"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:background= "@color/green"
app:layout_constraintEnd_toEndOf= "parent"
app:layout_constraintHorizontal_bias= "0.5"
app:layout_constraintStart_toStartOf= "parent"
app:layout_constraintTop_toTopOf= "parent" />
<!-- List View from which data is to be searched -->
<ListView
android:id= "@+id/listView"
android:layout_width= "match_parent"
android:layout_height= "0dp"
android:layout_below= "@id/searchview"
app:layout_constraintEnd_toEndOf= "parent"
app:layout_constraintHorizontal_bias= "0.5"
app:layout_constraintStart_toStartOf= "parent"
app:layout_constraintTop_toBottomOf= "@+id/searchview" />
</androidx.constraintlayout.widget.ConstraintLayout>
Design UI: Step 3: Working with the MainActivity File Go to the MainActivity File and refer to the following code.
Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail. In this file, items are added to List View manually and setOnQueryTextListener is attached to Search View. onQueryTextSubmit() method is overridden in which List View gets filtered according to the search query entered by the user.
MainActivity File:
Java
package org.geeksforgeeks.demo ;
import android.os.Bundle ;
import android.widget.ArrayAdapter ;
import android.widget.ListView ;
import android.widget.SearchView ;
import android.widget.Toast ;
import androidx.appcompat.app.AppCompatActivity ;
import java.util.ArrayList ;
public class MainActivity extends AppCompatActivity
{
// ListView and SearchView objects
private ListView listView ;
private SearchView searchBar ;
// ArrayAdapter for ListView
private ArrayAdapter < String > adapter ;
// ArrayList for ListView data
private ArrayList < String > myList ;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . activity_main );
// Initialize ListView and SearchView
listView = findViewById ( R . id . listView );
searchBar = findViewById ( R . id . searchview );
// Add items to ArrayList
myList = new ArrayList <> ();
myList . add ( "C" );
myList . add ( "C++" );
myList . add ( "C#" );
myList . add ( "Java" );
myList . add ( "Advanced java" );
myList . add ( "Interview prep with c++" );
myList . add ( "Interview prep with java" );
myList . add ( "data structures with c" );
myList . add ( "data structures with java" );
// Set adapter to ListView
adapter = new ArrayAdapter <> ( this ,
androidx . appcompat . R . layout . support_simple_spinner_dropdown_item ,
myList );
listView . setAdapter ( adapter );
// Set SearchView query text listener
searchBar . setOnQueryTextListener ( new SearchView . OnQueryTextListener () {
@Override
public boolean onQueryTextSubmit ( String query ) {
if ( myList . contains ( query )) {
adapter . getFilter (). filter ( query );
}
else {
Toast . makeText ( MainActivity . this , "No Match found" , Toast . LENGTH_LONG ). show ();
}
return false ;
}
@Override
public boolean onQueryTextChange ( String newText ) {
return false ;
}
});
}
}
Kotlin
package org.geeksforgeeks.demo
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.SearchView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity () {
// List View object
private lateinit var listView : ListView
private lateinit var search_bar : SearchView
// Define array adapter for ListView
var adapter : ArrayAdapter < String >? = null
// Define array List for List View data
var mylist : ArrayList < String >? = null
override fun onCreate ( savedInstanceState : Bundle?) {
super . onCreate ( savedInstanceState )
setContentView ( R . layout . activity_main )
// initialise ListView with id
listView = findViewById ( R . id . listView )
search_bar = findViewById ( R . id . searchview )
// Add items to Array List
mylist = ArrayList ()
mylist !! . add ( "C" )
mylist !! . add ( "C++" )
mylist !! . add ( "C#" )
mylist !! . add ( "Java" )
mylist !! . add ( "Advanced java" )
mylist !! . add ( "Interview prep with c++" )
mylist !! . add ( "Interview prep with java" )
mylist !! . add ( "data structures with c" )
mylist !! . add ( "data structures with java" )
// Set adapter to ListView
adapter = ArrayAdapter (
this , androidx . appcompat . R . layout . support_simple_spinner_dropdown_item ,
mylist !!
)
listView . adapter = adapter
search_bar . setOnQueryTextListener ( object : SearchView . OnQueryTextListener {
override fun onQueryTextSubmit ( query : String ): Boolean {
if ( mylist !! . contains ( query )) {
adapter !! . filter . filter ( query )
} else {
Toast . makeText ( this @MainActivity , "No Match found" , Toast . LENGTH_LONG ). show ()
}
return false
}
override fun onQueryTextChange ( newText : String ): Boolean {
return false
}
})
}
}
Output: For learning more about the topic refer to this topic: SearchView in Android with RecyclerView
Git Link: Click Here to get the full Project for SearchView
Similar Reads
TextView in Android with Example TextView is a simple widget that is seen in every android application. This widget is used to display simple text within the android application. We can add custom styling to the text that we have to show. In this article, we will take a look at How to create a simple Text View in an android applica
2 min read
PhotoView in Android with Example In this article, PhotoView is added to android. PhotoView aims to help produce an easily usable implementation of a zooming Android ImageView using multi-touch and double-tap. Besides that, it has many more features like it notifying the application when the user taps on the photo or when the displa
2 min read
TextView widget in Android with Examples Widget refers to the elements of the UI (User Interface) that help the user interact with the Android App. TextView is one of many such widgets which can be used to improve the UI of the app. TextView refers to the widget which displays some text on the screen based on the layout, size, colour, etc
5 min read
TextView widget in Android with Examples Widget refers to the elements of the UI (User Interface) that help the user interact with the Android App. TextView is one of many such widgets which can be used to improve the UI of the app. TextView refers to the widget which displays some text on the screen based on the layout, size, colour, etc
5 min read
SearchView in Android with Kotlin SearchView is a widget in android which provides a search interface with the help of which users can be able to make searches within the given list of data. In this search view, the user has to specify the search query. According to the search, query results will be populated within the listview. In
4 min read
How to Implement Custom Searchable Spinner in Android? Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it. The default value of the android spinner will be the c
5 min read