Custom AutoCompleteTextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<AutoCompleteTextView
android:id="@+id/actv"
android:completionThreshold="1"
android:hint="Select a country"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {
private List<CountryItem> countryList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillCountryList();
AutoCompleteTextView editText = findViewById(R.id.actv);
AutoCompleteAdapter adapter = new AutoCompleteAdapter(this, countryList);
editText.setAdapter(adapter);
}
private void fillCountryList() {
countryList = new ArrayList<>();
countryList.add(new CountryItem("Afghanistan", R.drawable.screenshot_11));
countryList.add(new CountryItem("albanaia", R.drawable.screenshot_12));
countryList.add(new CountryItem("Algeria", R.drawable.screenshot_13));
countryList.add(new CountryItem("Korea", R.drawable.screenshot_11));
}
}
public class CountryItem {
private String countryName;
private int flagImage;
public CountryItem(String countryName, int flagImage) {
this.countryName = countryName;
this.flagImage = flagImage;
}
public String getCountryName() {
return countryName;
}
public int getFlagImage() {
return flagImage;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dp"
xmlns:tools="http://schemas.android.com/tools">
<ImageView
android:id="@+id/image_view_flag"
tools:src="@drawable/screenshot_12"
android:layout_width="60dp"
android:layout_height="40dp" />
<TextView
android:id="@+id/text_view_name"
android:layout_gravity="center_vertical"
android:paddingStart="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
tools:text="korea"/>
</LinearLayout>
class AutoCompleteAdapter extends ArrayAdapter<CountryItem> {
private List<CountryItem> countryListFull;
public AutoCompleteAdapter(@NonNull Context context, @NonNull List<CountryItem> countryList) {
super(context, 0, countryList);
countryListFull = new ArrayList<>(countryList);
}
@NonNull
@Override
public Filter getFilter() {
return countryFilter;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.country_autocomplete_row, parent, false
);
}
TextView textView = convertView.findViewById(R.id.text_view_name);
ImageView imageView = convertView.findViewById(R.id.image_view_flag);
CountryItem countryItem = getItem(position);
if (countryItem != null) {
textView.setText(countryItem.getCountryName());
imageView.setImageResource(countryItem.getFlagImage());
}
return convertView;
}
private Filter countryFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
List<CountryItem> suggestions = new ArrayList<>();
if (constraint == null || constraint.length() == 0) {
suggestions.addAll(countryListFull);
} else {
String filterPattern = constraint.toString().toLowerCase().trim();
for (CountryItem item : countryListFull) {
if (item.getCountryName().toLowerCase().contains(filterPattern)) {
suggestions.add(item);
}
}
}
results.values = suggestions;
results.count = suggestions.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
addAll((List) results.values);
notifyDataSetChanged();
}
@Override
public CharSequence convertResultToString(Object resultValue) {
return ((CountryItem) resultValue).getCountryName();
}
};
}