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.
Some important xml attributes of Android AutoCompleteTextView are explained as follows:
|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.|
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:
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:
In this example, we are going to use demonstrate AutoCompleteTextView with customized suggestion box.
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
Step 3: Open res -> values -> strings.xml and replace it with below given code.
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.
Step 5: Open your MainActivity.java and replace it's code with below given code of f my MainActivity.java as follows:
Step 6: Clean and run the project to see the result.