AutoCompleteTextView doesn't show dropdown when I press space after typing a complete word

Hisham Muneer picture Hisham Muneer · Oct 16, 2012 · Viewed 14.1k times · Source

My main activity code:

// here you put all your data.
String[] dataArray = { "Amit sharma Kumar", "Hisham Kumar Munner",
        "Vineet John Chaturvedi", "Lucky Kumar Verma" };

ArrayList<String> alAutoCompleteList;
AutoCompleteTextView acTV;
ArrayAdapter<String> adapter1;

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

    // etAuto = (EditText) findViewById(R.id.etAuto);
    acTV = (AutoCompleteTextView) findViewById(R.id.acTV);
    // Arraylist
    alAutoCompleteList = new ArrayList<String>();
    adapter1 = new ArrayAdapter<String>(MainActivity.this,
            android.R.layout.simple_dropdown_item_1line,     alAutoCompleteList);


    acTV.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            if (acTV.enoughToFilter()) {
                acTV.showDropDown();
                acTV.bringToFront();
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
            alAutoCompleteList.clear();
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

            String acText = acTV.getText().toString().trim();

            for (String item : dataArray) {

                if     (item.toLowerCase().contains(acText.toLowerCase())) {
                    alAutoCompleteList.add(item);
                }
            }


            acTV.setThreshold(4);
            acTV.setAdapter(adapter1);
            acTV.showDropDown();

        }
    });

When I search for "sharma" and press a space after that the suggestions go off. I want those suggestions to stay there. I have tried to do everything but didn't got any success. Can someone please help?

Edit: Can someone please try this code on their emulators? Just add a AutoCompleteTextView in xml and run it.

Answer

user picture user · Oct 25, 2012

First, is there any reason why you set that TextWatcher listener on the AutoCompleteTextView? If you did this to filter the data yourself you shouldn't do it(because the widget does this by default and your code is incorrect).

When i search "sharma" and press a space after that. suggestions goes off. I want those suggestions to stay there.

This is happening because of the adapter and the default Filter implementation which comes with it, elements that the AutoCompleteTextView uses under the hood to provide the values that you see in the drop down list. The default behavior for an ArrayAdapter is the one you see, you can find an explanation in this answer. The solution is to implement your own adapter with a filter that will search the whole adapter's row data for the filter string. I've taken the code of the ArrayAdapter class from the SDK and made a slight adjustment so the filtering doesn't break when inserting a space after a word. You can find the class here as the code is to big to post. Just copy the class in your project and use it as a normal ArrayAdapter:

FilterWithSpaceAdapter<String> adapter1;
//... 
adapter1 = new FilterWithSpaceAdapter<String>(MainActivity.this,
            android.R.layout.simple_dropdown_item_1line, dataArray);