Android: Prevent AutoCompleteTextView.setText() from showing dropdown

If you're going to use AutoCompleteTextView.setText(), you'll soon notice that the standard dropdown behaviour will annoy you since it shows the dropdown after the call is finished.

To prevent it, you'll have to:

  • remove the adapter
  • set the text
  • set the adapter back

Since there is no adapter information to autocomplete, there will be no dropdown.

Here's some working sample code.

tvSuburbs.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Suburb s = (Suburb) tvSuburbs.getAdapter().getItem(position);
ArrayAdapter<Suburb> a = (ArrayAdapter<Suburb>) tvSuburbs.getAdapter();
tvSuburbs.setAdapter(null); // Remove the adapter so we don't get a dropdown when
tvSuburbs.setText(s.name); // text is set programmatically.
tvSuburbs.setAdapter(a); // Restore adapter
}
});

And there you have it!

Sources

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog