The autocomplete
by default works when you enter the first character and it matches the first character of one or more elements defined in Array
.
In this case you need the autocomplete
to be activated when you enter the character @
, for this you need a Adapter
custom in which the filter that determines the suggestions to be displayed in the view is modified.
String[] email = {"@gmail.com", "@hotmail.com", "@yahoo.com"};
//Adapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,email);
CustomFilterAdapter adapter = new CustomFilterAdapter(this, android.R.layout.simple_list_item_1, /*Convert Array to List*/ new ArrayList<>(Arrays.asList(email)));
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.auto);
actv.setThreshold(1);
actv.setAdapter(adapter);
I suggest this Adapter
:
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomFilterAdapter extends ArrayAdapter<String> {
private static final String 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 word = constraint.toString();
String beforeCiotola;
//find character @
if(word != null && word.indexOf("@") != -1) {
String newWord = word.substring(word.indexOf("@"));
try{
beforeCiotola = word.substring(0, word.indexOf("@"));
}catch (Exception ex) {
Log.e(TAG, ex.getMessage());
beforeCiotola ="";
}
suggestions.clear();
for (String customer : itemsAll) {
if(customer.toLowerCase().startsWith(newWord.toString().toLowerCase())){
suggestions.add(beforeCiotola+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);
}
//Update adapter.
notifyDataSetChanged();
}
}
};
}
To get something similar to: