0

One screen of my Android Compose app contains TextField and the users need that cursor is placed inside this TextField when the screen is shown. And I am seeking the solution that avoids the meddling with the cursors and FocusRequester.

There are questions and answers about the similar situation e.g. Android Compose Jetpack Autofocus on TextField when screen open or programmatically on application requirement and Programmatically click textfield in jetpack compose but the solutions always involve FocusRequesters.

My experience is that the default focus system of Android Compose is pretty good and pretty constrained and that one should not meddle with it. https://medium.com/google-developer-experts/focus-in-jetpack-compose-6584252257fe also suggests not to meddle with:

Even more inadvisably, you can opt to manage focus all by yourself! This can be a bad idea if you’re not careful enough, as it’s easy to miss subtleties in how focus traversal is expected to work. If you are careful enough, you end up re-implementing focus traversal logic without using focus internals — not fun.

When I am trying to implement the auto-click with the FocusRequester I am experiencing very strange things - once the TextField has requested the focus, it tries to keep indefinitely. E.g. when the component requested the focus and the user clicks outside the TextField, the TextField still shows flickering cursors and opened IME software keyboard and then I am pressed to hack the focus behavior, to put another component to request focus and so on. I.e. when I have started to manage the focus manually then I am pressed to keep going with the manual management and step outside the standard and default behavior.

Actually, the default behavior ir pretty simple - if the user clicks outside the TextField, then the TextField looses everything - cursor, focus and keyboard. This is the right behavior, but once the TextField has requested the focus (with FocuseRequester), then the TextField tries to keep the focus indefinitely, even when users clicks outside the TextField.

So - my question is - how to just simply click inside the TextField programmatically and let the Androdi Compose take care about the focus management itself and automatically? That is I am seeking the solution without manual management of the Focus.

My understanding is that clicking inside the TextField comes before focus as the clicking initiates the whole bunch of things - focus, IME keyboard, maybe the scroll of the page and maybe some rearrangement of the components and space due to IME keyboard.

One suggestion is to use the combination/pair of focusRequster.requestFocus() and focusRequester.freeFocus(), but this is not good either - freeFocus() can remove the greed of the TextField for the keeping the focus indefinitely (that is good and that is part of the solution indeed), but freeFocus() does just that - removes the greed/affinity, but it still requires some other actions that remove the actual focus and that close the IME keyboard.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
TomR
  • 2,696
  • 6
  • 34
  • 87
  • 1
    "if the user clicks outside the TextField, then the TextField looses everything - cursor, focus and keyboard.". This does not happen by default. You have to implement this. – Abhimanyu Aug 19 '22 at 07:14
  • 1
    Using a focus requestor is a good approach. It is not that difficult. – Abhimanyu Aug 19 '22 at 07:15
  • "This does not happen by default. You have to implement this." - I don't know about this yet, but currently I refuse to believe that such pretty standard and pretty normal behavior should be implemented and managed manually. – TomR Aug 19 '22 at 07:20
  • 1
    It is not standard. This was not the default in the View system as well. – Abhimanyu Aug 19 '22 at 07:21
  • OK, I have another Android Compose screen with 2 TextField componentes, without any focus-related code. And indeed - when I am clicking inside TextField, then this TextField keeps focus and keeps IME opened even when I am clicking outside TextField. I am required to click to another TextField component to move focus. Huh, this is beyond comprehension that such basic focus management should be done manually and that it does not come out-of-the-box... Poor Android developers... – TomR Aug 19 '22 at 07:57
  • 1
    Because I got confused, you want when you touch outside the textfield to close the keyboard and loose focus? Android users through the years they know how android behaves and they just press the back button on their phones :) Like me as a user – F.Mysir Aug 19 '22 at 15:46

0 Answers0