Joe Altidore
04/24/2023, 11:53 AMBasicTextField
and I want the content of the TextField to be highlighted when it receives focus. I am familiar with how to do it in XML but the I am stuck on how to implement it in compose. I'll appreciate any help. ThanksMichael Paus
04/24/2023, 12:10 PMOleksandr Balan
04/24/2023, 12:14 PMTextFieldValue
and observing InteractionSource
:
var value by remember { mutableStateOf(TextFieldValue("")) }
val interactionSource = remember { MutableInteractionSource() }
LaunchedEffect(interactionSource) {
interactionSource.interactions.collect {
if (it is FocusInteraction.Focus) {
val textSize = value.text.length
value = value.copy(selection = TextRange(0, textSize))
}
}
}
BasicTextField(
value = value,
onValueChange = { value = it },
interactionSource = interactionSource,
)
Joe Altidore
04/24/2023, 12:17 PMJoe Altidore
04/24/2023, 3:26 PMfocusRequester.requestFocus()
, it highlights. However, tapping on the textfield does not hightlight itOleksandr Balan
04/24/2023, 3:30 PMJoe Altidore
04/24/2023, 10:15 PMOleksandr Balan
04/25/2023, 7:01 AMonFocusChanged
modifier exists, which was recommended here. Could you try it if it will work in your case?Joe Altidore
04/25/2023, 3:20 PMOleksandr Balan
04/25/2023, 3:37 PMmove the cursorDo you mean to un-focus the input field? Or move it with drag-n-drop? To un-focus I have used custom modifier on outer Box:
fun Modifier.clearFocusOnTap(): Modifier = composed {
val focusManager = LocalFocusManager.current
Modifier.pointerInput(Unit) {
detectTapGestures(
onTap = { focusManager.clearFocus() }
)
}
}
Like this:
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.fillMaxSize()
.imePadding()
.clearFocusOnTap()
) {
...
}
But moving with drag-n-drop works fine for me, see the video.Joe Altidore
04/26/2023, 6:44 PM