Android Search ListView using Filter

  • 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
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(, 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(, menu);

    and rectified the problem with:
    getMenuInflater().inflate(, 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.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



    final ListView list = (ListView) dialog.findViewById(;

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

    final productsearchAdaptor adapter ;

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

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


    inputSearch.addTextChangedListener(new TextWatcher() {


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

    // TODO Auto-generated method stub



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

    int arg3) {

    // TODO Auto-generated method stub



    public void afterTextChanged(Editable arg0) {

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


    // TODO Auto-generated method stub





    return rootView;



  • Sean Campbell

    How can i make it search first letter such as if I typed the letter
    “N” only “Nigeria” would show up instead of “China”, “India”, “United states” and any other country with the letter “N”

    • 玉龍 陳

      Instead of

  • Mina Gabriel

    I have a question for you why when i add this.arraylist = worldpopulationlist; instead of this.arraylist.addAll(worldpopulationlist); that doesn’t work what is the difference between assigning the value with equal sign and add all in Java means

  • eddi

    How do i highlight the listview item and make it stay highlighted? I have commented the code for Listen for ListView Item Click so that when clicking listview item it won’t redirect to another view and i can see the highlight effect. I have tried android:choiceMode=”singleChoice” but it has no effect.

  • Toma Alexandru

    Thank you very much sir!

  • Sibtain

    I want to highlight the searched text in the listview. I changed the code for filter function to this. please have a look, it is not working

    public void filter(String charText) {

    View view = inflater.inflate(R.layout.listview_item, null);

    TextView countryTextView = (TextView) view.findViewById(;

    charText = charText.toLowerCase(Locale.getDefault());

    System.out.println(“country: ” + countryTextView.getText().toString());


    if (charText.length() == 0) {





    for (WorldPopulation wp : arraylist)


    // Find charText in wp

    int startPos = wp.getCountry().toLowerCase(


    int endPos = startPos + charText.length();

    if (startPos != -1)


    Spannable spannable = new SpannableString(wp.getCountry());

    ColorStateList blueColor = new ColorStateList(new int[][] { new int[] {}}, new int[] { Color.BLUE });

    TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null);

    spannable.setSpan(highlightSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


    // worldpopulationlist.add(wp);






  • Leo Garcia

    I have a question about the search listview , did one, but when looking for something eg position 5 , I click and I returned to position 0. As could do to make the search position 5 and click me back the same position 5th

  • Asfand yar khan

    Hi I am using listview for text and image from parse AndroidBegin tutorial and also implements this search filter method. But I got error while implementing this.
    java.lang.NullPointerException: Attempt to invoke virtual method ‘java.lang.String java.lang.String.toLowerCase(java.util.Locale)’ on a null object reference
    Can you please help me.

  • Muhammad Zia Shahid

    can i search with 2 params in listview like country with rank ?