AutoCompleteTextView and email domain Android
So there is an autocomplete text view field in my application. I want the user to enter his e-mail address. Now, in order to help him enter it faster and make no mistakes, I want to recommend the most common e-mail domain server to him
I am using this control with this array
String[] arraymails ={"@gmail.com","@hotmail.com","@yahoo.com","@outlook.com"};
This is in oncreate
mEmailView = (AutoCompleteTextView) findViewById(R.id.register_email);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arraymails);
mEmailView.setAdapter(adapter);
The idea is that when users type the "@" character and then "g", it will suggest @ gmail.com
If I type "@ G.." directly in the text box, but if I type anything before, such as "John @ GM", it will not work properly
Are there any wildcards, such as "* @gmail.com"? Or how should I implement it?
thank you
resolvent:
A few days later, I looked everywhere for a solution and couldn't find it. I posted it because it might help someone
Through trial and error and investigation of several websites and guides, I can find the solution I am looking for
This is an image of the solution:
Text box, you can enter anything you want, John is an example
Just enter "@" to get a list of all possible domain names
You can filter more by starting typing the domain name
code:
Customfilteradapter class
public class CustomFilterAdapter extends ArrayAdapter<String> {
private final String MY_DEBUG_TAG = "CustomFilterAdapter";
private ArrayList<String> items;
private ArrayList<String> itemsAll;
private ArrayList<String> suggestions;
private int viewResourceId;
public CustomFilterAdapter(Context context, int viewResourceId, ArrayList<String> items) {
super(context, viewResourceId, items);
this.items = items;
this.itemsAll = (ArrayList<String>) items.clone();
this.suggestions = new ArrayList<String>();
this.viewResourceId = viewResourceId;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(viewResourceId, null);
}
String customer = items.get(position);
if (customer != null) {
TextView customerNameLabel = (TextView)v;
if (customerNameLabel != null) {
customerNameLabel.setText(customer);
}
}
return v;
}
@Override
public Filter getFilter() {
return nameFilter;
}
Filter nameFilter = new Filter() {
public String convertResultToString(Object resultValue) {
String str = (String)resultValue;
return str;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null){
String palabra = constraint.toString();
if(palabra != null && palabra.indexOf("@") != -1) {
String palabra2 = palabra.substring(palabra.indexOf("@"));
String antesArroba;
try{
antesArroba = palabra.substring(0, palabra.indexOf("@"));
}catch (Exception ex)
{
antesArroba ="";
}
suggestions.clear();
for (String customer : itemsAll) {
if(customer.toLowerCase().startsWith(palabra2.toString().toLowerCase())){
suggestions.add(antesArroba+customer);
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = suggestions;
filterResults.count = suggestions.size();
return filterResults;
} else {
return new FilterResults();
}
}else {
return new FilterResults();
}
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
ArrayList<String> filteredList = (ArrayList<String>) results.values;
if(results != null && results.count > 0) {
clear();
for (String c : filteredList) {
add(c);
}
notifyDataSetChanged();
}
}
};
}
Oncreate activity (you can add anything you want here)
arraymails = new ArrayList();
arraymails.add("@gmail.com");
arraymails.add("@hotmail.com");
arraymails.add("@yahoo.com");
arraymails.add("@outlook.com");
arraymails.add("@adinet.com.uy");
mEmailView = (AutoCompleteTextView) findViewById(R.id.register_email);
mEmailView.setText(mEmail);
CustomFilterAdapter adapter = new CustomFilterAdapter(this,android.R.layout.simple_list_item_1,arraymails);
That's it.
Good luck!