Filterable ListView in Android

June 14, 2012
By

In this article, we are going to create a filterable listview application in Android. Filterable listview means that, whenever user types in some text, correspondingly the items in the listview will be filtered.


1. Create an Android project namely “ListViewFilterable”

Create new Android Project

Figure 1: Create new Android Project


2. Select a build target for this application

Android API Level

Figure 2: Select Android API Level


3. Enter the application details

Application Details

Figure 3: Enter application details


4. Open and update the file /res/layout/main.xml with the given below code :


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />

</LinearLayout>


5. Open and update the file /src/in/wptrafficanalyzer/listviewfilterable with the given below code :

package in.wptrafficanalyzer.listviewfilterable;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
    String[] countries = new String[] {
        "India",
        "Pakistan",
        "Sri Lanka",
        "China",
        "Bangladesh",
        "Nepal",
        "Afghanistan",
        "North Korea",
        "South Korea",
        "Japan"
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Creating an array adapter to host the list view items
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);

        // Get the reference of listview from the main layout
        ListView listView = (ListView) findViewById(R.id.listview);
        listView.setAdapter(adapter);

        // This statement enables text filtering in the listview
        listView.setTextFilterEnabled(true);

    }
}


 

6. Running the application

Filterable ListView in execution

Figure 4: Filterable ListView in execution


7. Download the source code


8. Testing this application

Testing of this application is discussed in the article “Testing Filterable ListView in Android


8. Reference

http://developer.android.com/guide/index.html


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags:

2 Responses to Filterable ListView in Android

  1. Ammar on January 6, 2013 at 3:17 am

    How are you able to type on the screen? Don’t you need an EditText?

  2. Naren Lee on April 23, 2014 at 12:41 pm

    How to do filtering the listview populate with mysal database data having baseAdapter. If you can please help me.

Leave a Reply

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

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner