0

I wrote the code below to display a Dialog. When a user calls the onDone function on the keyboard, the keyboard should disappear. The problem is that the focusManager.clearFocus() line does nothing: the focus doesn't disappear. I tried everything, there is no way to remove the focus and make the keyboard disappear.

@Composable
        fun showRouteDialog(
            initialName: String,
            initialDescription: String,
            onResult: (String, String) -> Unit,
            onDismiss: () -> Unit,
            create: Boolean = true
        ) {
            var name by remember { mutableStateOf(initialName) }
            var description by remember { mutableStateOf(initialDescription) }
            var isLoading by remember { mutableStateOf(false) }


            val focusManager = LocalFocusManager.current

            Dialog(onDismissRequest = {  }) {

                Column(
                    modifier = Modifier
                        .background(
                            MaterialTheme.colors.onPrimary,
                            MaterialTheme.shapes.medium
                        )
                        .padding(16.dp)
                ) {
                    TextField(
                        value = name,
                        enabled = !isLoading,
                        onValueChange = {
                            if (it.length <= 50) name = it
                        },
                        label = { Text(stringResource(R.string.name)) },
                        singleLine = true,
                        textStyle = TextStyle(
                            color = nodemappBlack,
                            fontSize = 18.textDp,
                            fontFamily = SourceSansPro
                        ),
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(top = 8.dp),
                        keyboardOptions = KeyboardOptions(
                            imeAction = ImeAction.Done
                        ),
                        keyboardActions = KeyboardActions(
                            onDone = {
                                focusManager.clearFocus()
                            }
                        )
                    )


                }
            }
        }
Sven Nijs
  • 498
  • 3
  • 19
  • Check also: https://stackoverflow.com/questions/73411163/jetpackcompose-localfocusmanager-current-movefocus-doesnt-work-in-dialog/73412379#73412379 – Gabriele Mariotti Aug 30 '22 at 12:25

1 Answers1

0

call val focusManager = LocalFocusManager.current inside Dialog

Thracian
  • 43,021
  • 16
  • 133
  • 222