Default ListView Filter

Many times we want to filter the ListView based on what user types and in Android, simple straightforward way to filter the list in Android is as follows:
1) Create Layout XML


2) Create Activity Class
It has got EditText and ListView. We want that as user will type in

EditText the ListView should get filter. So here is our Activity class:

private EditText filterText = null;
ArrayAdapter adapter = null;

protected void onCreate(Bundle savedInstanceState) {


    filterText = (EditText) findViewById(;

    adapter=new ArrayAdapter(this,

private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(Editable s) {

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

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {


protected void onDestroy() {

1) We create the adapter with default android simple_list_item_1 layout.
2) We want the list to be filtered as user will type in EditText, so, we created TextWatcher and added that as TextChangedListener in Line 11.
3) ListView Adapter classes come with a Filter object that can be used to perform the filtering automatically. We just need to pipe the input from the EditText into the Filter. You can check out on Line no. 30. Filter checks whether entered string contained in each item of String ArrayAdapter.

Running the app now should show previous ListView, with a nice text box above. As user will type into that textbox, the list will get filtered.

1) It only works with String Array adapter. If we have our own Custom array adapter then it will not work.
2) We cannot apply our own filter. Default filter filters the List in “contains” fashion. But if we want our own filter (for eg: filtering list in “starts with fashion”) we cannot do that.

Ofcourse, there is trick to overcome both this limitation :-). Please check next tutorial to check how to overcome this limitation.