How do I show some default suggestions for AutoCompleteTextView before the user type anything? I cannot find a way to do this even with creating a custom class that extends AutoCompleteTextView.
I want to show suggestions for common input values to save the user from typing.
Any suggestions?
You should subclass AutoCompleteTextView
and override enoughToFilter()
to return true
all the time. After that you can call performFiltering("",0)
(it's a protected function, so you can export this call via a public function in your class).
Something like that:
public class ContactsAutoCompleteTextView extends AutoCompleteTextView {
public ContactsAutoCompleteTextView(Context context) {
super(context);
}
public ContactsAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ContactsAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean enoughToFilter() {
return true;
}
public void temp() {
performFiltering("",0);
}
}
Itay Kahana's answer is indeed correct. The only thing I would add is that instead of creating a temp() function, to override the onFocusChanged function. Personally I used the following:
@Override
protected void onFocusChanged (boolean focused, int direction, Rect previouslyFocusedRect) {
if(focused)
performFiltering("", 0);
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With