Hey, I am having this issue whereby Textfield does...
# compose
s
Hey, I am having this issue whereby Textfield does not lose focus on clicking out of it. After looking around, I saw a suggestion whereby you can add
pointerInput
modifier with
detectTapGestures
to your Parent layout. However, this can get messy when reusing the textfield in multiple places. Any suggestions around this?
z
Pretty sure that’s standard Android behavior? Just checked a few standard Google apps (Photos, Home, Play Store, Chrome, Calendar) and they all have text fields that keep focus when a tapping outside them.
Also if you just make a basic android screen with an EditText, that’s the default behavior.
s
I just created a simple Textfield as shown below but seems losing focus when you click outside the view is not a default behaviour in compose:
Copy code
var text by remember { mutableStateOf(TextFieldValue("")) }
TextField(
    value = text,
    label = { Text(text = "Number Input Type") },
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
    onValueChange = { it ->
        text = it
    }
)
z
Right, compose tries to match the platform behavior for stuff like this. That behavior – a text field keeping focus when clicking outside it – is the default in both cases.
s
I lost you there. what do you imply by "both cases"?
z
The behavior of views and of compose
s
Right, but for this case I am trying to figure out why in this case on clicking outside the textfield has no effect on the focus state
z
Because that’s not the default behavior, by design. If you want to do that, you need to somehow handle the click yourself and manually clear focus, e.g. with a click handler at the root of your view/compose hierarchy. But you’d be diverging from the standard behavior, so i’d be cautious about doing so.
s
Got it! Thank you for the heads up