Android SearchView to Filter Listview items

Android SearchView Video Tutorial

Almost every application requires search functionality in their app to quickly search for particular content or information.

In this video, we will learn how to add SearchView to our android application. We will see how to filter ListView items using SearchView.

Activity_main.xml

So first we will design our layout and then we move to the implementation part. Let’s get started with a new project.

I have used a Linear layout in our XML file and we require SearchView to search content in our application. Also, I have used ListView which contains items so that we can filter particular items with the help of SearchView.

Below I have provided the full source code of our Activity_main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
    android:orientation="vertical"
    android:padding="30dp">

    <SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:queryHint="Search"
        android:iconifiedByDefault="false"/>

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"/>

</LinearLayout>

MainActivity.java

Now we will move to our implementation part, so we will declare SearchView and ListView in our main activity.

SearchView searchView;
ListView listView;

Thereafter, we will add different items in our ListView that we created. So to add items we use List to store all these items and we require an Adapter to show the list items in our ListView.

ArrayList<String> list;
ArrayAdapter<String> adapter;

With the help of ArrayList, we will add items to our list, Let’s say I want to add all months, but you can use anything as per your requirement.

list = new ArrayList<String>();
list.add("January");
list.add("February");
list.add("March");
list.add("April");
list.add("May");
list.add("June");
list.add("July");
list.add("August");
list.add("September");
list.add("October");
list.add("November");
list.add("December");

Now we will use Adapter to bind all these items and show in our ListView with the help of setAdapter() method.

adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);

listView.setAdapter(adapter);

We are now done with our List part, we will now implement SearchView which is our main focus of this tutorial.

Here we will use a listener for SearchView that is, setOnQueryTextListener() – this sets a listener for user inputs inside SearchView. Same as when we click on a button or typing some text.

We have two methods inside OnQueryTextListener() that is:

  • onQueryTextSubmit(String s) – This performs an action when you type the entire text
  • onQueryTextChange(String s) – This will listen each character you type in your SearchView

So we will use it here onQueryTextChange(String s). Inside this method we use getFilter().filter(s) method to set data from SearchView to our ListView.

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;
            }
        });

Finally, we are now ready to run our application to see the final result. For a complete video tutorial, you can check my YouTube channel. I have provided the full source code below so that you can try and practice in your android studio.

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<String> list;
    ArrayAdapter<String> 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);

        list = new ArrayList<String>();

        list.add("January");
        list.add("February");
        list.add("March");
        list.add("April");
        list.add("May");
        list.add("June");
        list.add("July");
        list.add("August");
        list.add("September");
        list.add("October");
        list.add("November");
        list.add("December");

        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);

        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;
            }
        });

    }
}

Environment

I have used the below platform to RUN this application however, you can also use windows, the old android studio version.

  • Android Studio Version 4.1.1
  • Emulator: Android Studio’s emulator
  • Operating System: ubuntu 16.04 LTS | 64-bit

So that is all guys for this tutorial, I hope you find this helpful. Let me know in the comment section if you have any questions regarding this example or if you can visit or contact us for any suggestions, I will try to answer as per my knowledge.

Sharing Is Caring:

I'm a Computer Science graduate. I'm passionate about blogging and I owned codewithap.com to share some of my interests with other people.