Mitchell
Mitchell

Reputation: 929

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?

Upvotes: 18

Views: 14233

Answers (6)

Bartosz Kraszewski
Bartosz Kraszewski

Reputation: 233

For people who want to change threshold using SearchView you have to use:

SearchView.SearchAutoComplete complete = (SearchView.SearchAutoComplete)search.findViewById(R.id.search_src_text);
complete.setThreshold(0);

Upvotes: 2

Pranaysharma
Pranaysharma

Reputation: 577

Alternate method of changing the setting in your XML: As mentioned by others you need to set your 'Auto Completion Threshold' to 1

Other than what @systempuntoout mentioned.

You can also do that in your XML file as shown

<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/edittext_id"
    android:inputType="textAutoComplete"
    android:completionThreshold="1"
/>

Note the line : android:completionThreshold="1"

Upvotes: 0

Heinrisch
Heinrisch

Reputation: 5935

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.

Upvotes: 34

matt
matt

Reputation: 1983

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;
    }

}

Upvotes: 9

Pentium10
Pentium10

Reputation: 207912

Pad your adapter with one/two white character on left depending on the threshold setting.

Upvotes: 1

systempuntoout
systempuntoout

Reputation: 74094

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.

Upvotes: 9

Related Questions