Articles → Android → Search Functionality Using SearchView In Android

Search Functionality Using SearchView In Android






What Is SearchView?





Example




  1. Add a listbox in layout file. The purpose of this listbox is to display the list of items.
  2. <?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"    
                      tools:context=".debug.Demo">
    	<ListView android:id="@+id/lView"        
                                android:layout_width="match_parent"   
                                android:layout_height="match_parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>


  3. Add a menu folder. For that
    1. Right-click on res folder
    2. Go to New → Android Resource Directory
    3. Picture showing the Android Resource Directory option in the context menu on the res folder
      Click to Enlarge

    4. A popup will appear, where we can select a folder of type menu
    5. Picture showing a popup window for adding new resource directory
      Click to Enlarge
    6. Once you click on Ok, a menu folder is created inside the res folder.
    7. Picture showing the menu folder added inside the res folder
      Click to Enlarge
  4. Add a resource file in the menu folder. For that
    1. Right-click on menu folder.
    2. Click on New → Menu Resource File.
    3. Picture showing adding the Menu resource file inside the menu folder
      Click to Enlarge

    4. A pop up window will appear where you can give the file name and click on Ok.
    5. Picture showing the popup window for adding the new resource file
      Click to Enlarge
  5. Add code in search_option.xml.
  6. <?xml version="1.0" encoding="utf-8"?>
    <menu
    	xmlns:android="http://schemas.android.com/apk/res/android"
    	xmlns:app="http://schemas.android.com/apk/res-auto">
    	<item android:id="@+id/search" android:title="Search"    app:showAsAction="always"    
            app:actionViewClass="android.widget.SearchView" />
    </menu>


  7. Add a code on onCreate method to bind listbox with data source.
  8. String fruitlist[] = {
      "apple",
      "banana",
      "orange",
      "cherry"
    };
    ListView listView = (ListView) findViewById(R.id.lView);
    adapter = new ArrayAdapter < String > (this, R.layout.support_simple_spinner_dropdown_item, fruitlist);
    listView.setAdapter(adapter);


  9. Override onCreateOptionsMenu and add a code to filter data
  10. @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.search_option, menu);
      MenuItem item = menu.findItem(R.id.search);
    
      SearchView searchView = (SearchView) item.getActionView();
      searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
          return false;
        }
    
        @Override
        public boolean onQueryTextChange(String s) {
          adapter.getFilter().filter(s);
          return false;
        }
      });
      return true;
    }



Output




Picture showing the output of Search functionality using searchview in android
Click to Enlarge



Picture showing the output of Search functionality using searchview in android
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, November 19, 2019

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250