4

I have a social messaging app that I am creating with a list of Strings of usernames and would like the edittext to show a list of the users when typing a message and keying in an "@".

How do I go about doing this?

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
hakobob
  • 81
  • 7

1 Answers1

2

You can add a TextWatcher and dynamically change the adapter of your AutoCompleteTextView. Changing only the array and calling notifyDataSetChanged() doesn't seem to be working.

   autoCompleteView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                    int occurrences = 0;
                    String enteredString = s.toString();
                    for (char c : enteredString.toCharArray()) {
                        if (c == '@') {
                            occurrences++;
                        }
                    }
                    if (occurrences == 1) {
                        String requiredString = enteredString.substring(0, enteredString.indexOf("@"));
                        email[0] = requiredString + "@gmail.com";
                        email[1] = requiredString + "@hotmail.com";
                         .
                         .
                        email[10] = requiredString + "@yahoo.com";
                        adapter = null;
                        adapter = new ArrayAdapter<>(MyActivity.this, android.R.layout.simple_list_item_1, email);
                        autoCompleteView.showDropDown();
                        autoCompleteView.setThreshold(0);
                        autoCompleteView.setAdapter(adapter );
                    } else if (occurrences == 0) {
                        autoCompleteView.dismissDropDown();
                    }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

This is for displaying a list of domains on entering '@' . You can modify the code to fit your needs.

JiTHiN
  • 6,548
  • 5
  • 43
  • 69