Skip to content
Related Articles

Related Articles

Android SearchView with Example

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Hard
  • Last Updated : 18 Feb, 2021

Search View widget is used to provide a search interface to user so that the user can enter his search query and submit a request to search provider and get a list of query suggestions or results.

Class Syntax:

public class SearchView
extends LinearLayout
implements CollapsibleActionView

Class Hierarchy:

  ↳  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 search view and list view. User will type search query in search view which is present in action bar. Here are explained steps:

  • Step 1: activity_main.xml

    It consist of Relative Layout with ListView in it from which data is to be search. Here is complete code for activity_main.xml:


    <?xml version="1.0" encoding="utf-8"?>
    <!--Relative Layout-->
        <!--List View from which data is to be searched-->

  • Step 2: menu.xml

    Create menu.xml and add search view as menu item to Action Bar with title as “Search” this file. Here is complete code for menu.xml file.


    <?xml version="1.0" encoding="utf-8"?>  
        <!--Search view widget as item in menu-->

  • Step
    Next step is to set up the code for main activity. In this java file items are added to List View manually and setOnQueryTextListener is attached to Search View. onQueryTextSubmit() method is overriden in which List View get filter according to the search query entered by the user.Here is complete code for

    package com.geeksforgeeks.searchviewwidget;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.os.Bundle;
    import android.widget.ListView;
    import android.widget.ArrayAdapter;
    import android.widget.SearchView;
    import java.util.ArrayList;
    public class MainActivity extends AppCompatActivity {
        // List View object
        ListView listView;
        // Define array adapter for ListView
        ArrayAdapter<String> adapter;
        // Define array List for List View data
        ArrayList<String> mylist;
        protected void onCreate(Bundle savedInstanceState)
            // initialise ListView with id
            listView = findViewById(;
            // Add items to Array List
            mylist = new ArrayList<>();
            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
                = new ArrayAdapter<String>(
        public boolean onCreateOptionsMenu(Menu menu)
            // Inflate menu with items using MenuInflator
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(, menu);
            // Initialise menu item search bar
            // with id and take its object
            MenuItem searchViewItem
                = menu.findItem(;
            SearchView searchView
                = MenuItemCompat
            // attach setOnQueryTextListener
            // to search view defined above
                new SearchView.OnQueryTextListener() {
                    // Override onQueryTextSubmit method
                    // which is call
                    // when submitquery is searched
                    public boolean onQueryTextSubmit(String query)
                        // If the list contains the search query
                        // than filter the adapter
                        // using the filter method
                        // with the query as its argument
                        if (list.contains(query)) {
                        else {
                            // Search query not found in List View
                                          "Not found",
                        return false;
                    // This method is overridden to filter
                    // the adapter according to a search query
                    // when the user is typing search
                    public boolean onQueryTextChange(String newText)
                        return false;
            return super.onCreateOptionsMenu(menu);


  • Main Activity with search bar and list
  • Search results according to search query

  • Search results

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!