Java – How do I autofill edit text from the middle?

How do I autofill edit text from the middle?… here is a solution to the problem.

How do I autofill edit text from the middle?

I’m working on a contact manager project for android. In this project, I want to automatically fill in the end of the email address in that field when the user registers.
For example, when a user enters his or her username, it should automatically suggest things like @gmail.com or @outlook.com.

Well, this is a small 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 expect this suggestion to appear when the user enters his/her username, but it doesn’t appear.

Image is here

Well, my problem is not android how an EditText Duplication of work as AutoComplete issues
This question. My question is different from this.

Thanks in advance.

Solution

I recommend that you append text from the email to each string in maindb –> before setting up the adapter Use TextWatcher to detect changes to the message View.

Edit

This might be what you’re 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 that helps

Related Problems and Solutions