Android Autocomplete Textview Example
What is AutoCompleteTextView?
An editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.
The drop down can be dismissed at any time by pressing the back key or, if no item is selected in the drop down, by pressing the enter/dpad center key.
The application that we are going to develop has a simple screen with TextView.
Create layout XML
- Create new android project [File >> New >> Android Project] with project name AutoCompleteTextView
- Click next and select target android device version [I chose version 2.2]
- Click next and enter package name – ‘com.prgguru.android’
- Click finish
Open main.xml, now you can view the layout as either XML or in graphical view and just replace the XML with below one:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <AutoCompleteTextView android:id="@+id/languages" android:layout_width="fill_parent" android:layout_height="wrap_content"></AutoCompleteTextView> </LinearLayout>
- Create Array adapter – Define drop down style and string array
- Retrieve the AutoCompletTextView widget in UI that we need to interact with programmatically
- Set the array adapter to the AutoCompleteTextView widget object
Set threshold – Number of characters the user must type before the drop down list is shown
Let us test the application:
Run click on the project >> Run as >> Android application
You could see following screen:
Download Source Code
Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project(How to import android project in eclipse). If you just want to run the application in your mobile and see the output but don’t want to hit your head with source code, download application(apk) file and install it in your mobile device.Download Source Code Download Application(apk)
*apk in Android is the installation file simliar to exe in windows.
I hope you enjoyed the post!! 🙂
Keep posted with your valuable comments below 🙂
Android AutoComplete TextView