Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid autocomplete dropdown appearing when text is programmatically set?

I have an AutoCompleteTextView in my layout. I also have an alternative way to select the same items which are present in the AutoCompleteTextView. When the alternative way is selected, I populate the value in the AutoCompleteTextView via:

autoCompleteTextView.setText(valueFromAlternativeSource);

where valueFromAlternativeSource is one of the valid auto complete options. The trouble with this is that the autocomplete dropdown appears when setText is called. Putting the following line after the above doesn't work:

autoCompleteTextView.dismissDropDown();  //Doesn't work.  Why?

Any ideas on why dismiss dropdown isn't working or other ways I could dismiss the dropdown?

like image 250
Chris Knight Avatar asked Sep 17 '11 22:09

Chris Knight


People also ask

What is autocomplete text view?

An editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.

What does Android Completionhint attribute in autocomplete text view does?

This defines the hint view displayed in the drop down menu. This defines the number of characters that the user must type before completion suggestions are displayed in a drop down menu. This is the View to anchor the auto-complete dropdown to.


5 Answers

This works fine for me and is less complex:

ListAdapter adapter = autoCompleteTextView.getAdapter();
autoCompleteTextView.setAdapter(null);
autoCompleteTextView.setText("whatever");
autoCompleteTextView.setAdapter(adapter);
like image 62
drlue Avatar answered Oct 06 '22 04:10

drlue


If you want to support API<17, Subclass AutoCompleteTextview and override setText(text, filter) method

@Override
public void setText(CharSequence text, boolean filter) {
    if(Build.VERSION.SDK_INT>=17) {
        super.setText(text, filter);
    }else{
        if(filter){
            setText(text);
        }else{
            ListAdapter adapter = getAdapter();
            setAdapter(null);
            setText(text);
            if(adapter instanceof ArrayAdapter)
                setAdapter((ArrayAdapter) adapter);
            else
                setAdapter((CursorAdapter) adapter);
            //if you use more types of Adapter you can list them here
        }
    }
}

Then whenever you want to set the text manually call setText(text, false)

like image 35
martinpelant Avatar answered Oct 06 '22 04:10

martinpelant


Looks like its a problem of the order how messages are processed. My work around looks like this:

//autoCompleteTextView.dismissDropDown();
new Handler().post(new Runnable() {
    public void run() {
        autoCompleteTextView.dismissDropDown();
}});
like image 26
Gene Avatar answered Oct 06 '22 05:10

Gene


autoCompleteTextView.setText(valueFromOtherMeans, filter);

     * @param filter If <code>false</code>, no filtering will be performed
     *        as a result of this call.
like image 30
Elango Balusamy Avatar answered Oct 06 '22 05:10

Elango Balusamy


My solution (but I don't like it, there must be something better):

autoCompleteTextView.setText(valueFromAlternativeSource);
autoCompleteTextView.setDropDownHeight(0);

autoCompleteTextView.setOnKeyListener(new OnKeyListener(){

   @Override
   public boolean onKey(View v, int keyCode, KeyEvent event) {
       autoCompleteTextView.setDropDownHeight(LayoutParams.WRAP_CONTENT);
   }
}
like image 23
Arie Avatar answered Oct 06 '22 05:10

Arie