You can use keyboard's done button to dismiss the keyboard when the user is done entering text.
<TextInput placeholder={'Password'} secureTextEntry={true} style={styles.input}
onChangeText={(pin1) => this.setState({pin1: pin1})}
value={this.state.pin1}
maxLength={8}
onSubmitEditing={ ()=> this.dismissKeyboardAction()}/>
Define this method somewhere.
dismissKeyboardAction() {
dismissKeyboard();
}
Don't forget to import
var dismissKeyboard = require('dismissKeyboard');
Also, there are several workarounds possible if you want to dismiss keyboard when the user taps anywhere else other than keyboard whenever the keyboard is visible. Use keyboardWillShow and keyboardWillHide methods to set and unset a state variable something like isKeyboardVisible = true. Also, based on this state variable, if true, render an overlay over your entire screen (transparent TouchableHighlight or TouchableWithoutFeedback) with absolute coordinates (from height 0 to screen height - keyboardheight) and call the same dismisskeyboard() method on tap.
Something like
componentWillMount() {
if (Platform.OS === 'ios') {
Keyboard.addListener('keyboardWillShow', this.keyboardWillShow); //not supported on Android
Keyboard.addListener('keyboardWillHide', this.keyboardWillHide); //not supported on Android
}
else {
Keyboard.addListener('keyboardDidShow', this.keyboardWillShowAndroid);
Keyboard.addListener('keyboardDidHide', this.keyboardWillHideAndroid);
}
}
keyboardWillShow (e) {
let newSize = e.endCoordinates.height
this.setState({
keyboardHeight: newSize,
isKeyboardVisible: true
})
}
keyboardWillHide (e) {
this.setState({
keyboardHeight: 0,
isKeyboardVisible: false
})
}
You can get screen height like this
import Dimensions from 'Dimensions';
var height = Dimensions.get('window').height;
var width = Dimensions.get('window').width;
From here, you can render a transparent touchable component over your UI only when the keyboard is visible and dismiss keyboard in the onPress method.