Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immediately show autocomplete on Android

The Android autocomplete only starts after two letters. How can I make it so the list appears when the field is just selected?

like image 546
Mitchell Avatar asked Nov 20 '10 22:11

Mitchell


People also ask

How do I set autocomplete text on Android?

If you want to get suggestions , when you type in an editable text field , you can do this via AutoCompleteTextView. It provides suggestions automatically when 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 in auto-complete 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.


3 Answers

To get the autocomplete to show on focus add focus listener and show the drop down when the field gets focus, like this:

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
  @Override
  public void onFocusChange(View view, boolean hasFocus) {
    if(hasFocus){
      editText.showDropDown();
    }
  }
});

Or just call editText.showDropDown() if you don't need the focus part.

like image 141
Heinrisch Avatar answered Oct 04 '22 14:10

Heinrisch


Have a look to setThreshold method:

public void setThreshold (int threshold)
Since: API Level 1
Specifies the minimum number of characters the user has to type in the edit box before the drop down list is shown.
When threshold is less than or equals 0, a threshold of 1 is applied.

like image 31
systempuntoout Avatar answered Oct 04 '22 14:10

systempuntoout


Extend the AutoCompleteTextView, overriding the enoughToFilter() methods and the threshold methods so that it doesn't replace the 0 threshold with a 1 threshold:

public class MyAutoCompleteTextView extends AutoCompleteTextView {

    private int myThreshold;

    public MyAutoCompleteTextView(Context context) {
        super(context);
    }

    public MyAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setThreshold(int threshold) {
        if (threshold < 0) {
            threshold = 0;
        }
        myThreshold = threshold;
    }

    @Override
    public boolean enoughToFilter() {
        return getText().length() >= myThreshold;
    }

    @Override
    public int getThreshold() {
        return myThreshold;
    }

}
like image 40
matt Avatar answered Oct 04 '22 16:10

matt