Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear auto-complete Text View if Suggestion Not Found

I have an auto-complete Text View in my app, when the user starts typing it brings up suggestions. However when there is no suggestion meaning that the user's input does not match any data item I want the text field to clear when the user leaves focus.

I am new to Android. This is how I do my auto-complete:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.simple_list_item, busAutoCompleteList);
                            acBus.setAdapter(adapter);
like image 984
user3718908x100 Avatar asked Oct 03 '16 09:10

user3718908x100


Video Answer


1 Answers

When you AutoCompleteTextView lost focus, simple check your AutoCompleteTextView datasource contains the user input data or not

yourAutoCompleteTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        // When textview lost focus check the textview data valid or not
        if (!hasFocus) {
            if (!busAutoCompleteList.contains(yourAutoCompleteTextView.getText().toString()) {
                yourAutoCompleteTextView.setText(""); // clear your TextView
            }
        }
});
like image 122
Linh Avatar answered Sep 20 '22 12:09

Linh