Search code examples
focusandroid-jetpack-compose

How shift Request Focus to next textfield in Jetpack compose


My requirement is when I enter 1 digit in text field after entering it should be shifted to next text field. Everywhere I got the answer like this.

In short I'm looking for similar functionality like onTextChangeListener in android

Request Focus on TextField in jetpack compose


Solution

  • This code should give a basic idea.

    Code

    @Composable
    fun OtpScreen() {
        val focusManager = LocalFocusManager.current
        val (digit1, setDigit1) = remember {
            mutableStateOf("")
        }
        val (digit2, setDigit2) = remember {
            mutableStateOf("")
        }
        val (digit3, setDigit3) = remember {
            mutableStateOf("")
        }
        val (digit4, setDigit4) = remember {
            mutableStateOf("")
        }
        LaunchedEffect(
            key1 = digit1,
        ) {
            if (digit1.isNotEmpty()) {
                focusManager.moveFocus(
                    focusDirection = FocusDirection.Next,
                )
            }
        }
        LaunchedEffect(
            key1 = digit2,
        ) {
            if (digit2.isNotEmpty()) {
                focusManager.moveFocus(
                    focusDirection = FocusDirection.Next,
                )
            }
        }
        LaunchedEffect(
            key1 = digit3,
        ) {
            if (digit3.isNotEmpty()) {
                focusManager.moveFocus(
                    focusDirection = FocusDirection.Next,
                )
            }
        }
    
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceAround,
        ) {
            OutlinedTextField(
                value = digit1,
                onValueChange = {
                    setDigit1(it)
                },
                keyboardOptions = KeyboardOptions(
                    keyboardType = KeyboardType.NumberPassword,
                    imeAction = ImeAction.Next,
                ),
                modifier = Modifier.width(64.dp),
            )
            OutlinedTextField(
                value = digit2,
                onValueChange = {
                    setDigit2(it)
                },
                keyboardOptions = KeyboardOptions(
                    keyboardType = KeyboardType.NumberPassword,
                    imeAction = ImeAction.Next,
                ),
                modifier = Modifier.width(64.dp),
            )
            OutlinedTextField(
                value = digit3,
                onValueChange = {
                    setDigit3(it)
                },
                keyboardOptions = KeyboardOptions(
                    keyboardType = KeyboardType.NumberPassword,
                    imeAction = ImeAction.Next,
                ),
                modifier = Modifier.width(64.dp),
            )
            OutlinedTextField(
                value = digit4,
                onValueChange = {
                    setDigit4(it)
                },
                keyboardOptions = KeyboardOptions(
                    keyboardType = KeyboardType.NumberPassword,
                    imeAction = ImeAction.Done,
                ),
                modifier = Modifier.width(64.dp),
            )
        }
    }
    

    enter image description here