There is a moment in my app, that I need to force to show all items in the suggestion list, no matter what the user has typed. How can I do that?
I tried to do somet
If you want to show the suggestions instantly, then you have to override enoughToFilter()
to make it always return true. To ignore what is given as text input, you have to use performFiltering("", 0)
with an empty filtering pattern. The AutoCompleteTextView then shows all suggestions.
This is solution I've combined from other StackOverflow posts:
public class InstantAutoComplete extends android.support.v7.widget.AppCompatAutoCompleteTextView {
public InstantAutoComplete(Context context) {
super(context);
}
public InstantAutoComplete(Context context, AttributeSet attrs) {
super(context, attrs);
}
public InstantAutoComplete(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean enoughToFilter() {
return true;
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused && getAdapter() != null) {
performFiltering("", 0);
}
}
}
Note: You have to set text for autoComplete with space text and in filter, you should trim constraint text input and it will show all dropdown list.
It's actually even easier than Sam listed. Whenever you want to show all, just do:
performFiltering("", 0);
When the filtering finishes it'll automatically display the drop down with all of the items visible.
As "ArtOfWarfare" suggested, you can just sub-class and override performFiltering():
public class My_AutoCompleteTextView extends AutoCompleteTextView {
public My_AutoCompleteTextView(Context context) {
super(context);
}
public My_AutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public My_AutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void performFiltering(CharSequence text, int keyCode) {
super.performFiltering("", 0);
}
}
I'm using it successfully.
This is what worked for me:
public class CustomAutoCompleteTextView extends AutoCompleteTextView {
public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean enoughToFilter() {
return true;
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused) {
performFiltering(getText(), 0);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
this.showDropDown();
return super.onTouchEvent(event);
}
}
You don't define the "moment" when you want to display all the results, so I hope this fits. But try something like this:
AutoCompleteTextView autoComplete;
String savedText;
public void showAll() {
savedText = autoComplete.getText().toString();
autoComplete.setText("");
autoComplete.showDropDown();
}
public void restore() {
autoComplete.setText(savedText);
}