1

Is it possible not to show the soft keyboard even when the TextInput gets focused? My use case is that I need to hide the keyboard completely (for example like when you use some external keyboard) but I need to retain focus on the TextInput so I can use it normally (see the caret and so).

I don't like hacking libs, but I looked inside the textinput folder in the react-native package and found some files which I thought could be relevant. First this one:

https://github.com/facebook/react-native/blob/1e8f3b11027fe0a7514b4fc97d0798d3c64bc895/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java#L96

I replaced the code on line 96 with this:

editText.setInputType(InputType.TYPE_NULL); editText.setTextIsSelectable(true);

but unfortunately it didn't work.

Then I found this file:

https://github.com/facebook/react-native/blob/1e8f3b11027fe0a7514b4fc97d0798d3c64bc895/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java#L215

And commented out the line 215, but again, it didn't help.

Could anybody point me in a correct direction about how to get this done? Thanks.

filster
  • 193
  • 4
  • 9

2 Answers2

2

You can do it by adding an attribute to the textinput "showsoftinputonfocus" to false

More info on implementation https://techythought.com/home/detail/Disable_keyboard_for_textinput_in_React-native

Vijay
  • 21
  • 2
0

You can do this by setting a flag on the MainActivity in the /android part of the project.

Open the AndroidManifest.xml file for app/src/main and under the MainActivity change:

  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:windowSoftInputMode="adjustResize">

to this

  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:windowSoftInputMode="stateHidden">

That should disable the keyboard permanently for that Activity, which in the general case is the bulk of your React Native app on Android. For more information see: https://developer.android.com/guide/topics/manifest/activity-element.html#wsoft

Tyler Whitman
  • 364
  • 2
  • 11
  • Thanks for the suggestion, but unfortunately it didn't work. I tried also `stateAlwaysHidden`, didn't work neither. I forgot to mention that these were the first options that I tried. – filster Sep 14 '17 at 09:36
  • It seems that `windowSoftInputMode` only works if the focus is assigned automatically? (3rd answer here) https://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard "...By default, Android will automatically assign initial focus to the first EditText or focusable control in your Activity. It naturally follows that the InputMethod (typically the soft keyboard) will respond to the focus event by showing itself. The windowSoftInputMode attribute in AndroidManifest.xml, when set to stateAlwaysHidden, instructs the keyboard to ignore this automatically-assigned initial focus." – filster Sep 14 '17 at 10:03