Android SearchView Example Tutorial using DataBinding

Filed Under: Android

Today we will look into Android SearchView widget and develop an application that filters a ListView by the queried text. We’ll be using DataBinding to hook up the layouts in the Activities and Adapters. If you haven’t read about DataBinding refer this tutorial first for a better understanding.

Android SearchView

Android allows us to use the search functionality in our app by displaying the SearchView widget either in the ToolBar/ActionBar or inserting it into a layout. Android SearchView widget is available from Android 3.0 onwards.

The SearchView is defined in the XML layout as shown below.

        android:layout_height="wrap_content" />

There are many forms for searching in Android such as voice search, suggestions etc. In this tutorial we’ll use SearchView.OnQueryTextListener and Filterable interfaces.

The Filterable interface filters the queried text over a ListView and displays the resulted ListView rows.

OnQueryTextListener interface can detect two events.

  1. onQueryTextChange is called when the user types each character in the text field
  2. onQueryTextSubmit is triggered when the search is pressed

Android SearchView Example

Below image shows the final android SearchView example project.

android search view, android SearchView, android SearchView Example

The project consists of an Activity and an Adapter for the ListView.

Android SearchView Example Code

The activity_main.xml is given below. It consists of a ListView with a SearchView on top.


<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="">


            android:clickable="true" />

            android:layout_below="@+id/search" />



The is given below.

package com.journaldev.searchview;

import android.databinding.DataBindingUtil;
import android.os.Bundle;

import com.journaldev.searchview.databinding.ActivityMainBinding;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding activityMainBinding;
    ListAdapter adapter;

    List<String> arrayList= new ArrayList<>();

    protected void onCreate(Bundle savedInstanceState) {
        activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);


        adapter= new ListAdapter(arrayList);
        activityMainBinding.listView.setAdapter(adapter);;"Type your keyword here");;;; SearchView.OnQueryTextListener() {
            public boolean onQueryTextSubmit(String query) {
                return false;

            public boolean onQueryTextChange(String newText) {


                return false;

In the above code, we are passing an ArrayList of Months to the List Adapter.

We’re invoking the filter method that’s defined in the adapter class every time the search query text changes.

The class looks like this.

package com.journaldev.searchview;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import com.journaldev.searchview.databinding.RowItemBinding;
import java.util.ArrayList;
import java.util.List;

public class ListAdapter extends BaseAdapter implements Filterable {

    List<String> mData;
    List<String> mStringFilterList;
    ValueFilter valueFilter;
    private LayoutInflater inflater;

    public ListAdapter(List<String> cancel_type) {
        mStringFilterList = cancel_type;

    public int getCount() {
        return mData.size();

    public String getItem(int position) {
        return mData.get(position);

    public long getItemId(int position) {
        return position;

    public View getView(int position, View convertView, final ViewGroup parent) {

        if (inflater == null) {
            inflater = (LayoutInflater) parent.getContext()
        RowItemBinding rowItemBinding = DataBindingUtil.inflate(inflater, R.layout.row_item, parent, false);

        return rowItemBinding.getRoot();

    public Filter getFilter() {
        if (valueFilter == null) {
            valueFilter = new ValueFilter();
        return valueFilter;

    private class ValueFilter extends Filter {
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();

            if (constraint != null && constraint.length() > 0) {
                List<String> filterList = new ArrayList<>();
                for (int i = 0; i < mStringFilterList.size(); i++) {
                    if ((mStringFilterList.get(i).toUpperCase()).contains(constraint.toString().toUpperCase())) {
                results.count = filterList.size();
                results.values = filterList;
            } else {
                results.count = mStringFilterList.size();
                results.values = mStringFilterList;
            return results;


        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            mData = (List<String>) results.values;



As you can see in the above code we’re performing filtering using an inner class ValueFilter that extends the Filter class. It filters the list by checking if the search query text matches the strings given in the ArrayList.

The XML layout for the ListView row is given below.


<layout xmlns:android="">


            android:textAppearance="?android:attr/textAppearanceMedium" />



The output of the android search view application in action is given below.

android search view output, android searchview

The SearchView shown above requires us to press the search icon to activate the text field. Also, it doesn’t contain any hint/placeholder text.

Add the following code in the MainActivity to enable the SearchView by default and display a hint.;"Type your keyword here");;;;

The SearchView after customising as above looks like this.
android search view example

This brings an end to android SearchView tutorial. We’ll dive into the advanced features of SearchView in a later tutorial. You can download the Android SearchView Project from the link given below.


  1. belas says:

    hi sir , how can i make searchView with tablelayout or tableRow?

  2. Tony says:

    Are you comedy me with name of Data Binding and SearchView Widget? 😀 😛

  3. ArunAjayan says:

    Sir, is there anyway to change text color and hint color of searchview?
    Please reply.

  4. Hud says:

    Can I use this with RecyclerView?

  5. Akhil says:

    ActivityMainBinding ?

  6. tushar says:

    how to add search view webview search any website how to apply ??

  7. Amitabh Bhandari says:

    Great tutorial!! I would like to know one thing… For E.g..I have two separate lists in separate activities ..months and weekdays.. and implement a searchview as suggested here.. it searches for the months perfectly.. and the other activity searches for weeks perfectly.. what if i want to add a searchview wherein boththe lists (months & weekdays) can be searched.. i.e if the user searches JANUARY or MONDAY the list should filter and show the results.. how to do this when the list are in separate activities… Your reply in this regard… will be greatly appreciated….

  8. Stacy says:

    thank you for your sharing great information to about DataBinding. its really great post for us. this the good blog site. good job.

    1. Anupam says:

      Glad it helped you Stacy.

  9. Chris H says:

    Getting some errors with the ListAdapter section:

    “RowItemBinding rowItemBinding = DataBindingUtil.inflate(inflater, R.layout.row_item, parent, false);

    > The second sentence with “.get(position)”. Set Text wants to pull a string. Position is an Int. Casting the parameter to Int ‘works’ but when launching the app to test, I get a Fatal Exception stating String cannot be cast to Integer. So it doesn’t work.

    Any suggestions here?

    1. Anupam says:

      Nice observation Chris.
      Change this :

  10. luis says:

    How can i make that search result will open new activity when i click on it.

  11. Alee says:

    ThanQu… Bro Doing Well.

  12. Lex Luther says:

    How can I apply this to a csv adapter that puts the inputs into an arraylist?

  13. Nens Patel says:

    Where is code of ActivityMainBinding Class ?

    1. Anupam says:

      By default, a Binding class will be generated based on the name of the layout file, converting it to Pascal case and suffixing “Binding” to it. The above layout file was activity_main.xml so the generate class was ActivityMainBinding

      If your xml name is main_activity.xml than DataBinding class name will be MainActivityBinding.

      Take note that data binding is enabled in the build.gradle file.
      The tutorial will be updated to mention this explicitly.


  14. khushbu says:

    Can someone pls help me out with where is d code for

    Not able to complete this tutorial due to this

    1. Anupam says:

      By default, a Binding class will be generated based on the name of the layout file, converting it to Pascal case and suffixing “Binding” to it. The above layout file was activity_main.xml so the generate class was ActivityMainBinding

      If your xml name is main_activity.xml than DataBinding class name will be MainActivityBinding.

      Take note that data binding is enabled in the build.gradle file.
      The tutorial will be updated to mention this explicitly.


      1. Hashapi says:

        Where is the binding class generated

  15. DM says:

    how make project listview with text, image, and when click the listview we can see other activity ?

  16. ERIK says:

    CAN YOU ADD THE CODE FOR THE MISSING: com.journaldev.searchview.databinding.RowItemBinding

    1. ERIK says:

      never-mind I just saw that it was a binding

      1. Davide Giuseppe Farella says:

        Can you help me to fix it?

  17. Walid says:

    Thaaaaaaaank You , Very helpfull

    1. Anupam says:

      Glad that it was helpful to you Walid.

  18. Pandastein says:

    Hi, I’m looking for implement this sort of searchview but I don’t want to show the original listview’s datas. I just wanna display the results only in the listview. Is it possible to implement that ?


  19. alb says:

    Hi! Thanks for sharing the code that helps a lot, I have a question is it ossible to add onClickListeners to the every single one of the item in the list?

    1. Anupam says:

      Inside the onItemClickListener you can add custom behaviours for each row based on the id.

  20. Bad says:

    ActivityMainBinding ?

    1. cristopher says:

      This is fine in XML:

      However, this is the correct JAVA

      SearchView search = (SearchView) findViewById(;
      search.setQueryHint(“Type your keyword here”);

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content