I want to use the AutoCompleteTextView
to provide a list of options so when the user clicks on it they get the full list but then as they type it reduces the number of items as they search for specific items. And secondly it needs to limit the entry to just the options available in the adapter provided.
Why doesn't AutoCompleteTextView
do this already? Because you have to type two characters in before the list even appears, I would like this list to be scrollable the moment the TextView
has focus.
xml code :
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:maxLines="1"
android:inputType="text" />
This is what I currently have an it's susceptible to the problem I described above.
java code :
List<String> typeList = new ArrayList<>();
for (int i = 0; i < items.size(); i++){
typeList.add(i, items.get(i).getName());
}
String[] descriptions = typeList.toArray(new String[0]);
final ArrayAdapter<String> autoCompleteAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, names);
final AutoCompleteTextView categoryInput = (AutoCompleteTextView) view.findViewById(R.id.autoCompleteCategory);
Alright! Managed to figure out a solution, quite simple in the end, add a OnFocusChangeListener()
to the AutoCompleteTextView
and then if the view has focus do showDropDown()
This way anyone who clicks it immediately gets the full list and as they type the list shrinks in size.
You have to override enoughToFilter()
method in AutoCompleteTextView
to achieve filtering happen without user entering minimum 1 or 2 characters.
Basically create your own custom view by extending AutoCompleteTextView
and override enoughtToFilter()
method to return true
always.
public class CustomAutoCompleteTextView extends AutoCompleteTextView {
public CustomAutoCompleteTextView(Context context) {
super(context);
}
public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean enoughToFilter() {
return true;
}
}