Android AutoCompleteTextView


Download Android AutoCompleteTextView sample

Android AutoCompleteTextView is an editable text view that shows completion suggestions automatically while the user start 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.

AutoCompleteTextView xml attributes


Some important xml attributes of Android AutoCompleteTextView are explained as follows:

Xml attributes Description
android:completionHint Specify the hint displayed in the drop down menu.
android:completionHintView Specify the hint view displayed in the drop down menu
android:completionThreshold Specify the number of characters that the user must type before completion suggestions are displayed in a drop down menu
android:dropDownAnchor This is the View to anchor the auto complete dropdown to.
android:dropDownHeight Specify the basic height of the dropdown.
android:dropDownHorizontalOffset The amount of pixels by which the drop down should be offset horizontally
android:dropDownSelector This is the selector in a drop down list
android:dropDownVerticalOffset The amount of pixels by which the drop down should be offset vertically.
android:dropDownWidth This specifies the basic width of the dropdown.

Text decoration in android


Android provides several text decorating attributes like Typeface, font family, text style, text size, text color, text shadow etc. that can be applied to AutoCompleteText. To learn more visit below given link:

How to decorate text in android

How to customize suggestion box of AutoCompleteTextView?


To customize suggestion box, i created a new layout as follows:

When I created adapter to handle AutoCompleteTextView data, I just specified the layout & TextView which I want for suggestion box as follows:

Result

AutoCompleteTextView custum suggestion box

Example of AutoCompleteTextView


In this example, we are going to use demonstrate AutoCompleteTextView with customized suggestion box.

Result

Android autoComplete text view

Step 1: Create a new project in Android Studio and name it Android AutoCompleteTextView.

Step 2: Open res -> layout -> activity_main.xml and add following code

activity_main.xml

Step 3: Open res -> values -> strings.xml and replace it with below given code.

strings.xml

Step 4: Create a new layout resource under res -> layout named hint_completion_layout.xml. Replace the code of hint_completion_layout.xml with below given code.

hint_completion_layout.xml

Step 5: Open your MainActivity.java and replace it's code with below given code of f my MainActivity.java as follows:

MainActivity.java

Step 6: Clean and run the project to see the result.


Share & Like

Tutorial topics

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.