React Native - Moving up screen in TextInput with KeyboardAvoidingView - react-native

I have a view with some TextInputs, and some of them are in the bottom part of the screen. The thing is I want the screen to move up when I click on them to that way I can see what I'm writing. I searched a lot but nothing works for me, I have my view nested in a KeyboardAvoidingView but nothing happens when I click on the TextInput. Here's my code:
<KeyboardAvoidingView
keyboardVerticalOffset={64}
style={{ flex: 1 }}
>
<View style={styles.screen}>
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<TextInput
value={title}
onChangeText={text => setTitle(text)}
style={styles.singleLineTextInput}
placeholder="Title"
/>
<TextInput
value={keywords}
onChangeText={text => setKeywords(text)}
style={styles.singleLineTextInput}
placeholder="Keywords"
/>
<TextInput
value={description}
onChangeText={text => setDescription(text)}
style={{ ...styles.singleLineTextInput, ...styles.descriptionTextInput }}
placeholder="Description"
multiline={true}
autoFocus={true}
/>
</TouchableWithoutFeedback>
</View>
</KeyboardAvoidingView >
And my styles:
const styles = StyleSheet.create({
screen: {
flex: 1,
padding: 16,
alignItems: 'center'
},
singleLineTextInput: {
width: DEVICE_WIDTH * 0.8,
borderColor: 'black',
borderBottomWidth: 2,
fontSize: 16,
paddingHorizontal: 16
},
descriptionTextInput: {
maxHeight: DEVICE_HEIGHT / 4
}
});
I'm using React-Navigation and I tried changing keyboardVertialOffset and behavior to multiples values but nothing happens. Any ideas?
Thanks in advance

Import Content from native-base as
import { Content } from 'native-base';
And import platform from react-native
import { Platform } from 'react-native';
And use content and platform in your code like this:
<KeyboardAvoidingView
behavior={Platform.Os == "ios" ? "padding" : "height"}
style={{ flex: 1 }}
><Content>
<View style={styles.screen}>
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<TextInput
value={title}
onChangeText={text => setTitle(text)}
style={styles.singleLineTextInput}
placeholder="Title"
/>
<TextInput
value={keywords}
onChangeText={text => setKeywords(text)}
style={styles.singleLineTextInput}
placeholder="Keywords"
/>
<TextInput
value={description}
onChangeText={text => setDescription(text)}
style={{ ...styles.singleLineTextInput, ...styles.descriptionTextInput }}
placeholder="Description"
multiline={true}
autoFocus={true}
/>
</TouchableWithoutFeedback>
</View>
</Content>
</KeyboardAvoidingView>
Hope this helps!

Related

React-Native unable to type in TextInput

I'm learning react-native, currently learning state.
I have a TextInput on my screen:
however, when I try to type in it the text doesn't appear.
My code:
const Login = ({ navigation } : any) => {
const [text, setText] = useState('');
return (
<View style={[styles.container, { flexDirection: "column" }]}>
<View style={{ flex: 1, alignItems:"center", justifyContent:"center" }}>
<Image source={require('../assets/images/help.png') } style={{ width: 40, height: 40 }} />
</View>
<View style={{ flex: 2, backgroundColor: "#5f6364", borderTopRightRadius:20, borderTopLeftRadius:20 }}>
<TextInput style={styles.input} placeholder="Type here to translate!" onChangeText={newText => setText(newText)} defaultValue={text} />
</View>
</View>
)
}
Can someone explain why this is happening?
Try replacing the defaultValue with value or remove it completely

Change a vector icon colour when pressed, React Native

I have started to learn React Native recently, to try and build an application. I am trying to change the colour of the social media icons when the user clicks on them. I have manged to direct to a link when pressed but failed to change the colour.
Index.js
const SocialMedia = () => {
return (
<View style={styles.container}>
<Pressable
onPress={() =>{ Linking.openURL('https://www.facebook.com/frogsystems')
;
}}>
<Icon style={styles.social} name="facebook-f" size={30} color="#900" />
</Pressable>
<Pressable
onPress={() =>{ Linking.openURL('https://www.linkedin.com/company/frog-systems-ltd/')
;
}}>
<Icon style={styles.social} name="linkedin" size={30} color="#900" />
</Pressable>
<Pressable
onPress={() =>{ Linking.openURL('https://twitter.com/frogsystemsltd')
;
}}>
<Icon style={styles.social} name="twitter" size={30} color="#900" />
</Pressable>
</View>
)
}
export default SocialMedia;
styles.js
import { StyleSheet } from "react-native";
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
padding: 50,
justifyContent: 'center',
alignItems: "center",
},
social:{
color:'white',
padding:20,
alignItems: 'center',
},
});
export default styles;
Add pressed to change vector icon color when pressed
<Pressable
onPress={() =>{ Linking.openURL('https://www.facebook.com/frogsystems')
;
}}>
{({ pressed }) => (
<Icon
style={[
{
color: pressed ? '#D6D6D6' : '#343434'
},
styles.social,
]}
name="facebook-f"
size={30}
/>
)}
</Pressable>
Remove color from styling
social:{
padding:20,
alignItems: 'center',
},

KeyboardAwareScrollView with Footer Button Still Behind Keyboard

const headerHeight = useHeaderHeight();
const keyboardVerticalOffset = Platform.OS === 'ios' ? headerHeight + 20 : 0;
const behavior = Platform.OS === 'ios' ? 'padding' : 'height';
<SafeAreaView style={styles.wrapper}>
<KeyboardAwareScrollView
keyboardVerticalOffset={keyboardVerticalOffset}
behavior={behavior}
contentContainerStyle={{flexGrow: 1}}
style={{
backgroundColor: 'blue',
}}>
<View style={{flex: 1, backgroundColor: 'red', height: '100%'}}>
<Text>Details Screen</Text>
<View style={styles.priceInputWrapper}>
<Text style={styles.dollarSignText}>$</Text>
<TextInput
keyboardType="numeric"
style={styles.priceInput}
onChangeText={handlePriceValue}
value={priceValue}
placeholder="0"
/>
</View>
</View>
<ButtonTouchOpacity onPress={() => navigation.navigate('PostDelivery')}>
<Text>Save and Next</Text>
</ButtonTouchOpacity>
</KeyboardAwareScrollView>
</SafeAreaView>
I'm trying to set it so when the keyboard pops up, the button at the bottom will be above the keyboard. Trying to keep the button at the footer of the page.
IMO KeyboardAwareScrollView's use case is a bit different than yours. I use it when there is a situation that user can change focus between multiple inputs and they should be automatically aligned to a visible position. For cases like this, I use RN's KeyboardAvoidingView component. I don't know how strict you are on using KeyboardAwareScrollView, but if you are not that strict about it, here is a snippet that works with KeyboardAvoidingView:
import * as React from "react";
import {
View,
Button,
SafeAreaView,
Text,
TextInput,
Platform,
KeyboardAvoidingView
} from "react-native";
export default function App() {
const headerHeight = 20;
const keyboardVerticalOffset =
Platform.OS === "ios" ? headerHeight + 20 : 0;
const behavior = Platform.OS === "ios" ? "padding" : "height";
return (
<SafeAreaView style={{ flex: 1 }}>
<KeyboardAvoidingView
keyboardVerticalOffset={keyboardVerticalOffset}
behavior={behavior}
style={{
flex: 1
}}
>
<View
style={{ flex: 1, backgroundColor: "red", height: "100%" }}
>
<Text>Details Screen</Text>
<TextInput
style={{
width: 500,
height: 60,
backgroundColor: "cyan"
}}
/>
</View>
<Button
title="Save and Exit"
/>
</KeyboardAvoidingView>
</SafeAreaView>
);
}
You need to move the button outside the scrollview.
const headerHeight = useHeaderHeight();
const keyboardVerticalOffset = Platform.OS === 'ios' ? headerHeight + 20 : 0;
const behavior = Platform.OS === 'ios' ? 'padding' : 'height';
<SafeAreaView style={styles.wrapper}>
<KeyboardAwareScrollView
keyboardVerticalOffset={keyboardVerticalOffset}
behavior={behavior}
contentContainerStyle={{flexGrow: 1}}
style={{
backgroundColor: 'blue',
}}>
<View style={{flex: 1, backgroundColor: 'red', height: '100%'}}>
<Text>Details Screen</Text>
<View style={styles.priceInputWrapper}>
<Text style={styles.dollarSignText}>$</Text>
<TextInput
keyboardType="numeric"
style={styles.priceInput}
onChangeText={handlePriceValue}
value={priceValue}
placeholder="0"
/>
</View>
</View>
</KeyboardAwareScrollView>
<ButtonTouchOpacity onPress={() => navigation.navigate('PostDelivery')}>
<Text>Save and Next</Text>
</ButtonTouchOpacity>
</SafeAreaView>

How to fix keyboard from hiding input fields in React Native

I’m setting up a new component,
and the keyboard covers the fields
This is for a new component
<KeyboardAwareScrollView enableOnAndroid={true} extraScrollHeight={50} enableAutomaticScroll={true}>
<View style={{ paddingHorizontal: 20 }}>
<View>
<FloatingLabelInput
onRef={(ref) => {
this.inputs['firstName'] = ref
}}
onSubmitEditing={() => {
this.focusNextField('lastName')
}}
label={i18n.t('t_driver_registration_first_name_label')}
onChangeText={(text) => this.onChangeInputText('firstName', text)}
keyboardType='default'
maxLength={20}
value={form.firstName}
labelStyle={Style.inputLabel}
basicColor={GStyle.GREEN}
inputTextColor={Color(GStyle.BLACK).alpha(.7)}
editable={mode !== DriverFormMode.EDIT}
/>
I expect the keyboard will not cover my fields.
Wrap your view in this to scroll automatically on input focus. Do:
<KeyboardAvoidingView style={{ flex: 1, flexDirection: 'column',justifyContent: 'center',}} behavior="padding" enabled keyboardVerticalOffset={100}>
<ScrollView>
<View style={Styles.row}>
//your view
</View>
</ScrollView>
</KeyboardAvoidingView>
<View style={Styles.row}> This is just a Stylesheet e.g. Create a new StyleSheet:
const Styles = StyleSheet.create({
row: {
borderRadius: 4,
borderWidth: 0.5,
borderColor: '#d6d7da',
},
title: {
fontSize: 19,
fontWeight: 'bold',
},
activeTitle: {
color: 'red',
},
});
Use a StyleSheet:
<View style={Styles.row}>
<Text style={[Styles.title, this.props.isActive && styles.activeTitle]} />
</View>

React Native View

Hello I am new to react native and i am having problems with the view. I am having trouble with the text input as it only shows very small. When i
remove the container in the StyleSheet All Text disappears and the text inputs become big.
Here is my code
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Text style={styles.instructions}>
To get started, edit index.android.js
</Text>
<Text style={styles.instructions}>
Double tap R on your keyboard to reload,{'\n'}
Shake or press menu button for dev menu
</Text>
<View>
<View style ={styles.textinput}>
<TextInput
keyboardType='ascii-capable'
placeholder='First Name'
value={this.state.firstName}
onChangeText={(text) => {this.setState({firstName: text})}}/>
<TextInput
keyboardType='ascii-capable'
placeholder='Middle Name'
value={this.state.middleName}
onChangeText={(text) => {this.setState({middleName: text})}}/>
<TextInput
keyboardType='ascii-capable'
placeholder='Last Name'
value={this.state.lastName}
onChangeText={(text) => {this.setState({lastName: text})}}/>
<TextInput
keyboardType='ascii-capable'
placeholder='Email'
value={this.state.email}
onChangeText={(text) => {this.setState({email: text})}}/>
<TextInput
keyboardType='ascii-capable'
placeholder='Address'
value={this.state.address}
onChangeText={(text) => {this.setState({address: text})}}/>
</View>
<View style={styles.buttonModal}>
<Button
style={styles.cancelButton}
onPress={this.editUser}
title="SAVE"
color="#343434"
accessibilityLabel="Save user."/>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
textinput: {
flex: 0,
},
buttonModal: {
paddingTop: 20,
flexDirection: 'row',
justifyContent: 'space-around'
},
});
Remove <view> above <View style ={styles.textinput}> and make
textinput: {
flex: 1,
}
for button, try by commenting flexDirection: 'row',justifyContent: 'space-around' one by one.