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 Comments

  1. wasim February 2, 2014 at 3:28 pm

    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 ?

     
  2. Danilo April 3, 2014 at 3:17 pm

    That’s great man, works to me <3

     
  3. payal April 5, 2014 at 6:54 am

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

     
  4. alester June 30, 2014 at 2:39 pm

    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

     
  5. Svenson July 2, 2014 at 5:19 pm

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

     
  6. rume October 19, 2014 at 8:31 pm

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

     
    • James October 23, 2014 at 8:29 am

      check the arg0 parameter in the click function.

       
  7. Jyotman September 3, 2015 at 5:58 pm

    Thanks! Very useful…

     
    • Lawrence October 25, 2017 at 7:37 am

      Where to download the Cheeses?

       
      • James October 27, 2017 at 5:37 pm

        Lawrence,

        I have update the code. Please check.

         

Leave a Reply

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

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.