4

I am working on a contact manager project for android.In which i want to auto-fill the email address ending part in the field at the time of signup by the user. For example when user type his or her username it should be automatically gives the suggestion for @gmail.com or @outlook.com and so on.

Well,This is a little part of my code

String[] maindb = {"@gmail.com", "@rediffmail.com", "@hotmail.com", "@outlook.com"};

mail = (AutoCompleteTextView) findViewById(R.id.A1_edt_mail);

ArrayAdapter<String> adpt = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, maindb);
mail.setAdapter(adpt);

Well, I got output like this

Image is here

But I want that when user enter his/her username then this suggestion should be appear but it din't.

Image is here

Well, My question is not duplicate of android how an EditText work as AutoComplete this question.My problem is differ from this.

Thanks in advance.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Nitin
  • 1,280
  • 1
  • 13
  • 17

1 Answers1

3

I suggest that you append the text from email to each string in your maindb before setting the adapter --> use TextWatcher to detect changes on mail view .

Edit

may be this what you are looking for

final ArrayList<String> maindb = new ArrayList<>();
    maindb.add("@gmail.com");
    maindb.add("@rediffmail.com");
    maindb.add("@hotmail.com");
    maindb.add("@outlook.com");
    final ArrayList<String> compare = new ArrayList<>();
    final ArrayAdapter<String> adpt = new ArrayAdapter<String>(MainActivity.this, R.layout.support_simple_spinner_dropdown_item, compare);
    Word.setAdapter(adpt);
    Word.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) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.length() >= 0)
                if (!s.toString().contains("@")) {
                    compare.clear();
                    for (String aMaindb : maindb) {
                        aMaindb = s + aMaindb;
                        compare.add(aMaindb);
                    }
                    adpt.clear();
                    adpt.addAll(compare);
                }
        }
    });

Hope this will help

Mohammad
  • 739
  • 7
  • 22