Open In App

SearchView in Android with ListView

Last Updated : 08 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

SearchView is a widget provided by the Android framework that allows users to search for specific data within an application. It is commonly used in apps that have large amounts of data or content that can be searched, such as contacts, music, or emails.

The SearchView widget consists of an input field and a search button. Users can enter a search query into the input field, and then click the search button to initiate the search. The search query can be entered using the device’s keyboard, and the search results are displayed in the same activity or fragment as the SearchView.

ListView

ListView is a UI component provided by the Android framework that displays a list of items in a vertical scrollable view. It is commonly used in Android applications to display data that can be scrolled vertically, such as a list of contacts, messages, or news articles.

Step-by-Step Implementation

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. Note that select Java as the programming language.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:queryHint="Search Contacts"
        android:iconifiedByDefault="true" />
  
    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
  
</LinearLayout>


Step 3: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java




package com.anas.gfgsearchview;
  
import androidx.appcompat.app.AppCompatActivity;
  
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
  
import java.util.ArrayList;
  
public class MainActivity extends AppCompatActivity {
  
    SearchView searchView;
    ListView listView;
  
    ArrayList arrayList;
    ArrayAdapter adapter;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        searchView=findViewById(R.id.searchView);
        listView=findViewById(R.id.listView);
  
        arrayList=new ArrayList();
        arrayList.add("Anas");
        arrayList.add("Aman");
        arrayList.add("Shruti");
        arrayList.add("Palki");
        arrayList.add("Nikhil");
        arrayList.add("Varun");
        arrayList.add("Avinash");
        arrayList.add("Subham");
        arrayList.add("Abhishek");
        arrayList.add("Sayantan");
        arrayList.add("Siddharth");
        arrayList.add("Abhinav");
        arrayList.add("Viplav");
        arrayList.add("Puneet");
        arrayList.add("Tarzan");
        arrayList.add("Badshah");
        arrayList.add("Jake");
  
        adapter=new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1,arrayList);
  
        listView.setAdapter(adapter);
  
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                adapter.getFilter().filter(query);
                return false;
            }
  
            @Override
            public boolean onQueryTextChange(String newText) {
                adapter.getFilter().filter(newText);
                return false;
            }
        });
  
    }
}


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads