Monday, 26 August 2013

Android Search Functionality with ListView


 In this tutorial i am showing that how to Adding search functionality to ListView and how can  filters the list data with a matching string,which provides user an easy way to find the information he needs from list. 

Why we need this search box with ListView?

Lets Suppose we have various item attached with listview,it may be more than 1k.So in this case we use search box to find the required item from listview,because this is not possible to scroll down the whole list to search the that item.
Lets takes a simple example to show the core concept behind this.
  • Create a new project in Eclipse File > New > Android Application Project and fill the required details.
  • Create required files needed to generate a listview. I am using my default activity_main.xml as listview and created a new xml file for single listitem that is  mylist.xml. Also creating a EditText acting as a search box in this example.
Here is activity_main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >
    <EditText android:id="@+id/Search_box"
        android:hint="Search a Item from ListView"
        android:layout_height="wrap_content" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >
    <TextView android:id="@+id/List_item"
  •  Its the time to write the java code, open your and paste the following code to create a simple ListView. I am puting all the list data in an array called listElements[] and here ArrayAdapter is used to attached the listview.

package com.example.robi;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

    private ListView list;

    ArrayAdapter<String> adapter;

    EditText edtSearch;

    ArrayList<HashMap<String, String>> productList;

    public void onCreate(Bundle savedInstanceState) {

        // Data to add into listview
        String listElements[] = { "Audi", "Frari", "Fortuner", "Bugatti",
                "Hennessey Venom", "Boster-b1", "Verna", " McLaren F1", "I-20",
                "Hypersport", "Koenigsegg Agera ", "Mercedes-Benz" };

        list = (ListView) findViewById(;
        edtSearch = (EditText) findViewById(;

        // Now Adding items to listview
        adapter = new ArrayAdapter<String>(this, R.layout.mylist,
      , listElements);

         * below i am Enabling the Search Filter
         * */
        edtSearch.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence cs, int arg1, int arg2,
                    int arg3) {
                // When user changed the Text

            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
                // TODO Auto-generated method stub


            public void afterTextChanged(Editable arg0) {


How to Enable the Search Functionality 
 We have to add a  addTextChangedListener to EditText.when we enter some thing new to EditText we need to get the text from it and passing it to array adapter filter.  
inputSearch.addTextChangedListener(new TextWatcher() {
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
    public void afterTextChanged(Editable arg0) {
  •  Adding properties in AndroidManifest.xml file.
 <manifest xmlns:android=""
    android:versionName="1.0" >

        android:targetSdkVersion="15" />

        android:theme="@style/AppTheme" >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

  •  To run the application, right click on the project -> Run As -> Android Application. You should see following result in your device. 


Please Share this tutorial On: