Android Articles » AutoComplete TextView

Introduction

The AutoCompleteTextView is an Android Widget which provides us the typing suggestions based on the list of data bound to it. The suggestions are listed in a dropdown list from which the users can select the desired option.

The word suggestions will be stored inside an adapter. Whenever we start typing in the first letter of a word, the words starting with that letter will be shown as suggestions in the dropdown list.


Here is a live demo of the expected output.





Let's now create a project to see how to implement the AutoCompleteTextView.



Place a TextView and an AutoCompleteTextView widget in the activity_main.xml.



					  
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:id="@+id/activity_main"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:paddingBottom="@dimen/activity_vertical_margin"
       android:paddingLeft="@dimen/activity_horizontal_margin"
       android:paddingRight="@dimen/activity_horizontal_margin"
       android:paddingTop="@dimen/activity_vertical_margin">
   
       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Animals list" />
   
       <AutoCompleteTextView
           android:id="@+id/auto_text_animal"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_marginTop="20dp"
           android:hint="Search for an Animal" />
   </RelativeLayout>					  
					  


In the MainActivity.class create a String array named AnimalList and create a list of names of animals and the items are displayed using the ArrayAdapter. The code is given below:



					  
   import android.app.Activity;
   import android.graphics.Color;
   import android.os.Bundle;
   import android.widget.ArrayAdapter;
   import android.widget.AutoCompleteTextView;
   
   public class MainActivity extends Activity {
       String[] AnimalList = {
   
               "Aardvark", "Albatross", "Alligator", "Alpaca", "Ant", "Anteater", "Antelope", "Ape", "Armadillo", "Donkey", "Baboon",
               "Badger", "Barracuda", "Bat", "Bear", "Beaver", "Bee", "Bison", "Boar", "Buffalo", "Butterfly", "Camel", "Capybara",
               "Caribou", "Cassowary", "Cat", "Caterpillar", "Cattle", "Chamois", "Cheetah", "Chicken", "Chimpanzee", "Chinchilla",
               "Chough", "Clam", "Cobra", "Cockroach", "Cod", "Cormorant", "Coyote", "Crab", "Crane", "Crocodile", "Crow", "Curlew",
               "Deer", "Dinosaur", "Dog", "Dogfish", "Dolphin", "Dotterel", "Dove", "Dragonfly", "Duck", "Dugong", "Dunlin", "Eagle",
               "Echidna", "Eel", "Eland", "Elephant", "Elk", "Emu", "Falcon", "Ferret", "Finch", "Fish", "Flamingo", "Fly", "Fox",
               "Frog", "Gaur", "Gazelle", "Gerbil", "Giraffe", "Gnat", "Gnu", "Goat", "Goldfinch", "Goldfish", "Goose", "Gorilla",
               "Goshawk", "Grasshopper", "Grouse", "Guanaco", "Gull", "Hamster", "Hare", "Hawk", "Hedgehog", "Heron", "Herring",
               "Hippopotamus", "Hornet", "Horse", "Human", "Hummingbird", "Hyena", "Ibex", "Ibis", "Jackal", "Jaguar", "Jay", "Jellyfish",
               "Kangaroo", "Kingfisher", "Koala", "Kookabura", "Kouprey", "Kudu", "Lapwing", "Lark", "Lemur", "Leopard", "Lion", "Llama",
               "Lobster", "Locust", "Loris", "Louse", "Lyrebird", "Magpie", "Mallard", "Manatee", "Mandrill", "Mantis", "Marten", "Meerkat",
               "Mink", "Mole", "Mongoose", "Monkey", "Moose", "Mosquito", "Mouse", "Mule", "Narwhal", "Newt", "Nightingale", "Octopus",
               "Okapi", "Opossum", "Oryx", "Ostrich", "Otter", "Owl", "Oyster", "Panther", "Parrot", "Partridge", "Peafowl", "Pelican",
               "Penguin", "Pheasant", "Pig", "Pigeon", "Pony", "Porcupine", "Porpoise", "Quail", "Quelea", "Quetzal", "Rabbit", "Raccoon",
               "Rail", "Ram", "Rat", "Raven", "Red deer", "Red panda", "Reindeer", "Rhinoceros", "Rook", "Salamander", "Salmon", "Sand Dollar",
               "Sandpiper", "Sardine", "Scorpion", "Seahorse", "Seal", "Shark", "Sheep", "Shrew", "Skunk", "Snail", "Snake", "Sparrow",
               "Spider", "Spoonbill", "Squid", "Squirrel", "Starling", "Stingray", "Stinkbug", "Stork", "Swallow", "Swan", "Tapir", "Tarsier",
               "Termite", "Tiger", "Toad", "Trout", "Turkey", "Turtle", "Viper", "Vulture", "Wallaby", "Walrus", "Wasp", "Weasel", "Whale",
               "Wildcat", "Wolf", "Wolverine", "Wombat", "Woodcock", "Woodpecker", "Worm", "Wren", "Yak", "Zebra"
       };
   
       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
   
           //Creating the instance of ArrayAdapter containing list of animal names
           ArrayAdapter adapter = new ArrayAdapter
                   (this, android.R.layout.select_dialog_item, AnimalList);
           //Getting the instance of AutoCompleteTextView
           AutoCompleteTextView autocomplete = (AutoCompleteTextView) findViewById(R.id.auto_text_animal);
           autocomplete.setThreshold(1);             // starts working from first character
           autocomplete.setAdapter(adapter);         //setting the data from adapter into the AutoCompleteTextView
           autocomplete.setTextColor(Color.RED);
       }
   }
					  
						


With this the setup is complete. Now try running the app & you should get the output, you see in the live demo.