Alon Amir
Alon Amir

Reputation: 5015

AutoCompleteTextView - disable filtering

I'm retrieving a list of strings from a webservice and I want to list them up on a AutoCompleteTextField regardless of the built-in AutoCompleteTextField filters.

How do I do that? is there a way to disable it's inner filtering easily (preferably without subclassing) I've loaded all my results into a ArrayAdapter, the problem is that some of them don't show up because of the filtering.

If I'm going in the wrong direction please point me to the right direction.

Upvotes: 23

Views: 12058

Answers (5)

Dennis K
Dennis K

Reputation: 1878

Found this while looking for a similar thing, but in what could be a bit unusual setup - using AutocompleteTextView as a Spinner (it's useful when you want a spinner wrapped in TextInputLayout). I'm also using app:simpleItems approach for providing items instead of a custom adapter like so:

<com.google.android.material.textfield.MaterialAutoCompleteTextView
                                android:id="@+id/sizes"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:inputType="none"
                                app:simpleItems="@array/sizes" />

There are two approaches that worked and don't require you to subclass the adapter:

  1. Call setText(text, false) in your onItemClickListener
view.findViewById<MaterialAutoCompleteTextView>(R.id.sizes).onItemClickListener =
        AdapterView.OnItemClickListener { _, _, position, _ ->
            setText(adapter.getItem(position).toString(), false);
        }
  1. Set empty array to filters.
view.findViewById<MaterialAutoCompleteTextView>(R.id.sampler).filters = arrayOf()

Hope this helps someone.

Upvotes: 2

NezSpencer
NezSpencer

Reputation: 680

When setting text to the AutoCompleteTextView, use setText(CharSequence text, boolean filter). Set filter to false, this sets the text without activating the filter

Upvotes: 23

cHAuHaN
cHAuHaN

Reputation: 111

I solved my problem by making a custom adapter extending ArrayAdapter class and overriding its getFilter() method. By doing this the list will not be filtered based on any text placed in the TextField and all items will be displayed.

public class MyAdapter extends ArrayAdapter{
    public MyAdapter(@NonNull Context context, int resource) {
        super(context, resource);
    }

    public MyAdapter(@NonNull Context context, int resource, int textViewResourceId) {
        super(context, resource, textViewResourceId);
    }

    public MyAdapter(@NonNull Context context, int resource, @NonNull Object[] objects) {
        super(context, resource, objects);
    }

    public MyAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull Object[] objects) {
        super(context, resource, textViewResourceId, objects);
    }

    public MyAdapter(@NonNull Context context, int resource, @NonNull List objects) {
        super(context, resource, objects);
    }

    public MyAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @NonNull
    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                return null;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

            }
        };
    }
}

Upvotes: 9

Alon Amir
Alon Amir

Reputation: 5015

Eventually I subclassed ArrayAdapter, Disabled the filters by Overriding it's getFilter method, and Made my HTTPRequest during the "text changed" events.

Upvotes: 5

Krzysztow
Krzysztow

Reputation: 781

Probably @Alon meant subclassing ArrayAdapter, instead of AutoCompleteTextView. In getFilter() method one has to return a custom filter, that performs no filtering at all (in its performFiltering()). Probably the performance could be a problem - because theread is spawned. The best thing would be to derive from TextEdit and implement own completion popup. But this is again too many hassle for me, so far. Finally, I did something as follows and it works for me. Any feedback appreciated.

public class KArrayAdapter<T> 
extends ArrayAdapter<T>
{
    private Filter filter = new KNoFilter();
    public List<T> items;

    @Override
    public Filter getFilter() {
        return filter;
    }

    public KArrayAdapter(Context context, int textViewResourceId,
            List<T> objects) {
        super(context, textViewResourceId, objects);
        Log.v("Krzys", "Adapter created " + filter);
        items = objects;
    }

    private class KNoFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence arg0) {
            FilterResults result = new FilterResults();
                result.values = items;
                result.count = items.size(); 
            return result;
        }

        @Override
        protected void publishResults(CharSequence arg0, FilterResults arg1) {
            notifyDataSetChanged();
        }
    }
}

Hope it helps.

Upvotes: 43

Related Questions