Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide keyboard in react-native

If I tap onto a textinput, I want to be able to tap somewhere else in order to dismiss the keyboard again (not the return key though). I haven't found the slightest piece of information concerning this in all the tutorials and blog posts that I read.

This basic example is still not working for me with react-native 0.4.2 in the Simulator. Couldn't try it on my iPhone yet.

<View style={styles.container}>   <Text style={styles.welcome}>     Welcome to React Native!   </Text>   <Text style={styles.instructions}>     To get started, edit index.ios.js   </Text>   <Text style={styles.instructions}>     Press Cmd+R to reload,{'\n'}     Cmd+D or shake for dev menu   </Text>   <TextInput     style={{height: 40, borderColor: 'gray', borderWidth: 1}}     onEndEditing={this.clearFocus}   /> </View> 
like image 538
TurboFish Avatar asked Apr 16 '15 20:04

TurboFish


People also ask

Is keyboard visible React Native?

To detect when keyboard is opened or closed in React Native, we can call Keyboard. addListener to listen to the 'keybvoardDidShow' and 'keyboardDidHide' events to watch for the keyboard showing and hiding respectively.

How do I stop keyboard pushing layout up in Android React Native?

Use android:windowSoftInputMode="adjustResize". KeyboardAvoidingView and other keyboard-related components don't work quite well if you have "adjustPan" set for your android:windowSoftInputMode in AndroidManifest. xml . Instead, you should use "adjustResize" and have a wonderful life.


2 Answers

The problem with keyboard not dismissing gets more severe if you have keyboardType='numeric', as there is no way to dismiss it.

Replacing View with ScrollView is not a correct solution, as if you have multiple textInputs or buttons, tapping on them while the keyboard is up will only dismiss the keyboard.

Correct way is to encapsulate View with TouchableWithoutFeedback and calling Keyboard.dismiss()

EDIT: You can now use ScrollView with keyboardShouldPersistTaps='handled' to only dismiss the keyboard when the tap is not handled by the children (ie. tapping on other textInputs or buttons)

If you have

<View style={{flex: 1}}>     <TextInput keyboardType='numeric'/> </View> 

Change it to

<ScrollView contentContainerStyle={{flexGrow: 1}}   keyboardShouldPersistTaps='handled' >   <TextInput keyboardType='numeric'/> </ScrollView> 

or

import {Keyboard} from 'react-native'  <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}>     <View style={{flex: 1}}>         <TextInput keyboardType='numeric'/>     </View> </TouchableWithoutFeedback> 

EDIT: You can also create a Higher Order Component to dismiss the keyboard.

import React from 'react'; import { TouchableWithoutFeedback, Keyboard, View } from 'react-native';  const DismissKeyboardHOC = (Comp) => {   return ({ children, ...props }) => (     <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}>       <Comp {...props}>         {children}       </Comp>     </TouchableWithoutFeedback>   ); }; const DismissKeyboardView = DismissKeyboardHOC(View) 

Simply use it like this

... render() {     <DismissKeyboardView>         <TextInput keyboardType='numeric'/>     </DismissKeyboardView> } 

NOTE: the accessible={false} is required to make the input form continue to be accessible through VoiceOver. Visually impaired people will thank you!

like image 110
Eric Kim Avatar answered Sep 21 '22 03:09

Eric Kim


This just got updated and documented! No more hidden tricks.

import { Keyboard } from 'react-native'  // Hide that keyboard! Keyboard.dismiss() 

Github link

like image 33
Gant Laborde Avatar answered Sep 19 '22 03:09

Gant Laborde