AutoCompleteTextView-AGN HUB
AutoCompleteTextView in Kotlin
AutoCompleteTextView is an editable text view that shows suggestions to the user as they type. The suggestions are displayed in a dropdown menu, and the user can select one to autocomplete the input field.
Key Properties
threshold: Minimum number of characters the user must type before suggestions appear.
adapter: Supplies the data (suggestions) for the dropdown menu.
setOnItemClickListener: Detects when a suggestion is selected.
Basic Implementation
XML Layout
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a name"
android:completionThreshold="1" />
Kotlin Code
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Reference to AutoCompleteTextView
val autoCompleteTextView: AutoCompleteTextView = findViewById(R.id.autoCompleteTextView)
// Data source for suggestions
val names = listOf("Alice", "Bob", "Charlie", "David", "Emily")
// Adapter to bind data to the AutoCompleteTextView
val adapter = ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, names)
autoCompleteTextView.setAdapter(adapter)
// Handle item selection from suggestions
autoCompleteTextView.setOnItemClickListener { parent, _, position, _ ->
val selectedName = parent.getItemAtPosition(position).toString()
// Handle the selected name
println("Selected: $selectedName")
}
}
}
Customizing Suggestions
Custom Layout for Suggestions You can define a custom layout for the dropdown items.
<!-- custom_dropdown_item.xml -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:textColor="@color/black"
android:textSize="16sp" />
In Kotlin:
val adapter = ArrayAdapter(this, R.layout.custom_dropdown_item, names)
autoCompleteTextView.setAdapter(adapter)
Filtering Suggestions You can implement a custom Filter to dynamically control the suggestions.
Advanced Example: Using with a Database or API
For dynamic suggestions (e.g., from a database or API), you need to fetch and set the adapter's data programmatically.
autoCompleteTextView.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
val query = s.toString()
// Fetch suggestions based on query
fetchSuggestions(query) { suggestions ->
val adapter = ArrayAdapter(this@MainActivity, android.R.layout.simple_dropdown_item_1line, suggestions)
autoCompleteTextView.setAdapter(adapter)
}
}
override fun afterTextChanged(s: Editable?) {}
})
Common Use Cases
Search Fields: Autocomplete for searching items.
Form Inputs: Cities, names, email providers, etc.
Autocomplete with Icons: Combine AutoCompleteTextView with custom layouts to include icons or additional details.
Would you like an example of a specific use case?
Comments
Post a Comment