14

My main activity code:

// here you put all your data.
String[] dataArray = { "Amit sharma Kumar", "Hisham Kumar Munner",
        "Vineet John Chaturvedi", "Lucky Kumar Verma" };

ArrayList<String> alAutoCompleteList;
AutoCompleteTextView acTV;
ArrayAdapter<String> adapter1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // etAuto = (EditText) findViewById(R.id.etAuto);
    acTV = (AutoCompleteTextView) findViewById(R.id.acTV);
    // Arraylist
    alAutoCompleteList = new ArrayList<String>();
    adapter1 = new ArrayAdapter<String>(MainActivity.this,
            android.R.layout.simple_dropdown_item_1line,     alAutoCompleteList);


    acTV.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            if (acTV.enoughToFilter()) {
                acTV.showDropDown();
                acTV.bringToFront();
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
            alAutoCompleteList.clear();
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

            String acText = acTV.getText().toString().trim();

            for (String item : dataArray) {

                if     (item.toLowerCase().contains(acText.toLowerCase())) {
                    alAutoCompleteList.add(item);
                }
            }


            acTV.setThreshold(4);
            acTV.setAdapter(adapter1);
            acTV.showDropDown();

        }
    });

When I search for "sharma" and press a space after that the suggestions go off. I want those suggestions to stay there. I have tried to do everything but didn't got any success. Can someone please help?

Edit: Can someone please try this code on their emulators? Just add a AutoCompleteTextView in xml and run it.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Hisham Muneer
  • 8,558
  • 10
  • 54
  • 79

4 Answers4

15

First, is there any reason why you set that TextWatcher listener on the AutoCompleteTextView? If you did this to filter the data yourself you shouldn't do it(because the widget does this by default and your code is incorrect).

When i search "sharma" and press a space after that. suggestions goes off. I want those suggestions to stay there.

This is happening because of the adapter and the default Filter implementation which comes with it, elements that the AutoCompleteTextView uses under the hood to provide the values that you see in the drop down list. The default behavior for an ArrayAdapter is the one you see, you can find an explanation in this answer. The solution is to implement your own adapter with a filter that will search the whole adapter's row data for the filter string. I've taken the code of the ArrayAdapter class from the SDK and made a slight adjustment so the filtering doesn't break when inserting a space after a word. You can find the class here as the code is to big to post. Just copy the class in your project and use it as a normal ArrayAdapter:

FilterWithSpaceAdapter<String> adapter1;
//... 
adapter1 = new FilterWithSpaceAdapter<String>(MainActivity.this,
            android.R.layout.simple_dropdown_item_1line, dataArray);
Community
  • 1
  • 1
user
  • 86,916
  • 18
  • 197
  • 190
  • 1
    This solution doesn't work. If you write blank space, dismiss dropdown suggestions. – cnbandicoot Feb 15 '17 at 12:14
  • @cnbandicoot Please ask a new question providing more details about what you're doing. – user Feb 15 '17 at 12:28
  • 1
    I have faced the same problem and a few complications. The complications were the dropdown list hasn't been showed and I watched absolutely nothing there when in string array contained strings with dots, pluses and other special characters. I have applied your solution with FilterWithSpaceAdapter.java usage and achieved the necessary behavior for my autocompletetextview. Thanks, a great deal! I killed two birds with one stone :). – Orlov Const May 13 '19 at 14:45
2

You don't need a Textwatcher, AutoCompleteTextView uses the Filter of the Adapter you set. The default adapter filters entries by calling toString() on them. Naturally, if the user enters a space, the values no longer match. To implement this custom behavior you shouldn't add a textwatcher but build a custom adapter. You can still extend ArrayAdapter or SimpleAdapter. You implement your custom filtering behavior (in your case a trim() call) by overwriting getFilter() and publishResults(). Here or here you can find examples on how to do that.

stoilkov
  • 1,686
  • 1
  • 11
  • 18
  • can't believe I've spent so much time bumping my head at the wall and never realized that my adapter had to be using toString to figure out how to filter elements, nothing would work simply because of this haha. thx – Guilherme Santos Aug 01 '18 at 09:18
2

First of all, you don't need TextWatcher with AutoCompleteTextView because AutoCompleteTextView has its own method to watch text i.e. MyWatcher. You need to use :

setThreshold(3);
final String[] AndroidDesk= getResources().getStringArray(R.array.clothname_arrays);
ArrayAdapter<String> My_arr_adapter= new ArrayAdapter<String>(getApplicationContext(),
            android.R.layout.simple_dropdown_item_1line,AndroidDesk);      
   cloths.setThreshold(1);
   cloths.setAdapter(My_arr_adapter);
   cloths.setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {                               
   }
   });
Manish Dubey
  • 4,206
  • 8
  • 36
  • 65
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
0

I think there are problem in text watcher because auto complete text view did not need of text watcher because that auto listener event . you have to simple pass your array in to adapter instead of list. alAutoCompleteList ther are no any value in list. you had never add value in list.

  • either you have to do like : alAutoCompleteList.add("data one "); alAutoCompleteList.add("data two");

  • or pass dataArray to adapter. adapter1 = new ArrayAdapter(MainActivity.this, android.R.layout.simple_dropdown_item_1line,dataArray );

  • No need to use text watcher .

Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85