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);
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
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With