Android: how to filter a ListView based on an EditText field

Android has very good support for displaying lists via the Android ListView class.

A typical setup for a list is that you want to have a filter field on the top of your list and this input into this field should filter the content of the list. Here is a small example showing how to do this.

Create for this example the project “de.vogella.android.list.filter” with the activity “ListFilterDemoActivity”. Create the following layout:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<EditText android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:id="@+id/filterText">
		<requestFocus></requestFocus>
	</EditText>
	<ListView android:layout_height="wrap_content" android:id="@+id/android:list"
		android:layout_width="fill_parent"></ListView>
</LinearLayout>

Create your adapter class.

package de.vogella.android.list.filter;

import java.util.ArrayList;
import java.util.List;

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

public class ListFilterDemoActivity extends ListActivity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, android.R.id.text1,
				getModel());
		setListAdapter(adapter);
		EditText filterEditText = (EditText) findViewById(R.id.filterText);
		filterEditText.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				adapter.getFilter().filter(s.toString());
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});
	}

	private List<String> getModel() {
		List<String> list = new ArrayList<String>();
		list.add("Linux");
		list.add("Windows7");
		list.add("Suse");
		list.add("Eclipse");
		list.add("Ubuntu");
		list.add("Solaris");
		list.add("Android");
		list.add("iPhone");
		return list;
	}
}

You find more information about ListViews in the Android ListView Tutorial

I hope this helps.

You find me also on Twitter and .

Update: Change to use the standard filter of adapter. Thanks Charles Zhang for the suggestion.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, He is the primary author of vogella.com. With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.
This entry was posted in Android and tagged , . Bookmark the permalink.

One Response to Android: how to filter a ListView based on an EditText field

  1. Justin says:

    Just came across this, very useful for my first app I am building, thanks!

Comments are closed.