How can I make `TextField` not focusable, but stil...
# compose
p
How can I make
TextField
not focusable, but still keep it clickable?
I tried stuff like this
Copy code
val launcher = rememberLauncherForActivityResult(OpenDocumentTree()) { uri ->
    uri?.let { onPathChanged(it) }
}

OutlinedTextField(
    readOnly = true,
    value = path?.toString().orEmpty(),
    onValueChange = {},
    modifier = modifier
        .clickable { launcher.launch(null) }
//      .focusProperties { canFocus = false }
    )
a
clickable makes things a focus target, so no matter you try you can't have not-focusable but clickable. what are you trying to achieve?
a
Why would you want to do that? Unfocusable textfields are bad for accessibility.
☝️ 1
p
I would like to have the similar behavior as
ExposedDropdownMenuBox
, with difference being that I want to show system directory picker instead of having dropdown popup.
a
@Peter not familiar with that component. can u explain the ux?
a
Have you tried it the suggested way in the documentation?
Copy code
TextField(
        // The `menuAnchor` modifier must be passed to the text field to handle
        // expanding/collapsing the menu on click. A read-only text field has
        // the anchor type `PrimaryNotEditable`.
        modifier = Modifier.menuAnchor(ExposedDropdownMenuAnchorType.PrimaryNotEditable),
        state = textFieldState,
        readOnly = true,
        lineLimits = TextFieldLineLimits.SingleLine,
        label = { Text("Label") },
        trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) },
        colors = ExposedDropdownMenuDefaults.textFieldColors(),
    )
But as Alex already said, I don't think that it is possible or necessary to disable the focusability.
m
I think your issue here is not that you don't want the thing focusable. Anything that can be clicked will always be focusable (as people have said above). Otherwise you won't be able to move focus to it in screen reader mode and tap it (though whatever means that user performs taps). I think you should reconsider what you are using to actually represent the field itself. If the goal is to force them into selection via a drop down menu (or some other method that's not the keyboard), you are probably much better off with a standard
Text
control to you apply some styling to (like a border or whatever) and make it clickable. This makes much more logical sense (at least to me) than trying to shoehorn a
TextField
into a purpose it's not meant for. This is what i do when i have what you would call a "Spinner" in the xml view world. I have a styled
Text
that is clickable and on clicking, it shows a drop down menu.