So I have an autocompletetextview field in my app which I want the user to enter his email address. Now, to help him type it faster and don\'t make mistakes, I want to suggest h
i think you must create your own adapter by extending BaseAdapter, and in the getView method you must create your own logic there
Example Link
After several days and looking everywhere for the solution, couldn't find it. I'm posting it because it might help someone.
By trial and error and investigating several sites and guides i could find the solution i was looking for.
This are images of what the solution looks like:
The Textbox, you can type whatever you want, john is an example.
Just typing "@" will give you the list of all possible domains
And you could filter a bit more by starting to type the domain name
The 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();
}
}
};
}
On the activity onCreate (you can add here whatever you want)
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);
And that's all.
Good Luck!