3

I have a search textfield, I am hiding the textfield and showing just a text when the user search for something.

TextField(
    value = query,
    onValueChange = { query = it },
    modifier = modifier.weight(1f)
        .fillMaxSize().onKeyEvent {
            Log.d("hello", "$it")
            false
        },
    colors = TextFieldDefaults.textFieldColors(
        textColor = contentColor,
        disabledTextColor = Color.Transparent,
        backgroundColor = Color.Transparent,
        focusedIndicatorColor = Color.Transparent,
        unfocusedIndicatorColor = Color.Transparent,
        disabledIndicatorColor = Color.Transparent
    ),
    placeholder = {
        Text(
            text = "Search...",
            color = contentColor.copy(0.3f),
            maxLines = 1,
            overflow = TextOverflow.Ellipsis,
            textAlign = TextAlign.Center,
            modifier = Modifier.fillMaxWidth()
        )
    },
    maxLines = 1,
    singleLine = true,
    keyboardOptions = KeyboardOptions.Default.copy(
        imeAction = ImeAction.Search
    ),
    keyboardActions = KeyboardActions(onSearch = {
        focusManager.clearFocus()
        switchEditMode(false)
        if (query.text.isNotBlank()) {
            onSearch(query.text.trim())
        }
    }),
    textStyle = LocalTextStyle.current.copy(textAlign = TextAlign.Center)
)

As you can see switchEditMode(false) this will hide the textfield. I want to do the samething when the user decided to close the keyboard.

How can I listen for user hiding the keyboard in Jetpack Compose?

Jeeva
  • 3,975
  • 3
  • 23
  • 47
  • 1
    You have to know whether the keyboard is open or hidden. This answer should help you - https://stackoverflow.com/a/69533584/7725103 – Rajasekhar Mar 12 '22 at 19:33

0 Answers0