{"id":2750,"date":"2020-04-07T22:14:39","date_gmt":"2020-04-07T12:14:39","guid":{"rendered":"https:\/\/learntodroid.com\/?p=2750"},"modified":"2020-04-07T22:14:49","modified_gmt":"2020-04-07T12:14:49","slug":"android-autocompletetextview-tutorial-with-examples","status":"publish","type":"post","link":"http:\/\/10.0.0.14:32769\/android-autocompletetextview-tutorial-with-examples\/","title":{"rendered":"Android AutoCompleteTextView Tutorial with Examples"},"content":{"rendered":"\n
AutoCompleteTextView in Android development is an editable text field that once a certain number of characters are entered, displays a drop down list showing auto-completion results that the user can select from to save time typing.<\/p>\n\n\n\n
I have created a tutorial on how to use the AutoCompleteTextView widget in Android which includes examples with sample code.<\/p>\n\n\n\n
In this tutorial on AutoCompleteTextView I will cover:<\/p>\n\n\n\n
All of the code samples shared in this tutorial are available in the AutoCompleteTextView GitHub repository I created at the following link.<\/p>\n\n\n\n
https:\/\/github.com\/learntodroid\/AutoCompleteTextViewTutorial<\/a><\/p>\n\n\n\n The documentation for AutoCompleteTextView widget<\/a> is a great resource which includes detailed information about the attributes, methods, constructors and along with some sample code for AutoCompleteTextView.<\/p>\n\n\n\n Standard Example of AutoCompleteTextView in Android<\/p>\n<\/div><\/div>\n\n\n\n The table below contains attributes used in the AutoCompleteTextView widget in Android along with the description of the attribute and in some cases an example image.<\/p>\n\n\n\n The table below contains some of the key methods of the AutoCompleteTextView widget in Android along with a description.<\/p>\n\n\n\n For additional information about the methods supported by the AutoCompleteTextView widget have a look at the official documentation for AutoCompleteTextView widget<\/a>.<\/p>\n\n\n\n In this tutorial for AutoCompleteTextView we will create a simple app in Android that will autocomplete a place entered by the user.<\/p>\n\n\n\n First we will start by creating a layout resource for to be used in an Activity where we will display the AutoCompleteTextView.<\/p>\n\n\n\n See an example layout resource file below for the MainActivity class containing a AutoCompleteTextView located to the right of a TextView with the label “Place”.<\/p>\n\n\nAutoCompleteTextView Attributes<\/h2>\n\n\n\n
No.<\/th> Attribute<\/th> Example<\/th><\/tr><\/thead> 1<\/td> android:completionHint<\/strong>
Defines the hint displayed in the drop down menu.<\/td>android:completionHint=\"Example Completion Hint\"<\/code><\/td><\/tr>
2<\/td> android:completionHintView<\/strong>
Defines the hint view displayed in the drop down menu.<\/td><\/td><\/tr> 3<\/td> android:completionThreshold
<\/strong>
Defines the number of characters that the user must type before completion suggestions are displayed in a drop down menu.<\/td>android:completionThreshold=\"5\"<\/code><\/td><\/tr>
4<\/td> android:dropDownAnchor
<\/strong>
View to anchor the auto-complete dropdown to.<\/td><\/td><\/tr> 5<\/td> android:dropDownHeight
<\/strong>
Specifies the basic height of the dropdown.<\/td>android:dropDownHeight=\"200dp\"<\/code><\/td><\/tr>
6<\/td> android:dropDownHorizontalOffset<\/strong>
Amount of pixels by which the drop down should be offset horizontally.<\/td>android:dropDownHorizontalOffset=\"100dp\"
android:layout_width=\"300dp\"<\/code><\/td><\/tr>7<\/td> android:dropDownSelector<\/strong>
Selector in a drop down list.<\/td><\/td><\/tr> 8<\/td> android:dropDownVerticalOffset<\/strong>
Amount of pixels by which the drop down should be offset vertically.<\/td>android:dropDownVerticalOffset=\"100dp\"<\/code><\/td><\/tr>
9<\/td> android:dropDownWidth<\/strong>
Specifies the basic width of the dropdown.<\/td>android:dropDownWidth=\"200dp\"<\/code><\/td><\/tr>
10<\/td> android:popupBackground
<\/strong>
The background to use for the popup window.<\/td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n AutoCompleteTextView Methods<\/h2>\n\n\n\n
Method Definition<\/th> Method Description<\/th><\/tr><\/thead> public void setAdapter (T adapter)<\/td> Changes the list of data used for auto completion. The provided list must be a filterable list adapter.<\/td><\/tr> public ListAdapter getAdapter ()<\/td> Returns a filterable list adapter used for auto completion.<\/td><\/tr> public void setThreshold (int threshold)<\/td> Specifies the minimum number of characters the user has to type in the edit box before the drop down list is shown.<\/td><\/tr> public int getThreshold ()<\/td> Returns the number of characters the user must type before the drop down list is shown.<\/td><\/tr> public void setOnItemClickListener (AdapterView.OnItemClickListener l)<\/td> Sets the listener that will be notified when the user clicks an item in the drop down list.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n How to Add AutoCompleteTextView to a Layout Resource<\/h2>\n\n\n\n