I did what you want to do with the following code:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.autocomplete_1); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES); AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.edit); textView.setAdapter(adapter); adapter.registerDataSetObserver(new DataSetObserver() { @Override public void onChanged() { super.onChanged(); Log.d(TAG, "dataset changed"); Object item = adapter.getItem(0); Log.d(TAG, "item.toString "+ item.toString()); } }); }
item.toString will print the text displayed in the first item.
Please note that this will happen even if you do not yet show the popup (suggestions). In addition, you should check if there are any elements that have passed the filter criteria (aka user input).
To solve the first problem:
int dropDownAnchor = textView.getDropDownAnchor(); if(dropDownAnchor==0) { Log.d(TAG, "drop down id = 0"); // popup is not displayed return; } //do stuff
To solve the second problem, use getCount> 0
Pedro loureiro
source share