9

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.

Makyen
  • 31,849
  • 12
  • 86
  • 121
trancer
  • 135
  • 1
  • 7

4 Answers4

11

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());
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
11

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.

Rajesh Satvara
  • 3,842
  • 2
  • 30
  • 50
  • Actually I did this but still, when the default value is set, it's showing only that item, not showing the remaining items. When setText() function is set with false as second parameter, all the items in the list should be shown but it's not so. – Kavin Raju S Aug 04 '20 at 04:16
  • 1
    This option worked perfect for me as I was having the same issue. Thanks! – Janou Apr 22 '21 at 00:53
2

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);
Mete
  • 2,805
  • 1
  • 28
  • 38
0

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.

mmdreza baqalpour
  • 1,106
  • 9
  • 18