AutoCompleteCountryAdapter.javaactivity_main.xmlcountry_autocomplete_row.xmlCountryItem.javaMainActivity.java
package com.codinginflow.customautocompleteexample; import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Filter; import android.widget.ImageView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class AutoCompleteCountryAdapter extends ArrayAdapter<CountryItem> { private List<CountryItem> countryListFull; public AutoCompleteCountryAdapter(@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 textViewName = convertView.findViewById(R.id.text_view_name); ImageView imageViewFlag = convertView.findViewById(R.id.image_view_flag); CountryItem countryItem = getItem(position); if (countryItem != null) { textViewName.setText(countryItem.getCountryName()); imageViewFlag.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(); } }; }
<?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" android:orientation="vertical" android:padding="16dp" tools:context="com.codinginflow.customautocompleteexample.MainActivity"> <AutoCompleteTextView android:id="@+id/actv" android:layout_width="match_parent" android:layout_height="wrap_content" android:completionThreshold="1" android:hint="Select a country" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="4dp"> <ImageView android:id="@+id/image_view_flag" android:layout_width="60dp" android:layout_height="40dp" tools:src="@drawable/flag_afghanistan" /> <TextView android:id="@+id/text_view_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:paddingStart="8dp" android:textAppearance="?android:attr/textAppearanceListItemSmall" tools:text="Afghanistan" /> </LinearLayout>
package com.codinginflow.customautocompleteexample; 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; } }
package com.codinginflow.customautocompleteexample; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.AutoCompleteTextView; import java.util.ArrayList; import java.util.List; 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); AutoCompleteCountryAdapter adapter = new AutoCompleteCountryAdapter(this, countryList); editText.setAdapter(adapter); } private void fillCountryList() { countryList = new ArrayList<>(); countryList.add(new CountryItem("Afghanistan", R.drawable.flag_afghanistan)); countryList.add(new CountryItem("Albania", R.drawable.flag_albania)); countryList.add(new CountryItem("Algeria", R.drawable.flag_algeria)); countryList.add(new CountryItem("Andorra", R.drawable.flag_andorra)); countryList.add(new CountryItem("Angola", R.drawable.flag_angola)); } }