alexa
Facebook
Twitter
LinkedIn
Instagram
Whatsapp
Call Now
Quick Inquiry

OutlinedTextField Autofocus in android ?

OutlinedTextField Autofocus in android ?

To place the cursor at the end of the text, you must use TextFieldValue

The following code, give the focus to the TextField and place the cursor at the end of the text :

 @Composable
fun TestTextField(title: String = "Hello") {
    val titleFocusRequester = remember { FocusRequester() }

    val titleTextFieldValueState = remember {
        mutableStateOf(
            TextFieldValue(
                text = title,
                selection = TextRange(title.length)
            )
        )
    }

    LaunchedEffect(Unit) {
        titleFocusRequester.requestFocus()
    }

    Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
        TextField(
            modifier = Modifier
                .fillMaxWidth()
                .focusRequester(titleFocusRequester),
            value = titleTextFieldValueState.value,
            onValueChange = { tfv: TextFieldValue ->
                titleTextFieldValueState.value = tfv
            }
        )
    }
}

249 0
7

Write a Comments


* Be the first to Make Comment

GoodFirms Badge
GoodFirms Badge

Fix Your Meeting With Our SEO Consultants in India To Grow Your Business Online