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.
Use LinearLayout in activity_main.xml file
In this file, we only use the LinearLayout and set it attributes.
Create list of suggestions in strings.xml file
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.
Create AutoCompleteTextView and button in MainActivity.kt file
First of all, we declare two variables autotextview and button to create the widgets and set their attributes.
val autotextView = AutoCompleteTextView(this) val button = Button(this)
and add both autotextview and button in LinearLayout using
val linearLayout = findViewById
(R.id.linear_layout) // Add AutoCompleteTextView and button to LinearLayout linearLayout?.addView(autotextView) linearLayout?.addView(button)
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)
Run as Emulator:
- AutoCompleteTextView in Kotlin
- Dynamic CheckedTextView in Kotlin
- Dynamic RadioButton in Kotlin
- Dynamic Switch in Kotlin
- Dynamic SeekBar in Kotlin
- Dynamic RatingBar in Kotlin
- Dynamic ImageButton in Kotlin
- Dynamic DatePicker in Kotlin
- Dynamic TimePicker in Kotlin
- Dynamic TextClock in Kotlin
- Dynamic Spinner in Kotlin
- Dynamic Chronometer in Kotlin
- Dynamic ProgressBar in Kotlin
- Dynamic ImageSwitcher in Kotlin
- Dynamic TextSwitcher in Kotlin
- Dynamic ImageView in Kotlin
- Dynamic TextView in Kotlin
- Dynamic Button in Kotlin
- Dynamic ScrollView in Kotlin
- Dynamic RadioGroup in Kotlin
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.