I have a component that renders the following:
return (
<KeyboardAvoidingView behavior="position">
<View style={styles.container}>
<View style={styles.messagesContainer}>
</View>
<SafeAreaView>
<View style={styles.inputView}>
<TextInput style={styles.input} />
<Button title="submit" />
</View>
</SafeAreaView>
</View>
</KeyboardAvoidingView>
)
With stylesheet:
const styles = StyleSheet.create({
container:{
height: "100%"
},
messagesContainer: {
backgroundColor: "red",
flex: 1
},
inputView: {
flexDirection: "row",
alignItems: "center",
padding: 5,
height: 24
},
input: {
height: 20,
backgroundColor: "white",
flex: 1
}
});
This view is rendered as a screen in a react-navigation StackNavigation.
I'd like the view to move up to make space for the keyboard, but it doesn't seem to move up far enough.
I know it's moving it up, because if I add some padding to the bottom of my container:
I've also wondered whether the SafeArea component is messing up the calculations of how far up the view should be moved, but it doesn't seem so.
I could tweak that bottom padding until the text input shows up right above the keyboard, but that seems like a flaky solution that'd require different values for different devices, orientations, keyboard layouts, etc.
Is there a more robust solution?
Need some modification in styling try this
return (
<KeyboardAvoidingView behavior="position" style={styles.containerWraper}>
<View style={styles.container}>
<View style={styles.messagesContainer}>
</View>
<SafeAreaView>
<View style={styles.inputView}>
<TextInput style={styles.input} />
<Button title="submit" />
</View>
</SafeAreaView>
</View>
</KeyboardAvoidingView>
)
Styles
const styles = StyleSheet.create({
container:{
flex: 1,
justifyContent: 'center'
},
containerWraper:{
flex: 1,
justifyContent: 'center'
},
messagesContainer: {
backgroundColor: "red",
flex: 1
},
inputView: {
flexDirection: "row",
alignItems: "center",
padding: 5,
height: 24
},
input: {
height: 20,
backgroundColor: "white",
flex: 1
}
});
Try NativeBase instead, that has better approach
Related
I'm currently developing an app to be used in an Android tablet, unfortunately I got comfortable and added too many features in web view before testing again on android and now they look quite different.
I have a few elements in a flex-box view with flex-direction 'row', this is how they look on the browser and how they should look:
Then the same app on android looks like this:
You can see the two lines with inputs without proper height to fit the text, and a third line on the bottom already from another input.
This is the component code (I removed one input line to make it simpler):
<View>
<Text style={styles.header}>
{LanguageService(language).form.personalData}:
</Text>
<View style={styles.twoColumns}>
<View style={styles.inputWrapper}>
<Text>
{LanguageService(language).form.firstName}:
</Text>
<TextInput
style={styles.input}
onChangeText={formikProps.handleChange('firstName')}
value={formikProps.values.firstName}
maxLength={20}
/>
</View>
<View style={styles.inputWrapper}>
<Text>
{LanguageService(language).form.phone}:
</Text>
<TextInput
style={styles.input}
onChangeText={formikProps.handleChange('phoneNumber')}
value={formikProps.values.phoneNumber}
keyboardType='phone-pad'
maxLength={15}
/>
</View>
</View>
</View>
And here is the style:
const styles = StyleSheet.create({
twoColumns: {
flex: 1,
flexDirection: 'row',
justifyContent: 'space-between',
},
inputWrapper: {
width: '48%',
flexDirection: 'row',
justifyContent: 'space-around',
borderBottomWidth: 1,
borderBottomColor: 'black',
margin: 10,
},
input: {
flexGrow: 1,
paddingLeft: 5,
},
header: {
color: 'red',
fontWeight: 'bold',
fontSize: 22,
},
}
I think you can achieve the shape in the first image with this structure:
<View style={styles.container}>
<View style={styles.headerContainer}>
<Text style={styles.header}>
{LanguageService(language).form.personalData}:
</Text>
</View>
<View style={styles.inputsContainer}>
<View stlye={styles.leftContainer>
<View style={styles.inputWrapper}>
<Text>
{LanguageService(language).form.firstName}:
</Text>
<TextInput
style={styles.input}
onChangeText={formikProps.handleChange('firstName')}
value={formikProps.values.firstName}
maxLength={20}
/>
</View>
</View>
<View stlye={styles.rightContainer>
<View style={styles.inputWrapper}>
<Text>
{LanguageService(language).form.phone}:
</Text>
<TextInput
style={styles.input}
onChangeText={formikProps.handleChange('phoneNumber')}
value={formikProps.values.phoneNumber}
keyboardType='phone-pad'
maxLength={15}
/>
</View>
</View>
</View>
</View>
Style:
const styles = StyleSheet.create({
container: {
display: 'flex',
flexDirection: 'column'
},
headerContainer: {
flex: 0.5
},
inputsContainer: {
flex: 1,
display: 'flex',
flexDirection: 'row'
},
leftContainer: {
flex: 1
},
rightContainer: {
flex: 1
}
inputWrapper: {
width: '48%',
flexDirection: 'row',
justifyContent: 'space-around',
borderBottomWidth: 1,
borderBottomColor: 'black',
margin: 10,
},
input: {
flexGrow: 1,
paddingLeft: 5,
},
header: {
color: 'red',
fontWeight: 'bold',
fontSize: 22,
},
}
I manage to solve this by removing the following line:
const styles = StyleSheet.create({
twoColumns: {
flex: 1,
}
}
Layout overlapping on text input in react native as you can see in the images.
I have experimented with keyboardAvoidingView and keyboardAwareScrollView but I just cant fix it or I am implementing them incorrectly.
How to avoid keyboard from changing your flex layout? What's the industry standard? keyboardAvoidingView? I find that inconsistent or have not understood it conceptually. My approach usually is wrapping the entire code with either keyboardAwareScrollView or keyboardAvoidingView. Do I only do it over the textInput component?
const InterviewAlignment = ({navigation}) => {
return (
<View style={styles.container}>
<View style={styles.header}>
</View>
<View style={styles.main}>
<View
style={{
flex: 1,
justifyContent: 'space-between',
}}>
<View
style={{
flex: 1,
backgroundColor: 'red',
}}>
</View>
<View style={styles.modeContainer}>
</View>
<View style={{flex: 1}}>
<Text style={styles.text}>Link Address</Text>
<TextInput
style={styles.linkInput}
/>
</View>
</View>
<View style={{flex: 1, backgroundColor: 'red'}}>
</View>
<View style={{flex: 1.5, backgroundColor: 'black'}}>
</View>
</View>
</View>
);
};
//RFValue is just a responsive wrapper
const styles = StyleSheet.create({
container: {
flex: 1,
},
header: {
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: RFValue(16),
minHeight: RFValue(56),
backgroundColor: '#B1C59B',
},
text: {
fontSize: RFValue(15),
color: 'black',
},
main: {
flex: 1,
padding: 15,
},
modeContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'flex-start',
flex: 1,
backgroundColor: 'yellow',
},
linkInput: {
marginHorizontal: RFValue(40),
padding: 0,
paddingTop: 2,
borderBottomWidth: RFValue(1),
borderBottomColor: '#C4C4C4',
},
});
You can set height instead of flex in styling
Is it possible to have the following layout (without resorting to absolute positioning)?
|...........centreElement.......RightElement|
So as if the justifyContent was 'space-between' but without the first element. I also thought to 'justifyContent: 'centre'' and then try 'justifySelf: 'flex-end' on the RightElement but this property does not exist.
<View styles={styles.container}>
<Text>centreElement</Text>
<Text>RightElement</Text>
</View>
container: {
flexDirection: 'row',
justifyContent: 'center'
}
There is mistake in your code please update code from <View styles={styles.container}> to <View style={styles.container}>
Please try the following code:
<View style={styles.container}>
<View style={{ flex: 1 }} />
<View style={{ flex: 1 }}>
<Text>centreElement</Text>
</View>
<View style={{ flex: 1 }}>
<Text>RightElement</Text>
</View>
</View>
An ideal solution without using absolute. The CentreElement text might not comes at the center, but without absolute, you cannot align it either, since you are not giving the content to take up the 100% width which is needed to rightly align the content to center. Following steps can give you the satisfying results:
Just give out flexDirection: 'row' to the container
Give both the text CentreElement and RightElement 50% width
Align both of them to right, and done!!
Code:
export default function App() {
return (
<View style={styles.container}>
{/* Txt container*/}
<View style={styles.textContainer}>
<Text style={styles.text}>
CentreElement
</Text>
<Text style={styles.text}>
RightElement
</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
textContainer: {
width: '100%',
flexDirection: 'row'
},
text: {
textAlign: 'right',
width: '50%',
fontSize: 16,
fontWeight: 'bold'
}
});
Result:
I have a React-Native view where I have to buttons side by side from each other.
What is happening is when I click Back, it does what it is supposed to do: console.log(-1)
However, when I click on Next, the console.log of "Back" is being activated almost 70% of the time and only 30% of the time it shows a console.log(+1)
I have no idea why this is happening. Here is a screenshot of what is rendered. Left side is what you see from the code below and the right side is what you see if I add a red border to styles.footerButtonContainer.
footerButtonContainer: {
flex: 1,
flexWrap: 'wrap',
alignItems: 'flex-start',
justifyContent: 'center',
flexDirection:'row',
borderWidth:1,
borderColor:"red"
},
What is even weirder is, if I add this border, then the above issue completely disappears and the buttons act as they are supposed do.
Code
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
backgroundColor: "#ecf0f1",
paddingBottom:30
},
footer: {
position: 'absolute',
left: 0,
bottom: 0,
width:"100%",
height:55,
backgroundColor:"#fff",
},
footerButtonContainer: {
flex: 1,
flexWrap: 'wrap',
alignItems: 'flex-start',
justifyContent: 'center',
flexDirection:'row',
},
footerItem: {
flex:1,
flexWrap: 'wrap',
alignItems: 'center',
justifyContent: 'center',
flexDirection:'row',
borderWidth:1,
borderColor:"#F1F0F4",
height:55,
width:"100%"
},
footerIcon: { marginTop:-5 },
footerText: { marginTop:-5, textAlign:"center", fontSize:24, fontWeight:"400", color:"#6B6D77", borderWidth:0, borderColor:"red" }
});
render() {
return (
<View style={{ height:"100%" }}>
<ScrollView ref="scrollWindow" style={{ paddingTop:"5%", marginBottom:56}}>
<View style={styles.container}>
</View>
</ScrollView>
<View style={styles.footer}>
<View style={{height:5, backgroundColor:"#E8E8EA", width: "25%"}} />
<View style={styles.footerButtonContainer}>
<TouchableHighlight underlayColor='#fff' style={styles.footerButtonContainer} onPress={() => {
console.log("-1");
} }>
<View style={styles.footerItem}>
<Icon containerStyle={styles.footerIcon} name="chevron-left" color="#000" type="solid" size={24} />
<Text style={[styles.footerText, {marginLeft: 10}]}>Back</Text>
</View>
</TouchableHighlight>
<TouchableHighlight underlayColor='#fff' style={styles.footerButtonContainer} onPress={() => {
console.log("+1");
}}>
<View style={styles.footerItem}>
<Text style={[styles.footerText, {marginRight: 10, color:"#000"}]}>Next</Text>
<Icon containerStyle={styles.footerIcon} name="chevron-right" color="#000" type="solid" size={24} />
</View>
</TouchableHighlight>
</View>
</View>
</View>
);
}
Your code misses the last close view tag (</View>). I run your code in the expo and seems that it is working correctly:
https://snack.expo.io/HJBsAIOFQ
Button is not visible in the android.
Render Method:
render() {
return (
<View style = { styles.container }>
<View style = { styles.form }>
<Image source={require('../../assets/icon.png')} style = {styles.loginIcon}/>
<View style = { styles.form }>
<TextInput style={ styles.input } onChangeText={(username) => this.setState({username})} placeholder="Username"/>
<TextInput style={ styles.input } onChangeText={(password) => this.setState({password})} placeholder="Password"/>
<View style={ styles.buttonView }>
<Button title="Login" color="#197DC1" style={ styles.primaryBttn } onPress={this.validateCredentialsAndLogin.bind(this)}/>
</View>
<Text>
Forgot Password?
</Text>
</View>
</View>
</View>
);
}
CSS:
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#ECF0F1'//#F5FCFF
},
form :{
flex:1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'white',
flexDirection: 'column',
marginTop: '25%',
marginBottom:'25%',
paddingTop:'10%',
width:'90%',
height:'40%'
},
input:{
height:40,
marginBottom:10,
borderBottomColor: 'black',
width:'100%'
},
loginIcon:{
width:80,
height:80,
},
buttonView:{
flex:1,
width:'100%',
height:50
}
Even though there is flex for parent and height and color for button still not able to view button.
PS: I am new to react-native correct me If I am wrong with the styles or anything else.
Looks like issue is with your styling. You can see your button simply replacing your form style with following:
form:{
flex:1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'white',
flexDirection: 'column',
width:'90%',
height:'40%'
},
You should first learn how to style using flexbox(link).