Android ListView → Android Simple ListView – Search/Filter – Camposha

Android ListView

Android ListView

Android ListView

Android Simple ListView – Search/Filter

Android Simple ListView Search/Filter Tutorial and Example.

How to search or filter a simple ListView in android.

Google search engine is the most visited website in the whole world. This tells us something about us humans and our need to always search and filter through information.

The worldwide web is really vast, and we cannot be able to use it conveniently by just remembering website urls or going through portals to find the content we want.

Instead we simply search. Now today we are not developing another search engine. Instead we look at a simple example in android, how to search through a list using searchview. Call it search or filter, but we find the information we want by typing it in the searchview. SearchView is a widget that was added to android in API 11.

It provides s imple edittext where user can enter a query and submit it to the search provider. For us we simply search through a simple listview using searchview.

Project Demo

Common Questions this example explores

  • Android SearchView example.
  • How to search a simple listview in android.
  • Android SearchView arrayadapter example.
  • How to search/filter in android.

This example was written with the following tools:

  • Windows 8.1
  • Eclipse IDE
  • Java Language
  • Bluestacks Emulator

Lets have a look at the source code.

MainActivity.java

Our MainActivity Class

Purpose :

  1. Reference our ListView and SearchView from xml
  2. Bind our data to ListView using ArrayAdapter.
  3. Handle our SearchView’s onQueryTextChangeListener.
  4. Filter or Search dynamically
package com.tutorials.simplelistviewfilter;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;

public class MainActivity extends Activity {

  ListView lv;
  SearchView sv;

    String[] teams={"Man Utd","Man City","Chelsea","Arsenal","Liverpool","Totenham"};
  ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv=(ListView) findViewById(R.id.listView1);
        sv=(SearchView) findViewById(R.id.searchView1);

        adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,teams);
        lv.setAdapter(adapter);

        sv.setOnQueryTextListener(new OnQueryTextListener() {

      @Override
      public boolean onQueryTextSubmit(String text) {
        // TODO Auto-generated method stub
        return false;
      }

      @Override
      public boolean onQueryTextChange(String text) {

               adapter.getFilter().filter(text);

        return false;
      }
    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

ActivityMain.xml

Our ActivityMain layout

Purpose

  1. Basically display whatever widgets we want to use,in this case ListView and SearchView.
<RelativeLayout 
    
    android_layout_width="match_parent"
    android_layout_height="match_parent"
    android_paddingBottom="@dimen/activity_vertical_margin"
    android_paddingLeft="@dimen/activity_horizontal_margin"
    android_paddingRight="@dimen/activity_horizontal_margin"
    android_paddingTop="@dimen/activity_vertical_margin"
    tools_context=".MainActivity" >

    <SearchView
        android_id="@+id/searchView1"
        android_layout_width="match_parent"
        android_layout_height="wrap_content"
        android_layout_alignParentLeft="true"
        android_layout_alignParentTop="true"
        android_queryHint="Search.."
         >

    </SearchView>

    <ListView
        android_id="@+id/listView1"
        android_layout_width="match_parent"
        android_layout_height="wrap_content"
        android_layout_alignParentLeft="true"
        android_layout_below="@+id/searchView1"
        android_layout_marginTop="22dp" >
    </ListView>

</RelativeLayout>

Video/Demo

YouTube Video

How To Run

  1. This project was created by Eclipse.
  2. Therefore just copy the code and use in your project in android studio, it’s still android anyway and expandableListview is the same regardless of the IDE. So copy paste the code to your android studio project.

Leave a Reply

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

Join Us
X