21

I am struggling with a keyboard problem on Android. When I want to type any thing in text input, it shows some suggestions on the keyboard. How can I avoid those suggestions?

Enter image description here

The above image is from Nexus 6.

Here is my TextInput code:

<TextInput
    style={styles.TextInput}
    value={this.state.currentWord}
    onChangeText={(text) => this.setState({currentWord:text.trim()})}
    placeholder="Type your word here"
    autoCapitalize='characters'
    autoCorrect={this.state.autoCorrect}
    autoFocus={this.state.autoFocus}/>

In state, I declare autoCorrect to be false.

9
  • Have you set autoCorrect to false? Commented May 3, 2016 at 10:05
  • 7
    yah but it is not working in android, In IOS it is working Commented May 3, 2016 at 12:06
  • Could you post the code for your TextInput? When I try it in the emulator autoCorrect={false} works fine. Commented May 3, 2016 at 13:53
  • Above i attched the code for TextInput Commented May 4, 2016 at 4:59
  • Sorry, but i can't reproduce your problem. It works exactly as it should in emulator. Commented May 4, 2016 at 8:59

6 Answers 6

19
+50

When using autoComplete="false", React Native sets the underlying native Android input type to TYPE_TEXT_FLAG_NO_SUGGESTIONS and clears out TYPE_TEXT_FLAG_AUTO_CORRECT, effectively telling the system not to offer any suggestions (see source code). This is the recommended way of disabling text suggestions per the Android reference guides.

The problem is it appears that some (or many?) HTC devices do not honor this setting. From my research, it appears some Samsung devices might not support this either. It is reasonable to assume that other manufactures will not honor this setting, which flat out just sucks. This is one of the big problems with Android; somehow they didn't learn from Microsoft. Sleazy manufacturers will destroy the reliability of your products and it takes years (a decade, roughly) to even begin to undo the damage </rant> (note: I'm an Android fan).

According to Daniel's answer, it appears someone had success setting the text type to use TYPE_TEXT_VARIATION_FILTER, which tells the device that your input is being used to filter a list of items. Let’s try to modify the existing text input and see if it works, and then you can build our own if you want:

  1. You need to find the file ReactTextInputManager.java. From the React Native folder, it will be located at this path:

     [react-native]/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java
    
  2. Around line 378 you will find a method called setAutoCorrect - update it to the following:

     public void setAutoCorrect(ReactEditText view, @Nullable Boolean autoCorrect) {
         // clear auto correct flags, set SUGGESTIONS or NO_SUGGESTIONS depending on value
         updateStagedInputTypeFlag(
             view,
             InputType.TYPE_TEXT_FLAG_AUTO_CORRECT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_FILTER,
             autoCorrect != null ?
                 (autoCorrect.booleanValue() ?
                     InputType.TYPE_TEXT_FLAG_AUTO_CORRECT : (InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_FILTER))
                 : 0);
     }
    
  3. Build your application and test. If it doesn't work, try removing both instances of InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | (including the pipe) from the above code and try again. If that doesn't work, I think you're out of luck.

  4. If it does work, then you can either a) instruct everyone on your team how to modify the React Native component before building (hacky and unreliable), or b) build your own text input component. You should be able to copy and paste the existing TextInput code and shouldn't have to write much native code at all, mostly just renaming things. Good luck.

Update: going further down the rabbit hole, you can also try the setting TYPE_TEXT_VARIATION_VISIBLE_PASSWORD. So here is the kitchen sink; I'm assuming you can read the code well enough to play around with different combinations of input types:

public void setAutoCorrect(ReactEditText view, @Nullable Boolean autoCorrect) {
    // Clear auto correct flags, set SUGGESTIONS or NO_SUGGESTIONS depending on value
    updateStagedInputTypeFlag(
        view,
        InputType.TYPE_TEXT_FLAG_AUTO_CORRECT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_FILTER | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,
        autoCorrect != null ?
            (autoCorrect.booleanValue() ?
                InputType.TYPE_TEXT_FLAG_AUTO_CORRECT : (InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_FILTER | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD))
            : 0);
}

It helps to understand that the method signature for updateStagedInputTypeFlag is the following:

updateStagedInputTypeFlag([view], [flagsToUnset], [flagsToSet]);

Update 2: There are lot's of "input type" flags you can use, see a full list here. Feel free to try others. You might stumble upon one that works. You should be able to modify the code from my first update above.

Sign up to request clarification or add additional context in comments.

4 Comments

Awesome! Thanks to you I learned about Android (I'm from iOS world) more than I expected and in just few seconds. I will test (just need to borrow my colleague's Samsung device).
I learned a lot too. Java is not my forte, so I hope this works or at least sends you in the right direction. I also just realized I left out some InputType.'s in my original answer - I've updated the code. Also, in keeping with the vein of this answer, you can try any of the input types described here.
It did not solve Samsung Note 3 issues. I will try to get more devices in my hands to see whether it occurs other Samsung and other devices. Anyway for a free app I already feel fully satisfied to be aware of why and how handling this issue.
Your next best option is to go down that list of input types and try every single one until you find one that works. Another place suggested using <input name="password> (note the "name" attribute, not "type"). Also worth noting that I don't think you can disable suggestions on API < 5.0. Note 3 uses Jelly Bean, which is 4.3. You are definitely not likely to find a solution for that. Sorry man :(
12

If anyone has this problem, setting autoCorrect=false and keyboardType="visible-password" hides the suggestions on Android.

2 Comments

This works great on some devices (Pixel 2) but not on others (e.g. Galaxy S7).
Works for Samsung Galaxy Tab S6 Lite
3

You can set the TextInput to Set autoCorrect to false.

Comments

3

My solution was using keyboardType={'visible-password'} when the password is visible:

  <TextInput
    onChangeText={onChangeText}
    label={label}
    autoCapitalize='none'
    value={password}
    secureTextEntry={isPasswordVisibile}
    keyboardType={this.state.isPasswordVisibile ? undefined : 'visible-password'}
  />

1 Comment

though this works for hiding the suggestions but secureTextEntry doesn't work after the password is made visible and then back hidden.
2

You would have to write your own Android TextView wrapper that sets the correct input type. See this Stack Overflow post for more information on the Android part.

1 Comment

Thank you you are right there is no better way. Such a shame manufacturers create a mess in Android. Google is going to change the deal soon so that we will be able to deliver better apps.
0

Our fix/hack was to simply change the input’s type from text to email and back to text.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.