AutoCompleteTextView force to show all items
Asked Answered
B

12

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();                    
         }                  
}
Busch answered 1/7, 2012 at 17:55 Comment(0)
B
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>();

Busch answered 3/7, 2012 at 0:3 Comment(1)
a cleaner solution and sourceBasseterre
B
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);
}
Brethren answered 1/7, 2012 at 18:7 Comment(2)
@Busch Great description! Are you using a FilterQueryProvider and a database to update your AutoCompleteTextView?Brethren
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 ACTVBusch
S
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;
        }
    });
Selfmade answered 8/11, 2016 at 9:13 Comment(2)
I used OnClickListener though and set the focusable property to false.Holierthanthou
This kinds works, but when you click on the field again instead of closing the drop down menu it opens it again !!Cid
R
15

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

autoCompleteTextView.setThreshold(100);

Hope it helps.

Ramentum answered 14/2, 2020 at 9:38 Comment(0)
B
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>();

Busch answered 3/7, 2012 at 0:3 Comment(1)
a cleaner solution and sourceBasseterre
S
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;
        }
    });
Stamps answered 25/9, 2014 at 11:43 Comment(0)
S
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.

Shawnshawna answered 15/11, 2016 at 22:48 Comment(0)
G
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);
        }
    }
}
Glacier answered 20/4, 2017 at 12:42 Comment(1)
this will show dropdown even i didnt press @Hebetude
J
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);
    }
}
Juliusjullundur answered 5/7, 2017 at 10:17 Comment(1)
it will make crash if adapter = nilHebetude
P
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.

Perilymph answered 10/1, 2020 at 11:55 Comment(0)
T
1

With reference to this answer above: https://mcmap.net/q/246155/-autocompletetextview-force-to-show-all-items 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.
    
Transfigure answered 14/8, 2023 at 14:10 Comment(0)
K
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.

Kelly answered 4/12, 2018 at 23:24 Comment(0)
V
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.

Viburnum answered 30/6, 2020 at 9:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.