[Android] Allowing the user to enter a filter for a ListView

Back in Using an ArrayAdapter to Control a ListView’s Data, I wrote about filtering a ListView using its setTextFilterEnabled method. One of the comments reminded me that that not all devices have a physical keyboard and the virtual keyboard does not always show when entering the filter. For devices that do not have a physical keyboard, you could ask the InputMethodManager to display the virtual keyboard:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(getListView(), 0);


To see this in action, have a look at the VirtualKeyboardFilter activity (the full source code is available from the GitHub repostiory). When started, the activity will display a list of items with the virtual keyboard. The filter can be entered via the virtual keyboard.

The list is popuplated and the filter is turned on in the onCreate method:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // This array list will be updated each time.
    final ArrayList<String> data = new ArrayList<String>();
    data.add("apple");
    data.add("banana");
    data.add("grape");
    data.add("orange");
    data.add("pear");
    data.add("peach");
    data.add("tomato");

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
        R.layout.item, R.id.itemName, data);

    setListAdapter(dataAdapter);
    getListView().setTextFilterEnabled(true);
}

The virtual keyboard is displayed when the window gains focus:

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        getListView().post(new Runnable() {

            @Override
            public void run() {
                showKeyboard();
            }
        });
    }
}

Notice that the keyboard is being opened on the UI thread. I found that if the virtual keyboard does not show if the request was made straight away. Post will add the Runnable to the queue of things to do on the UI thread.

Disabling keyboard support in the emulator

While running this demo in the emulator with Android 4, I noticed that the virtual keyboard still does not necessarily show. This was because keyboard support was enabled in the emulator. Keyboard can be disabled using the AVD manager. From Eclipse, choose Window -> AVD Manager. Select the AVD and click Edit and make sure that Keyboard support is set to “no”.

Filtering on the Adapter

Instead of using the ListView’s setTextFilterEnabled, you may be able to set the filter on the data adapter. Most of the standard adapter’s implement Filterable interface, which provides the getFilter method. The Filter object will allow you to set the filter through the filter method.

The EditTextFilter activity (also available from my GitHub repostiory) uses an EditText to allow the user to control the contents of the filter.

This is code snippet from the activity is how it listens for changes to the EditText and update the filter:

final EditText filterEditor = (EditText) findViewById(R.id.editFilter);

// Add a listener to the EditText that will update the filter.
filterEditor.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
        int count) {
    updateText();
    }

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

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

    private void updateText() {
    // Update the filter according to the contents of the field.
    dataAdapter.getFilter().filter(
        filterEditor.getText().toString());
    }
});
Advertisements

One Response to [Android] Allowing the user to enter a filter for a ListView

  1. sajal says:

    its only for showing 1st leter to be filtered, how could i filter whole data entered in the edit text.
    my no: (+91)9903076025
    mail id: industryjobx@gmail.com
    name:sajal

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: