AutoComplete TextView Android Studio

AutoComplete TextView Android Studio

In this article we will learn how to create AutoComplete TextView in android studio. Furthermore, autocomplete textview is used when we need to show a list of text like name, cities when the user type the first letters in the text. AutoComplete TextView is a UI widget. So that, when we need to create it we need to add it in the xml layout file as following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
 android:hint="@string/hint_enter_name" />

The AutoComplete TextView uses a list in order that we need to create adapter for this list. The adapter requires another XML layout file that represents the Row layout of the list. This row layout contains a TextView widget that displays the text.

So that, make right click on the layout folder in android studio and select new resource layout file and name it layout_row.xml. The past the following code in it.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
 android:text="Medium Text"
 android:textAppearance="?android:attr/textAppearanceMedium" />

Next, in the Java folder create new file that represents a model class that holds the first name and last name for People class. Use the following code.

public class People {
 private String name, lastName;
 private int id;
 public People(String name, String lastName, int id) { = name;
 this.lastName = lastName; = id;
 public int getId() {
 return id;
 public void setId(int id) { = id;
 public String getName() {
 return name;
 public void setName(String name) { = name;
 public String getlastName() {
return lastName;
 public void setlastName(String lastName) {
 this.lastName = lastName;

Well Done, after that we need to create and new Java class that extends the Adapter class. Name it

public class PeopleAdapter extends ArrayAdapter<People> {
 Context context;
 int resource, textViewResourceId;
 List<People> items, tempItems, suggestions;
 public PeopleAdapter(Context context, int resource, int textViewResourceId, List<People> items)
 super(context, resource, textViewResourceId, items);
 this.context = context;
 this.resource = resource;
 this.textViewResourceId = textViewResourceId;
 this.items = items;
 tempItems = new ArrayList<People>(items); // this makes the difference.
 suggestions = new ArrayList<People>();
 public View getView(int position, View convertView, ViewGroup parent) {
 View view = convertView;
 if (convertView == null) {
 LayoutInflater inflater = (LayoutInflater)
 view = inflater.inflate(R.layout.row, parent, false);
 People people = items.get(position);
 if (people != null) {
 TextView lblName = (TextView) view.findViewById(;
 if (lblName != null)
 return view;
 public Filter getFilter() {
 return nameFilter;
 * Custom Filter implementation for custom suggestions we provide.
 Filter nameFilter = new Filter() {
 public CharSequence convertResultToString(Object resultValue) {
 String str = ((People) resultValue).getName();
 return str;
 protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) {
 for (People people : tempItems) {
(people.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {
 FilterResults filterResults = new FilterResults();
 filterResults.values = suggestions;
 filterResults.count = suggestions.size();
 return filterResults;
 } else {
 return new FilterResults();
 protected void publishResults(CharSequence constraint, FilterResults results) {
 List<People> filterList = (ArrayList<People>) results.values;
 if (results != null && results.count > 0) {
 for (People people : filterList) {

Finally, in the main activity java file add the following code.

public class MainActivity extends AppCompatActivity {
 AutoCompleteTextView txtSearch;
 List<People> mList;
 PeopleAdapter adapter;
 private People selectedPerson;
 protected void onCreate(Bundle savedInstanceState) {
 mList = retrievePeople();
 txtSearch = (AutoCompleteTextView) findViewById(;
 adapter = new PeopleAdapter(this, R.layout.activity_main,, mList);
 txtSearch.setOnItemClickListener(new AdapterView.OnItemClickListener() {
 public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {
 //this is the way to find selected object/item
 selectedPerson = (People) adapterView.getItemAtPosition(pos);
 private List<People> retrievePeople() {
 List<People> list = new ArrayList<People>();
 list.add(new People("James", "Bond", 1));
 list.add(new People("Jason", "Bourne", 2));
 list.add(new People("Ethan", "Hunt", 3));
 list.add(new People("Sherlock", "Holmes", 4));
 list.add(new People("David", "Beckham", 5));
 list.add(new People("Bryan", "Adams", 6));
 list.add(new People("Arjen", "Robben", 7));
 list.add(new People("Van", "Persie", 8));
 list.add(new People("Zinedine", "Zidane", 9));
 list.add(new People("Luis", "Figo", 10));
 list.add(new People("John", "Watson", 11));
 return list;

Now run the app and comment us about the result.

Than You.

Learn more about autocomplete textview

Read about TextView in android studio

Leave a Comment

Your email address will not be published. Required fields are marked *