Search code examples
androidandroid-jetpack-compose

Prevent the keyboard from appearing in a Jetpack Compose app


I'm making a calculator to learn Compose, so I placed my own number buttons on screen and I wanted to prevent the soft keyboard from appearing.

Here is my repo: https://github.com/vitor-ramos/CalculadorCompose

I noticed in TextFieldImpl.kt there is a modifier to show the keyboard, so I tried to clone the code and remove the line: keyboardController.value?.showSoftwareKeyboard() I know it's not a good idea to duplicate code like that, but I wanted to give it a try, and it didn't work. As you can see in the original code below there's a TODO saying it should be handled by BaseTextField, but I looked in it's code and didn't find where it shows or hides the keyboard.

val textFieldModifier = modifier
    .focusRequester(focusRequester)
    .focusObserver { isFocused = it.isFocused }
    .clickable(indication = null) {
        focusRequester.requestFocus()
        // TODO(b/163109449): Showing and hiding keyboard should be handled by BaseTextField.
        //  The requestFocus() call here should be enough to trigger the software keyboard.
        //  Investiate why this is needed here. If it is really needed, instead of doing
        //  this in the onClick callback, we should move this logic to the focusObserver
        //  so that it can show or hide the keyboard based on the focus state.
        keyboardController.value?.showSoftwareKeyboard()
    }

I found in this question that with views I can extend EditText and change the functionality, but I haven't found a equivalent for Compose: Android: Disable soft keyboard at all EditTexts

public class NoImeEditText extends EditText {
    public NoImeEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    public boolean onCheckIsTextEditor() {
        return false;
    }
}

Solution

  • Explanation

    I created a Composable ReadonlyTextField, that places a invisible box in front of the text field. The box has the same size as the text field.

    With that workaround you can't focus the text field anymore, so no keyboard appears. In order to apply custom click handling, i added a onClick to the Box-Modifier.

    This is not really a clean solution, but a good workaround.

    Implementation of ReadonlyTextField

    @Composable
    fun ReadonlyTextField(
        value: TextFieldValue,
        onValueChange: (TextFieldValue) -> Unit,
        modifier: Modifier = Modifier,
        onClick: () -> Unit,
        label: @Composable () -> Unit
    ) {
    
        Box {
            TextField(
                value = value,
                onValueChange = onValueChange,
                modifier = modifier,
                label = label
            )
    
            Box(
                modifier = Modifier
                    .matchParentSize()
                    .alpha(0f)
                    .clickable(onClick = onClick),
            )
        }
    }
    

    Usage of ReadonlyTextField

    @Composable
    fun App() {
        val textState = remember { mutableStateOf(TextFieldValue()) }
    
        Column {
            ReadonlyTextField(
                value = textState.value,
                onValueChange = { textState.value = it },
                onClick = {
                    // custom click handling (e.g. open dialog)
                },
                label = {
                    Text(text = "Keyboardless Input")
                }
            )
        }
    }
    

    A complete integrated example can be found in my medium post: https://caelis.medium.com/jetpack-compose-datepicker-textfield-39808e42646a

    Credits also go to this stackoverflow answer: Jetpack Compose: Disable Interaction with TextField