mPandaRed
mPandaRed

Reputation: 241

Android Jetpack Compose TextField disable keyboard auto suggestions

I'm looking for a way to disable the keyboard auto suggestions with the TextField Composable.

In the olden days of Android from about 4 months ago, using EditText you could do something like this, setting the inputType to textNoSuggestions|textVisiblePassword.

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textNoSuggestions|textVisiblePassword" />

I'm using both inputTypes here because not all keyboards support the textNoSuggestions field.

Is there a way to do this with Jetpack Compose's TextField? I'm not seeing anything in their KeyboardOptions to mimic this functionality.

Upvotes: 24

Views: 7308

Answers (2)

Thiago Souza
Thiago Souza

Reputation: 1671

var text by remember { mutableStateOf("") }
TextField(
    value = text,
    keyboardOptions = KeyboardOptions(
        keyboardType = KeyboardType.Email,
        autoCorrect = false
    ),
    onValueChange = {
        text = it
    }
)

We can use the autoCorrect = false. But, according to the documentation, the autoCorrect parameter:

Informs the keyboard whether to enable auto correct. Only applicable to text based KeyboardTypes such as KeyboardType.Email, KeyboardType.Uri. It will not be applied to KeyboardTypes such as KeyboardType.Number. Most of keyboard implementations ignore this value for KeyboardTypes such as KeyboardType.Text.

So, be careful which keyboardType you are using.

Upvotes: 17

user14678216
user14678216

Reputation: 3393

A workaround I found to not show keyboard suggestions/hints is to set keyboardType of KeyboardOptions to KeyboardType.Password

The text in the TextField will still be shown as normal text but the keyboard will not suggest words to complete.

OutlinedTextField(
    value = textValue,
    onValueChange = onTextChange,
    keyboardOptions = KeyboardOptions(
        keyboardType = KeyboardType.Password // Password keyboard hides suggestions
    ),
)

Upvotes: 6

Related Questions