https://kotlinlang.org logo
#compose
Title
# compose
c

Chris Johnson

10/11/2022, 9:36 PM
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

Kirill Grouchnikov

10/11/2022, 10:00 PM
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

Chris Johnson

10/11/2022, 10:14 PM
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

Kirill Grouchnikov

10/11/2022, 10:18 PM
Keyboard navigation in desktop apps is first class feature, not accessibility
c

Chris Johnson

10/11/2022, 10:21 PM
Sorry I should've specified this is an android specific question. No need to consider keyboard navigation
z

Zach Klippenstein (he/him) [MOD]

10/12/2022, 1:27 AM
You can navigate by keyboard in android apps.
c

Chris Johnson

10/12/2022, 1:37 AM
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

Kirill Grouchnikov

10/12/2022, 1:52 AM
Anything clickable should be focusable
d

Dan MacNeil

10/12/2022, 1:40 PM
I would find a TextField that worked like that confusing. What's wrong with a Button?
c

Chris Johnson

10/12/2022, 6:12 PM
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

Alex Petitjean

10/13/2022, 7:42 PM
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

Chris Johnson

10/13/2022, 7:48 PM
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 ^_^
368 Views