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

Eko Prasetyo

05/14/2022, 11:43 AM
Hi guys, how would I make my error text sit on top of my soft keyboard when there is an error in currently focused text field?
👀 1
s

ste

05/14/2022, 1:17 PM
Copy code
val focusManager = LocalFocusManager.current

var textInput by remember {
    mutableStateOf("")
}

val focusRequester = remember {
    FocusRequester()
}

BasicTextField(
    value = textInput,
    onValueChange = { textInput = it },
    decorationBox = { innerTextField ->
        Column {
            innerTextField()
            if (textInput == "lol") {
                BasicText(text = "no, you can't do that")

                LaunchedEffect(Unit) {
                    focusManager.clearFocus()
                    focusRequester.requestFocus()
                }
            }
        }
    },
    modifier = Modifier
        .focusRequester(focusRequester)
)
Notice the thing you want to get focused must be inside the
decorationBox
and the focus needs to be cleared and request again
1
e

Eko Prasetyo

05/14/2022, 2:04 PM
@ste Thank you so much!
t

tad

05/15/2022, 3:34 AM
I think an alternative is
Modifier.focusGroup
on a Composable containing both the text field and error text
e

Eko Prasetyo

05/15/2022, 6:36 AM
@tad Thanks you! I just know that modifier, I'll look into it.
3 Views