Is there a way to prevent Focus on a TextField? `M...
# compose
c
Is there a way to prevent Focus on a TextField?
Modifier.focusable(false)
isn't doing what I expect it to do. My use case is I want to have a TextField that looks editable, but when clicked on is not focused and instead goes to a different screen. Ideally I'd like to use
enabled = false
but that changes the colors in an undesirable way. Currently, it still focuses the text in the TextField before navigating away
k
Focus != editable. You still want to allow the user to navigate to that field using the keyboard + focus indication, and then activating (Enter, something else) to take that action.
c
I see what you're saying. I realize in compose accessibility is a first class citizen now. With our app though the amount of people who use accessibility is extremely, extremely low. So I'm not too worried about breaking that focus contract. Mostly just trying to see if it's possible to have a pressed interaction without the TextField receiving focus and showing the cursor on the text.
k
Keyboard navigation in desktop apps is first class feature, not accessibility
c
Sorry I should've specified this is an android specific question. No need to consider keyboard navigation
z
You can navigate by keyboard in android apps.
c
Are we talking about phones that have a physical keyboard? Are you saying it's folly to try and remove focusability? I understand if so. I'm still kinda stuck wanting the functionality of clickable, but not focusable then 😄.
k
Anything clickable should be focusable
d
I would find a TextField that worked like that confusing. What's wrong with a Button?
c
Well, it's for an edit profile page. There's over 10+ of them in a column. TextField fits the design specs perfectly whereas a button would require extra work to look correct. I had a solution of wrapping the TextField in a box with a box on top that intercepts clicks. I was hoping for a solution that didn't require this but it works for the time being. Thanks for your responses everyone
a
The
TextField
composables have a
readOnly
parameter that might do what you want. I was recently doing something similar and found that it's kind of difficult to detect clicks on a
TextField
, so the
Box
wrapper may be the best solution. You could also try looking at the
MutableInteractionSource
parameter as well. It exposes a Flow of interactions that you could listen to.
c
I did try readOnly and listening for events from interactionSource. Alas they each had their own little quirks that wouldn't satisfy my UI requirements. The more I look at it the more I don't mind the box solution ^_^
513 Views