Android: Prevent AutoCompleteTextView.setText() from showing dropdown

0 Comments

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.

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

And there you have it!

Sources

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