Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

clearFocus does not work within a Dialog in Composable

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()
                            }
                        )
                    )


                }
            }
        }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

call val focusManager = LocalFocusManager.current inside Dialog

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading