20

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 something with filtering, but for me as a beginner filtering is just way too complicated, I tried searching beginners tutorial for filtering without any luck. Maybe, there is a simpler way to force to show all the suggestion items?

EDIT: Basically what was my idea, is that, when user types something whats not in the list, it shows all the available options he can have.

I've found the best way of checking weather the ACTV is beign shown or not, but onTextChangeEvent I compare the user typed text with my list, and then if no elements have been found show full list.

public void onTextChanged(CharSequence s, int start, int before, int count)
         {                
           final EditText editText = (EditText) findViewById(R.id.vardsUserInput);
            String strValue = editText.getText().toString().toUpperCase();
            String temp;
            int Cc=0; //my count variable
            for(int i=0; i<vardi.length; i++)
            {
                temp = vardi[i].toUpperCase();
                if(temp.startsWith(strValue.toUpperCase()))
                {
                    Log.d("testing",vardi[i]);
                    Cc++;                                                   
                }
            }               
        if(Cc == 0)
        {
        //Show all the available options
    textView.showDropDown();                    
         }                  
}
Real KEK
  • 169
  • 2
  • 12
XCoder
  • 295
  • 1
  • 2
  • 10

12 Answers12

15

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);
}
Sam
  • 86,580
  • 20
  • 181
  • 179
  • 1
    When I tried this I instantly got "Unfortunately, this application has stopped" I didn't really understand where should I use restore function though. – XCoder Jul 01 '12 at 18:58
  • Ok, then I need some context to your question. Please post the relevant code that you have written in your question and describe the moment when you want to display everything. – Sam Jul 01 '12 at 19:00
  • @XCoder Great description! Are you using a FilterQueryProvider and a database to update your AutoCompleteTextView? – Sam Jul 01 '12 at 21:30
  • No, I'm not I'll take a look at it. If I won't be able to figure it out I'll give it a shout, because the filter thingy is very confusing in ACTV – XCoder Jul 02 '12 at 09:51
15

This works for me perfectly, this is an easy way to resolve the problem:

final ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_dropdown_item_1line, usernameLists);
    etUsername.setThreshold(1);
    etUsername.setAdapter(adapter);
    etUsername.setOnTouchListener(new View.OnTouchListener() {

        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
            if (usernameLists.size() > 0) {
                // show all suggestions
                if (!etUsername.getText().toString().equals(""))
                    adapter.getFilter().filter(null);
                etUsername.showDropDown();
            }
            return false;
        }
    });
Ho Luong
  • 726
  • 8
  • 12
14

To stop adapter from filtering you can set high enough treshold on your textview:

autoCompleteTextView.setThreshold(100);

Hope it helps.

  • 1
    Worked for me. The new Material exposed dropdown is giving me problems. After user picks item it won't show adapter items anymore, so this was the perfect fix! Thanks. – Izak Jun 16 '20 at 03:36
  • Thank you so much, I have been facing the issue for days, only this solved my problem – Andrain Nov 19 '21 at 10:24
9

Basically, after 5-6 hours of experimentation to understand how the damn filter works, I wrote my own adapter which does exactly what I want:

    import android.content.Context;
    import android.widget.ArrayAdapter;
    import android.widget.Filter;
    import android.widget.Filterable;
    
    import java.util.ArrayList;
    
    public class BurtuAdapteris extends ArrayAdapter<String> implements Filterable {
        ArrayList<String> _items = new ArrayList<>();
        ArrayList<String> orig = new ArrayList<>();
    
        public BurtuAdapteris(Context context, int resource, ArrayList<String> items) {
            super(context, resource, items);
    
            for (int i = 0; i < items.size(); i++) {
                orig.add(items.get(i));
            }
        }
    
        @Override
        public int getCount() {
            if (_items != null)
                return _items.size();
            else
                return 0;
        }
    
        @Override
        public String getItem(int arg0) {
            return _items.get(arg0);
        }
    
        @Override
        public Filter getFilter() {
            Filter filter = new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
    
                    FilterResults oReturn = new FilterResults();
                    String temp;
                    int counters = 0;
                    if (constraint != null) {
    
                        _items.clear();
                        if (orig != null && orig.size() > 0) {
                            for (int i = 0; i < orig.size(); i++) {
                                temp = orig.get(i).toUpperCase();
    
                                if (temp.startsWith(constraint.toString().toUpperCase())) {
    
                                    _items.add(orig.get(i));
                                    counters++;
    
                                }
                            }
                        }
                        if (counters == 0) {
                            _items.clear();
                            _items = orig;
                        }
                        oReturn.values = _items;
                        oReturn.count = _items.size();
                    }
                    return oReturn;
                }
    
                @SuppressWarnings("unchecked")
                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    if (results != null && results.count > 0) {
                        notifyDataSetChanged();
                    } else {
                        notifyDataSetInvalidated();
                    }
    
                }
    
            };
    
            return filter;
        }
    
    }

And it's simple to use, just replace the original adapter with this:

    final BurtuAdapteris fAdapter = new burtuAdapteris(this, android.R.layout.simple_dropdown_item_1line, liste);

In my case liste is: ArrayList<String> liste = new ArrayList<String>();

Dmitriy Popov
  • 2,150
  • 3
  • 25
  • 34
XCoder
  • 295
  • 1
  • 2
  • 10
  • 2
    I'm copying and pasting, but leaving the name BurtuAdapter in homage to your work. :) – brainmurphy1 Oct 26 '14 at 00:51
  • 1
    a [cleaner solution](https://www.truiton.com/2018/06/android-autocompletetextview-suggestions-from-webservice-call/) and [source](https://github.com/Truiton/AutoSuggestTextViewAPICall/blob/master/app/src/main/java/com/truiton/autosuggesttextviewapicall/AutoSuggestAdapter.java) – lasec0203 Sep 07 '19 at 05:20
  • implementing the filterable was the solution! – G. Ciardini Nov 23 '20 at 16:19
3

method forcefully show drop down list.

you need to call requestFocus(); to show keyboard otherwise keyboard does not pop up.

autocomptv.setOnTouchListener(new OnTouchListener() {

        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
            // TODO Auto-generated method stub
            autocomptv.showDropDown();
            autocomptv.requestFocus();
            return false;
        }
    });
Hemant Shori
  • 2,463
  • 1
  • 22
  • 20
3

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.

Andrea Minosu
  • 51
  • 1
  • 3
2

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);
        }
    }
}
jmeinke
  • 530
  • 6
  • 27
2

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);
    }
}
Nasif Md. Tanjim
  • 3,862
  • 4
  • 28
  • 38
2

Simple and easy answer is:

after autoCompleteTextView.setText("") simply remove filter from adapter like this:

autoCompleteTextView.adapter.filter.filter(null)

thats it, filtering is gone and whole dropdown list is shown.

Antonis Radz
  • 3,036
  • 1
  • 16
  • 34
0
class InstantAutoComplete(context: Context?, attrs: AttributeSet?) : AutoCompleteTextView(context, attrs) {

override fun enoughToFilter(): Boolean {
    return true
}

override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
    super.onFocusChanged(focused, direction, previouslyFocusedRect)
    if (focused && filters != null) {
        performFiltering(text, 0)
    }
}

fun performFilter() {
    performFiltering(text, 0)
    showDropDown()
}

}

I need to show all data from autocompletetextview after click button dropdown(imageview). Using class extends AutoCompleteTextView and add this function, it works like magic.

latifalbr
  • 153
  • 2
  • 11
0
  1. autoComplete.setText(" ")
  2. filter { it.contains(constraint.trim(), true) } }

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.

beokh
  • 191
  • 2
  • 3
0

With reference to this answer above: https://stackoverflow.com/a/60223651/9166855 With some additional changes I was able to get the perfect workable solution:

  1. XML: Add below code in AutoCompleteTextView. This avoid user from entering any input manually and selecting only the suggestions.

    android:inputType="none"
    android:focusableInTouchMode="false"
    android:cursorVisible="false"
    
  2. Code: Setting high threshold helps showing all suggestions always.

    autotvLocations.setThreshold(100);  // Setting high threshold so that everytime all suggestions are shown.
    
Prajwal Waingankar
  • 2,534
  • 2
  • 13
  • 20