In Jetpack Compose, there is no direct equivalent to the traditional AutoCompleteTextView
. However, you can achieve the same functionality by combining a TextField
with a dropdown menu (DropdownMenu
) or similar composables to display suggestions as the user types.
Here’s how you can create an Autocomplete TextField:
Example: Autocomplete with a Dropdown Menu
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
@Composable
fun AutoCompleteTextViewExample() {
var text by remember { mutableStateOf(TextFieldValue("")) }
val suggestions = listOf("Apple", "Banana", "Cherry", "Date", "Elderberry", "Fig", "Grape")
var filteredSuggestions by remember { mutableStateOf(suggestions) }
var expanded by remember { mutableStateOf(false) }
Column(modifier = Modifier.padding(16.dp)) {
// TextField with suggestion filtering
TextField(
value = text,
onValueChange = { newValue ->
text = TextFieldValue(newValue.text)
filteredSuggestions = suggestions.filter {
it.contains(newValue.text, ignoreCase = true)
}
expanded = filteredSuggestions.isNotEmpty()
},
label = { Text("Enter a fruit") },
modifier = Modifier.fillMaxWidth()
)
// Dropdown for suggestions
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
modifier = Modifier.fillMaxWidth()
) {
filteredSuggestions.forEach { suggestion ->
DropdownMenuItem(
text = { Text(suggestion) },
onClick = {
text = TextFieldValue(suggestion)
expanded = false
}
)
}
}
}
}
Explanation
- TextField:
- Used for user input.
- The
onValueChange
lambda updates the text and filters suggestions dynamically.
- DropdownMenu:
- Displays filtered suggestions.
- Triggered by the
expanded
state.
- Dynamic Suggestions:
- The
filteredSuggestions
list updates based on the user’s input, showing matching options.
Key Features
- Dynamic Filtering: Suggestions are filtered based on the user’s input.
- Flexible Styling: You can customize the look of the dropdown and items.
- State Management:
expanded
controls whether the dropdown is visible.text
manages the current input.
Advanced Example: Using ExposedDropdownMenuBox
You can also use ExposedDropdownMenuBox
for a more Material Design-compliant implementation.
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AutoCompleteWithExposedDropdownMenu() {
val suggestions = listOf("Red", "Green", "Blue", "Yellow", "Purple")
var text by remember { mutableStateOf("") }
var expanded by remember { mutableStateOf(false) }
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = { expanded = !expanded }
) {
TextField(
value = text,
onValueChange = { newValue ->
text = newValue
expanded = suggestions.any { it.contains(newValue, ignoreCase = true) }
},
label = { Text("Select a color") },
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) },
modifier = Modifier.menuAnchor()
)
ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
suggestions.filter { it.contains(text, ignoreCase = true) }.forEach { suggestion ->
DropdownMenuItem(
text = { Text(suggestion) },
onClick = {
text = suggestion
expanded = false
}
)
}
}
}
}
Benefits of Using Jetpack Compose
- Declarative UI: Easy to build and customize autocomplete behavior.
- Dynamic Updates: Filter suggestions dynamically in real-time.
- Material Design: Use
ExposedDropdownMenuBox
for a polished, Material-styled dropdown.
This approach allows you to create a robust autocomplete feature fully integrated into the Jetpack Compose ecosystem.