Android AutoCompleteTextView is an editable text view which shows a list of suggestions when user starts typing text. When a user starts typing, a dropdown menu will be there based on the entered characters, defined in threshold limit and user can choose an item from list to replace the text.
The AutoCompleteTextView is a subclass of EditText class so we can easily inherit all the properties of EditText as per our requirements.
The dropdown list will be obtained suing data adaptor and these suggestions will be appeared only after entering the minimum number characters defined in the Threshold limit. The Threshold limit is used to define the minimum number of characters the user must type to see the dropdown list of suggestions.
In android, we can create a AutoCompleteTextView control in two ways either manually in XML file or create it in Activity file programmatically.
First we create a new project by following the below steps:
- Click on File, then New => New Project.
- After that include the Kotlin support and click on next.
- Select the minimum SDK as per convenience and click next button.
- Then select the Empty activity => next => finish.
Different attributes of AutoCompleteText widget –
|android:id||Used to uniquely identify the control.|
|android:gravity||Used to specify how to align the text like left, right, center, top, etc.|
|android:text||Used to set the text.|
|android:textSize||Used to set the size of the text.|
|android:textStyle||Used to set the style of the text like bold, italic.|
|android:background||Used to set background color of the Text View.|
|android:hint||Used to set display hint text in the Text View.|
|android:maxHeight||Used to set maximum height of the Text view.|
|android:maxWidth||Used to set maximum width of the Text view.|
|android:padding||Used to set the padding from left, right, top and bottom.|
Add the AutoCompleteTextView in activity_main.xml
In this file, we will add the AutoCompleteTextView and Button widget and set their attributes so that it can be accessed in the kotlin file.
Modify the strings.xml file to add the string-array
Here, we will specify the name of the activity and define other strings which can be used at different places in our activity. Another important thing is that we will define string_array which contains the items for the suggestion list of AutoCompleteTextView.
Access the CompleteTextView in MainActivity.kt file
First of all, we declare a variable autotextview to access the widget from the XML layout.
val autotextView = findViewById<AutoCompleteTextView>(R.id.autoTextView)
then, we declare another variable languages to get the items of the string-array from the strings.xml file.
val languages = resources.getStringArray(R.array.Languages)
Create an adaptor and add into the AutoCompleteTextView of LinearLayout using
val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, languages) autotextView.setAdapter(adapter)
We are familiar with further activities in previous articles like accessing button and set OnClickListener etc.
Run as Emulator:
- Dynamic AutoCompleteTextView in Kotlin
- Kotlin Data Types
- Hello World program in Kotlin
- Kotlin | Retrieve Collection Parts
- Destructuring Declarations in Kotlin
- DatePicker in Kotlin
- Kotlin labeled continue
- Introduction to Kotlin
- Kotlin Type Conversion
- Kotlin Exception Handling | try, catch, throw and finally
- Kotlin if-else expression
- Kotlin Environment setup for Command Line
- Kotlin constructor
- Kotlin Environment setup with Intellij IDEA
- Kotlin Nested class and Inner class
- Kotlin Variables
- Kotlin Operators
- Kotlin Standard Input/Output
- Kotlin Expression, Statement and Block
- Kotlin when expression
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.