How to Disable ImeAction/Button on the Soft Keyboard when TextField is Empty

Viewed 335

I have this TextFied Composable with both KeyboardOptions and KeyboardActions.

@Composable
fun TodoInputText(...) {
    val keyboardController = LocalSoftwareKeyboardController.current
    TextField( ....
        onValueChange = onTextChanged,
        keyboardOptions = KeyboardOptions.Default.copy(imeAction = ImeAction.Done),
        keyboardActions = KeyboardActions(onDone = {
            onImeAction()
            keyboardController?.hide()
        }))}

The TextField is working with the Done Action but I need to disable Done ImeAction on the keyboard whenever the TextFied is empty as illustrated on this GIF

enter image description here

I have already extracted a state to check if the TextField is empty.

@Composable
fun TodoItemEntryInput(...) {

    //hold state for TextField
    val (text, setText) = remember { mutableStateOf("") }

    val isTextBlank = text.isNotBlank()

    //declare lambda function submit that handles  a submit event when done is pressed
    val submitAction = { .... }

    TodoItemInput(
        text = text,
        onTextChange = setText,
        submitAction = submitAction,
       
    )}

Now my question is how can I use the isTextBlank state to disable or Gray-out the Done ImeAction whenever the text is empty. This is to avoid bugs if the user enters blank text - I found input verification not very optimal for this case.

1 Answers

This is impossible in regular Android, so with Jetpack Compose this task also cannot be solved.

All you can do is check if the text in the onDone callback is valid, continue if it is, and show an error if it is not.

Related