What is search view in android?

AndroidMobile DevelopmentProgramming

Before getting into searchview example, we should know what is search view in android, search view is just like search box in HTML. we can search anything from particular list items.

This example demonstrate about how to integrate search view in android.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent">
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   android:orientation = "vertical">
      android:id = "@+id/search"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content" />
      android:id = "@+id/list"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"/>

In the above code we are giving search view and listview because search view going to search element from a list.

Step 3 − Add the following code to src/MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
   protected void onCreate(Bundle savedInstanceState) {
      final ListView list = findViewById(R.id.list);
      SearchView search = findViewById(R.id.search);
      search.setQueryHint("Type your keyword here");
      ArrayList<String> arrayList = new ArrayList<>();
      arrayList.add("C Language");
      arrayList.add("CPP Language");
      arrayList.add("Go Language");
      arrayList.add("AVN SYSTEMS");
      final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,                   android.R.layout.simple_list_item_1, arrayList);
      search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
         public boolean onQueryTextSubmit(String query) {
            return false;
         public boolean onQueryTextChange(String newText) {
            return false;
      list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String clickedItem = (String) list.getItemAtPosition(position);
            Toast.makeText(MainActivity.this, clickedItem, Toast.LENGTH_LONG).show();

In the above searchview we have given one listener as setOnQueryTextListener. It going to listen your text changes with two methods as shown below.

  • onQueryTextSubmit(String query) − After enter into the text in searchview if you want to do anything with that string you can do in this method.

  • onQueryTextChange(String newText) − after text change means while enter text into search view it going to do some action.

In the above code we have filter text while query changes as shown below

public boolean onQueryTextChange(String newText) {
   return false;

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  Play Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen

Programming languages

Now enter some text which is closely matches to listview items as shown below

Search Java

Click here to download the project code

Published on 05-Dec-2018 15:54:28