Android Search ListView using Filter

In this tutorial, you will learn how to implement a search functionality in a listview using filters in your Android application. A listview can be filtered by the user input and is enabled using addTextChangedListener method. The search function will filter the listview with a matching string from the user input. Searching through the listview provides users an easy way to find the information they needed. We will create a listview with an edittext placed on top and on text input will filter the results and on listview item click will open a new activity. So lets begin…

Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project FilterListViewTutorial.

Application Name : FilterListViewTutorial

Project Name : FilterListViewTutorial

Package Name : com.androidbegin.filterlistviewtutorial

Open your MainActivity.java and paste the following code.

MainActivity.java

In this activity, we have created string arrays with sample data and pass it into the ListViewAdapter class. On listview item click will pass the selected position and string arrays to a new activity. An EditText will capture user input as a search parameter and pass it to the filter function in ListViewAdapter.

Next, create an array class. Go to File > New > Class and name it WorldPopulation.java. Select your package named com.androidbegin.filterlistviewtutorial and click Finish.

Open your WorldPopulation.java and paste the following code.

WorldPopulation.java

Next, create an XML graphical layout for your MainActivity. Go to res > layout > Right Click on layout > New > Android XML File
Name your new XML file listview_main.xml and paste the following code.

listview_main.xml

Output:
Filter ListView XML Main

Next, create a ListViewAdapter class. Go to File > New > Class and name it ListViewAdapter.java. Select your package named com.androidbegin.filterlistviewtutorial and click Finish.

Open your ListViewAdapter.java and paste the following code.

ListViewAdapter.java

In this custom listview adapter class, string arrays are passed into the ListViewAdapter and set into the TextViews followed by the positions. On listview item click will pass the string arrays and position to a new activity. The filter class captures the user input text from the MainActivity and refreshes the listview to show a matched result.

Next, create an XML graphical layout for your listview item. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file listview_item.xml and paste the following code.

listview_item.xml

Output:

Filter ListView XML Item

Next, create an activity to display results. Go to File > New > Class and name it SingleItemView.java. Select your package named com.androidbegin.filterlistviewtutorial and click Finish.

Open your SingleItemView.java and paste the following code.

SingleItemView.java

In this activity, strings are retrieved from the ListViewAdapter by using Intent and sets into the TextViews.

Next, create an XML graphical layout for your SingleItemView. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file singleitemview.xml and paste the following code.

singleitemview.xml

Next, change the application name and texts. Open your strings.xml in your res > values folder and paste the following code.

strings.xml

In your AndroidManifest.xml, declare an activity we have created above. Open your AndroidManifest.xml and paste the following code.

AndroidManifest.xml

Output:

Filter ListView ScreenShot

Source Code:

FilterListViewTutorial (613.1 KiB, 806 downloads)
  • dhaval

    what if i have to remove one cell from list view??

  • santosh2201

    Awesome tutorial. Thank you

  • Karabo Slasheth Mithi

    // Not using options menu in this tutorial
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;

    Error: activity_main cannot be resolved or is not a field

    • Karabo Slasheth Mithi

      Fixed with:

      getMenuInflater().inflate(R.layout.activity_main, menu)

  • Karabo Slasheth Mithi

    I had a problem with:
    getMenuInflater().inflate(R.menu.activity_main, menu);

    and rectified the problem with:
    getMenuInflater().inflate(R.menu.main, menu);

  • Ran Van

    Superb dude!…

  • Kelvin Yonga

    I am implementing the same on a dialog and the list view is not being displayed neither the app is crushing.what could be the issue ?…bellow is my code

    final Dialog dialog = new Dialog(getActivity());

    dialog.setContentView(R.layout.manualsalesearch);

    dialog.setTitle(“Product Name”);

    String[] rank = new String[] { “1″, “2″, “3″, “4″, “5″, “6″, “7″, “8″, “9″, “10″ };

    String[] country = new String[] { “China”, “India”, “United States”,

    “Indonesia”, “Brazil”, “Pakistan”, “Nigeria”, “Bangladesh”,

    “Russia”, “Japan” };

    ArrayList arraylist = new ArrayList();

    for (int i = 0; i < rank.length; i++)

    {

    productsearchobject wp = new productsearchobject(rank[i], country[i]

    );

    // Binds all strings into an array

    arraylist.add(wp);

    }

    final ListView list = (ListView) dialog.findViewById(R.id.list_view);

    final EditText inputSearch = (EditText) dialog.findViewById(R.id.inputSearch);

    final productsearchAdaptor adapter ;

    Toast.makeText(getActivity(), arraylist.size()+"size", Toast.LENGTH_LONG).show();

    adapter = new productsearchAdaptor(getActivity(), arraylist);

    list.setAdapter(adapter);

    inputSearch.addTextChangedListener(new TextWatcher() {

    @Override

    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

    // TODO Auto-generated method stub

    }

    @Override

    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,

    int arg3) {

    // TODO Auto-generated method stub

    }

    @Override

    public void afterTextChanged(Editable arg0) {

    String text = inputSearch.getText().toString().toLowerCase(Locale.getDefault());

    adapter.filter(text);

    // TODO Auto-generated method stub

    }

    });

    dialog.show();

    }

    });

    return rootView;

    }

    }