trancer
trancer

Reputation: 155

Android - AutoCompleteTextView not showing after setText is called

I'm having a weird issue with AutoCompleteTextView.

I have a AutoCompleteTextView that shows suggestions of cities when typing in it. The list of cities is retrieved from a remote server via JSON. When I use the soft keyboard or the Mic Button on the soft keyboard, the suggestions work fine. AutoCompleteTextView does show the suggested cities.

But, I have a problem when I try to set the text using myAutoCompleteTextView.setText("Chi") , the auto complete does not show.. I have also tried myAutoCompleteTextView.append("Chi") but still no luck..

The adapter is there, its just that the suggestions don't show.

Any tips?

Thanks.

Upvotes: 11

Views: 9039

Answers (4)

mmdreza baqalpour
mmdreza baqalpour

Reputation: 1403

I searched for it and just found this solution that worked so well Look at this issue

fun AutoCompleteTextView.showDropdown(adapter: ArrayAdapter<String>?) {
  if(!TextUtils.isEmpty(this.text.toString())){
      adapter?.filter?.filter(null)
  }
}

In kotlin language, you can use this extension function.

Upvotes: 1

Rajesh Satvara
Rajesh Satvara

Reputation: 3954

It is due to filtering, No Need to any extra code for manage it, I found it in very easy and working way.

Google Dev. Reference link

autoText.setText("Default Value here",false)
autoText.setSelection(autoText.text.count()) // kotlin

as per documentation second parameter you can pass for filtering.

boolean: If false, no filtering will be performed as a result of this call.

Upvotes: 13

Mete
Mete

Reputation: 2884

Biraj Zalavadia's answer work, but you must write to "settext" in Runnable. Like this:

 mACTextViewEmail.postDelayed(new Runnable() {
            @Override
            public void run() {
                mACTextViewEmail.showDropDown();    
                mACTextViewEmail.setText("My text is here");
                mACTextViewEmail.setSelection(mACTextViewEmail.getText().length());
            }
        },500);

Upvotes: 3

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Yes you are right there is a bug in AutocompleteTextview to show default suggestion using setText(""); method.

But you can achieve this by adding some more lines of code as below.

autoText.postDelayed(new Runnable() {
            @Override
            public void run() {
                autoText.showDropDown();
            }
        },500);
        autoText.setText("chi");
        autoText.setSelection(autoText.getText().length());

Upvotes: 11

Related Questions