How to use the readOnly property of the Material TextField Composable with Jetpack Compose

Viewed 944

My use case is that I have a form in which some fields are filled out manually and others take the user to a screen where they can search for the value they want in a large list. The fields that simply take user input are working fine but when I try to add the property readOnly = true to the TextField Composable I am presented with the following error:

None of the following functions can be called with the arguments supplied.
TextField(TextFieldValue, (TextFieldValue) → Unit, Modifier = ..., TextStyle = ..., (() → Unit)? = ..., (() → Unit)? = ..., (() → Unit)? = ..., (() → Unit)? = ..., Boolean = ..., VisualTransformation = ..., KeyboardOptions = ..., Boolean = ..., Int = ..., (ImeAction, SoftwareKeyboardController?) → Unit = ..., (SoftwareKeyboardController) → Unit = ..., InteractionState = ..., Color = ..., Color = ..., Color = ..., Color = ..., Shape = ...) defined in androidx.compose.material
TextField(String, (String) → Unit, Modifier = ..., TextStyle = ..., (() → Unit)? = ..., (() → Unit)? = ..., (() → Unit)? = ..., (() → Unit)? = ..., Boolean = ..., VisualTransformation = ..., KeyboardOptions = ..., Boolean = ..., Int = ..., (ImeAction, SoftwareKeyboardController?) → Unit = ..., (SoftwareKeyboardController) → Unit = ..., InteractionState = ..., Color = ..., Color = ..., Color = ..., Color = ..., Shape = ...) defined in androidx.compose.material

I am assuming that this error has something to do with my arguments not matching up with the provided overrides for TextField but I can't seem to find anything in the documentation for how I should be using this property other than simply supplying it with a Boolean value. I was looking here for the documentation.

The code is:

TextField(
   value = statefulValue,
   onValueChange = {},
   label = {
        Text("Label")
   },
   placeholder = {
       Text("Search items")
   },
   trailingIcon = {
       Icon(
           imageVector = Icons.Filled.Search
       )
   },
   readOnly = true
)

I am using version androidx.compose.material:material:1.0.0-alpha09 of the Compose Material package and alpha09 for the rest of my compose dependencies as well.

2 Answers

I was able to use the readOnly property after updating all my compose dependencies to 1.0.0-alpha10 and then invalidating caches and restarting Android Studio.

I found that this property was introduced in alpha10 so that is why I was getting the error before.

Here are the links I used to come to this conclusion:

For compose version 1.5.0 simply do:

TextField(
   readOnly = true,
   value = "",
   label = { Text("Your text here") },
   onValueChange = { }
)
Related