How to create a SearchView with Filter mode in a ListView in Android?

By | June 1, 2013

Actually this is fairly simple.
Android by default provides a SearchView class that has the ability to filter.

Filter ListView

Just look at the XML layout that I am using in this post.
It consists of a SearchView and a ListView. The searchView searches the listview for the matched content.
Click on the link below to download the code.

Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <SearchView
            android:id="@+id/search_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    <ListView
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

</LinearLayout>


Android Source Code

Now the Java class or the activity that implements the searchFilter.

package com.coderzheaven.searchviewwithfilter;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;

/**
 * Shows a list that can be filtered in-place with a SearchView in non-iconified mode.
 */
public class SearchViewFilterMode extends Activity 
                         implements SearchView.OnQueryTextListener {

    private SearchView mSearchView;
    private ListView mListView;

    private final String[] mStrings = { "Google", "Apple", "Samsung", "Sony", "LG", "HTC" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

        setContentView(R.layout.searchview_filter);

        mSearchView = (SearchView) findViewById(R.id.search_view);
        mListView = (ListView) findViewById(R.id.list_view);
        mListView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                mStrings));
        mListView.setTextFilterEnabled(true);
        setupSearchView();
    }

    private void setupSearchView() {
        mSearchView.setIconifiedByDefault(false);
        mSearchView.setOnQueryTextListener(this);
        mSearchView.setSubmitButtonEnabled(true); 
        mSearchView.setQueryHint("Search Here");
    }

    public boolean onQueryTextChange(String newText) {
        if (TextUtils.isEmpty(newText)) {
            mListView.clearTextFilter();
        } else {
            mListView.setFilterText(newText.toString());
        }
        return true;
    }

    public boolean onQueryTextSubmit(String query) {
        return false;
    }
}

Click to download the source code from here.

10 thoughts on “How to create a SearchView with Filter mode in a ListView in Android?

  1. wasim

    hey man you have done great job. this is what i need it.
    and one more question this search view will work with action bar ?

    Reply
  2. payal

    Such it’s wonderul. And it’s named is totally true it’s code is really proved for me the heaven

    Reply
  3. alester

    This is great, but a quick question is there a toggle to switch off the black box that appears over the list echoing the search query?

    Thanks

    Reply
  4. Svenson

    Nice! This code helped me!
    Just one question though.
    How would one disable the translucent box with the search text appearing in it?

    Reply
  5. rume

    how i will get array index position for ontiem click or for going to another activity by clicking array listed values

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *