KeyboardAvoidingView is not working when using flex - React Native - react-native

I have a Registration form with one form header and 3 input fileds.
When I am using KeyboardAvoidingView the deisgn is getting distorted.
My code is below:-
Index.js code:-
const Registration = (props)=>{
return (
<SafeAreaView style={styles.container}>
<Form submitForm={submitForm} />
</SafeAreaView>
)
}
Form.js
const Form = (props)=>{
return (
<View style={{flex:1}}>
<View style={[styles.headerTextContainer]}>
<Text style={styles.headerText}>
Enter details to get started
</Text>
</View>
<KeyboardAvoidingView behavior="height" style={styles.formContainer}>
<View style={{flex:.20,marginBottom:25}}>
<View style={{flex:1.5,}}>
<Text style={styles.labelText}>First Name</Text>
</View>
<View style={{flex:3}}>
<TextInput
value={userinfo.firstName}
onChangeText={(text)=>setFormValue('firstName',text)}
style={styles.input}
/>
</View>
<View style={{flex:1}}>
<Text style={styles.error}>{formError.first_name_error}</Text>
</View>
</View>
</KeyboardAvoidingView>
</View>
)
}
Corresponding css:-
const styles = StyleSheet.create({
headerTextContainer:{
flex:.15,
flexDirection:'column-reverse'
},
headerText:{
color:'#000',
fontSize:23,
textAlign:'center'
},
formContainer:{
flex:.75,
padding:40
},
input:{
height: 50,
borderColor: '#DFE4EB',
borderWidth: 1,
borderRadius:10
},
labelText:{
color:'#BDBDBD',
// marginLeft:12,
marginBottom:10
},
buttonContainer:{
flex:.20,
alignContent:'center',
justifyContent:'center',
paddingLeft: 50,
paddingRight: 50,
}
})
I am very new to react native. I am unable to find the problem in this code.I have tried changing the behaviour, but it does nothing.It will be helpful if any one can guide me about what I am doing wrong
Here are the Images of the screen:-

Related

React Native RenderItem not working with custom renderItem

I created a custom renderItem using the React Native documentation as an example and it is not working, however, when using a simple component (commented in the script), it is working perfectly. I have tried to change some things around, but I can't make it work.
For the flatlist, I based my code on the react native example at https://reactnative.dev/docs/flatlist, using the flat-list-simple example, calling the function in the render item as renderItem={Function} but react-native does not recognize it, I appreciate any guidance on what I could change or if I am doing the whole thing wrong, thank you!
import { StyleSheet, Text, View, TouchableOpacity, FlatList } from 'react-native'
import React from 'react'
import Ionicons from 'react-native-vector-icons/Ionicons';
import VisitasHeader from './VisitasHeader';
const VisitasCard = ({visitas,titulo}) => {
// renderItem for FlatList
const CardFunction = (item) => {
<View style={{marginBottom:10}}>
<View style={styles.card_top}>
<View style={styles.holder}>
<View style={styles.icon}>
<Ionicons
name='ios-people-circle-sharp'
color='white'
size={48}
/>
</View>
<View style={styles.visitante}>
<Text style={styles.text_nombre}>{item.nombre} </Text>
<Text style={styles.text_cedula}>e-0000001</Text>
<View>
<Text style={styles.text_fecha}>2 de enero de 2022</Text>
</View>
</View>
</View>
</View>
<View style={styles.card_bottom}>
<TouchableOpacity style={styles.button_accept}>
<Text style={styles.button_text}>Aceptar</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.button_reject}>
<Text style={styles.button_text}>Rechazar</Text>
</TouchableOpacity>
</View>
</View>
}
return (
<FlatList
ListHeaderComponent={<VisitasHeader title={titulo} />}
data={visitas}
keyExtractor={item => item.id}
renderItem={CardFunction}
// renderItem={({item}) => <View><Text>{item.nombre}</Text></View>} WORKS PERFECT WHEN USING THIS
ListEmptyComponent={<Text style={{textAlign:'center',paddingTop:10}}>No tiene visitas por aprobar.</Text>}
/>
)
}
export default VisitasCard
const styles = StyleSheet.create({
button_accept:{
backgroundColor:'green',
// margin:12,
padding:10,
borderRadius:12,
},
button_text:{
color:'white',
fontWeight:'bold',
},
button_reject:{
backgroundColor:'red',
// margin:12,
padding:10,
borderRadius:12,
},
card_bottom:{
height:50,
flexDirection:'row',
alignItems:'center',
justifyContent:'center',
borderWidth:1,
borderColor:'#3959ea',
borderBottomLeftRadius:8,
borderBottomRightRadius:8,
justifyContent:'space-evenly'
},
card_top:{
height:100,
backgroundColor:'#3959ea',
borderTopLeftRadius:8,
borderTopRightRadius:8,
alignItems:'center',
justifyContent:'center',
},
holder:{
flexDirection:'row',
},
icon:{
padding:10,
alignItems:'center',
margin:12,
},
text_cedula:{
color:'white',
fontSize:18,
textTransform:'uppercase',
fontWeight:'600',
},
text_nombre:{
color:'white',
fontSize:24,
textTransform:'capitalize',
fontWeight:'800',
// ellipsizeMode:'head',
// numberOfLines:1,
},
text_fecha:{
color:'white'
},
visitante:{
flex:2,
justifyContent:'center',
alignItems:'flex-start',
}
})
try add { item } and add return
const CardFunction = ({ item }) => {
return (
<View style={{ marginBottom: 10 }}>
<View style={styles.card_top}>
<View style={styles.holder}>
<View style={styles.icon}>
<Ionicons
name="ios-people-circle-sharp"
color="white"
size={48}
/>
</View>
<View style={styles.visitante}>
<Text style={styles.text_nombre}>{item.nombre} </Text>
<Text style={styles.text_cedula}>e-0000001</Text>
<View>
<Text style={styles.text_fecha}>2 de enero de 2022</Text>
</View>
</View>
</View>
</View>
<View style={styles.card_bottom}>
<TouchableOpacity style={styles.button_accept}>
<Text style={styles.button_text}>Aceptar</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.button_reject}>
<Text style={styles.button_text}>Rechazar</Text>
</TouchableOpacity>
</View>
</View>
);
};

text above row of buttons in React Native

This should be relatively easy for a React Native expert. I simply wish to display some text, and then under the text should be a row of buttons. Beginning with the row of buttons, this seems to work fine:
constructor(props) {
super(props);
this.state = {
titleText: "Bird's Nest",
bodyText: "This is not really a bird nest."
};
}
render() {
return (
<View style={styles.container}>
<View style={styles.buttonContainer}>
<Button title="Camera"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Start"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Website"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Stop"/>
</View>
</View>
); //return
} //render
const styles = StyleSheet.create({
page:{
flex:1,
backgroundColor: "#000",
paddingTop:50,
color: 'white'
},
container: {
flex: 1,
backgroundColor: "#000",
flexDirection: 'row',
alignItems: 'center',
justifyContent: "center"
},
buttonContainer: {
flex: 1,
},
container1: {
backgroundColor: 'powderblue',
height : 100
},
container2: {
backgroundColor: 'grey',
height : 100
},
baseText: {
fontFamily: "Cochin"
},
titleText: {
fontSize: 20,
fontWeight: "bold",
color: 'white'
}
});
When I attempt to add the text...it is functional however the text is positioned before the row of buttons instead of placement over/above the row of buttons. Here is the same code inclusive of the text:
...
render() {
return (
<View style={styles.container}>
<Text style={styles.baseText}>
<Text style={styles.titleText} onPress={this.onPressTitle}>
{this.state.titleText}
{"\n"}
{"\n"}
</Text>
<Text numberOfLines={5}>{this.state.bodyText}</Text>
</Text>
<View style={styles.buttonContainer}>
<Button title="Camera"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Start"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Website"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Stop"/>
</View>
</View>
); //return
} //render
...
So basically the 'text' is rendered and then the 4 buttons are rendered with everything all 'crushed' together...all within the same row. There probably needs to be some sort of 'column' for the text however I have no idea how to combine 'column' and 'row' (necessary for the buttons) in React Native (within the same page 'container'). Or perhaps there is some other way to do it?
In addition, it would be nice if the row of buttons appeared along the bottom of the device screen instead of centered. I had changed the 'alignItems' in the 'styles.container' to 'baseline' (before I added any text) however that did not accomplish the desired result. I thank you in advance for any advice.
UPDATE:
At this point I have modified my code above and what I have now is something more along the lines of what I am seeking...although not perfect. There is probably a better way however here is what my code looks like now:
import BoxContainer from './BoxContainer';
render() {
return (
<View style={styles.page}>
<BoxContainer style={styles.container1}>
<Text>Bunch of text here.</Text>
</BoxContainer>
<BoxContainer style={styles.container2}>
<View style={styles.container}>
<View style={styles.buttonContainer}>
<Button title="Camera"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Start"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Website"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Stop"/>
</View>
</View>
</BoxContainer>
</View>
); //return
} //render
The code for 'BoxContainer' is here, a resource I found on the Internet somewhere:
import React from 'react';
import { StyleSheet, View} from 'react-native';
const BoxContainer = props => {
return (
<View style={{...styles.boxContainer, ...props.style}}>
{props.children}
</View>
);
};
const styles = StyleSheet.create({
boxContainer:{
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.8,
shadowRadius: 2,
height:200,
margin: 20,
alignItems: 'center',
justifyContent: 'center'
}
});
export default BoxContainer;
As mentioned, this is close to what I am seeking however I wish I could find a way to do it without the structured box containers...or perhaps that is necessary in React Native...?

How to rotate Fontisto icon in react native

I Import Fontisto import { Fontisto } from "#expo/vector-icons";
Add an Icon <Fontisto style={styles.windDirectionArrow} name='arrow-up' />
and the styling to rotate it
const styles = StyleSheet.create({
windDirectionArrow: {
fontSize: 40,
transform: [{ rotate: "90deg" }],
},
});
But the transform is not working, does anyone know any other solutions for this?
The view I am rendering looks like this
return (
<View style={styles.container}>
{error ? (
<View style={styles.centeredContainer}>
<Text style={styles.text}>Unable to load weather data</Text>
<Text style={styles.text}>{error}</Text>
</View>
) : isLoading ? (
<View style={styles.centeredContainer}>
<Text style={styles.text}>Loading Weather Data</Text>
<ActivityIndicator
style={styles.activityIndicator}
size='large'
color='rgb(255,179,25)'
/>
</View>
) : (
<View style={[styles.centeredContainer]}>
<Fontisto style={styles.text} name={icon} />
<Text style={styles.text}>{temperature}˚C</Text>
<Text style={styles.text}>
<Fontisto name='arrow-up' style={styles.windDirectionArrow} />
{windSpeed}mph
</Text>
<Text style={styles.text}>{weatherCondition}</Text>
</View>
)}
</View>
);
It works if I have the Icon element wrapped in its own but I think that is not a clean solution
<Text style={styles.text}>
<View>
<Fontisto name='arrow-up' style={styles.windDirectionArrow} />
</View>
{windSpeed}mph
</Text>
It works perfectly for me
// With Rotation
<Fontisto name="arrow-up" style={styles.windDirectionArrow} />
// Without Rotation
<Fontisto name="arrow-up" style={styles.withoutRottion} />
// When icon is wrapped inside a View and that View is rotated
<View style={styles.windDirectionArrow}>
<Fontisto name="arrow-up" style={{ fontSize: 40 }} />
</View>
My Styles :
windDirectionArrow: {
fontSize: 40,
transform: [{ rotate: '90deg' }],
},
withoutRottion: {
fontSize: 40,
},
Working Example
And also why don't you use arrow-right from Fontisto
And for Text Side by side
<View style={{ justifyContent: 'center', alignItems: 'center', flexDirection: 'row' }}>
<View style={styles.windDirectionArrow}>
<Fontisto name="arrow-up" style={{ fontSize: 40 }} />
</View>
<Text style={styles.text}>{windSpeed}mph</Text>
</View>

Getting design issues when the device is in PORTRAIT UPSIDEDOWN in React native

I'm working on React native project, My issue is when I try to change the orientation from LANDSCAPE to PORTRAIT getting some design issues especially when the device is turned to PORTRAIT UPSIDEDOWN from LANDSCAPE.
Here Is My Code:
class Exm extends Component {
constructor() {
super();
const init = Orientation.getInitialOrientation();
this.state = {
init,
or: init,
sor: init,
emailId: '',
password:'',
Token:'',
orientation:'',
Spinner:false,
userId:null,
isDisabled:false
};
this._updateOrientation = this._updateOrientation.bind(this);
Orientation.addOrientationListener(this._updateOrientation);
this._updateSpecificOrientation = this._updateSpecificOrientation.bind(this);
Orientation.addSpecificOrientationListener(this._updateSpecificOrientation);
}
_updateOrientation(or) {
// alert("HI")
this.setState({ or });
}
_updateSpecificOrientation(sor) {
//alert("HELLO")
this.setState({ sor });
}
render() {
console.log("Type Of Orientation:"+this.state.init+','+this.state.or+','+this.state.sor);
const { init, or, sor} = this.state;
return (
<View style={styles.container}>
{this.state.sor=="PORTRAIT" ?(
<View style={{flex:1}}>
<Image style={{width:windowSize.width,height:windowSize.height,resizeMode:'cover'}} source={require('./../images/home.png')}>
<View style={{flex:1}}>
<View style={{flex:0.5,justifyContent:"flex-end"}}>
<Text style={{backgroundColor:"transparent",fontSize:17,color:"white",fontWeight:'500',textAlign:'center',marginBottom:45}}>Finest Wines Direct from Sonoma</Text>
<TouchableOpacity onPress={this.onClick.bind(this)}>
<View style={styles.button1}>
<Text style={{fontSize:18,color:"white",fontWeight:"bold",textAlign:'center'}}>BROWSE WEBSITE</Text>
</View>
</TouchableOpacity>
</View>
<View style={{flex:0.5}}>
<TouchableOpacity onPress={this.onClick1.bind(this)}>
<View style={styles.button}>
<Text style={{fontSize:18,color:"white",fontWeight:"bold",textAlign:'center'}}>DAILY DEALS</Text>
</View>
</TouchableOpacity>
</View>
</View>
</Image>
</View>
):
this.state.sor=="UNKNOWN" ? (
<View style={{flex:1}}>
<Image style={{width:windowSize.width,height:windowSize.height,resizeMode:'cover'}} source={require('./../images/home.png')}>
<View style={{flex:1}}>
<View style={{flex:0.5,justifyContent:"flex-end"}}>
<Text style={{backgroundColor:"transparent",fontSize:17,color:"white",fontWeight:'500',textAlign:'center',marginBottom:45}}>Finest Wines Direct from Sonoma</Text>
<TouchableOpacity onPress={this.onClick.bind(this)}>
<View style={styles.button1}>
<Text style={{fontSize:18,color:"white",fontWeight:"bold",textAlign:'center'}}>BROWSE WEBSITE</Text>
</View>
</TouchableOpacity>
</View>
<View style={{flex:0.5}}>
<TouchableOpacity onPress={this.onClick1.bind(this)}>
<View style={styles.button}>
<Text style={{fontSize:18,color:"white",fontWeight:"bold",textAlign:'center'}}>DAILY DEALS</Text>
</View>
</TouchableOpacity>
</View>
</View>
</Image>
</View>
):this.state.sor=="LANDSCAPEUPSIDEDOWN" ? (
<View style={{flex:1}}>
<Image style={{width:windowSize.height,height:windowSize.width}} source={require('./../images/home_landscape.png')}>
<View style={{flex:1}}>
<View style={{flex:0.5,justifyContent:"flex-end"}}>
<Text style={{backgroundColor:"transparent",fontSize:17,color:"white",fontWeight:'500',textAlign:'center',marginBottom:45}}>Finest Wines Direct from Sonoma</Text>
<TouchableOpacity onPress={this.onClick.bind(this)}>
<View style={styles.button1}>
<Text style={{fontSize:18,color:"white",fontWeight:"bold",textAlign:'center'}}>BROWSE WEBSITE</Text>
</View>
</TouchableOpacity>
</View>
<View style={{flex:0.5}}>
<TouchableOpacity onPress={this.onClick1.bind(this)}>
<View style={styles.button}>
<Text style={{fontSize:18,color:"white",fontWeight:"bold",textAlign:'center'}}>DAILY DEALS</Text>
</View>
</TouchableOpacity>
</View>
</View>
</Image>
</View>
):
<View style={{flex:1}}>
<Image style={{width:windowSize.height,height:windowSize.width}} source={require('./../images/home_landscape.png')}>
<View style={{flex:1}}>
<View style={{flex:0.5,justifyContent:"flex-end"}}>
<Text style={{backgroundColor:"transparent",fontSize:17,color:"white",fontWeight:'500',textAlign:'center',marginBottom:45}}>Finest Wines Direct from Sonoma</Text>
<TouchableOpacity onPress={this.onClick.bind(this)}>
<View style={styles.button1}>
<Text style={{fontSize:18,color:"white",fontWeight:"bold",textAlign:'center'}}>BROWSE WEBSITE</Text>
</View>
</TouchableOpacity>
</View>
<View style={{flex:0.5}}>
<TouchableOpacity onPress={this.onClick1.bind(this)}>
<View style={styles.button}>
<Text style={{fontSize:18,color:"white",fontWeight:"bold",textAlign:'center'}}>DAILY DEALS</Text>
</View>
</TouchableOpacity>
</View>
</View>
</Image>
</View>
}
</View>
);
}
}
const styles = StyleSheet.create({
container:{
flex:1,
backgroundColor:'#f5f5f5'
},
button:{
height: 55,
backgroundColor: 'transparent',
borderColor: 'white',
borderWidth: 2,
marginRight:50,
marginLeft:50,
marginTop: 20,
marginBottom:5,
justifyContent:'center',
alignSelf:'stretch',
},
button1:{
height: 55,
backgroundColor: 'transparent',
borderColor: 'white',
borderWidth: 2,
marginRight:50,
marginLeft:50,
marginBottom:5,
justifyContent:'center',
alignSelf:'stretch',
},
buttonText:{
fontSize: 18,
marginTop:5,
marginBottom:5,
textAlign:'center',
color:'white',
textAlignVertical:'center',
},
});
module.exports = Exm;
ScreenShots Here.
Please give me suggestions to solve this issue, Any help much appreciated
Ef.png
Seems like the Module is not updated with ES6 Syntax. Here is a Sample Code you can make use of. Demo
import React, { Component } from 'react';
import { Text, View } from 'react-native';
import OrientationModule from 'react-native-orientation'
class Example extends Component {
constructor() {
super();
this.state = {
orientationStatus: 'PORTRAIT'
};
}
componentWillMount(){
var Orientation = OrientationModule.getInitialOrientation()
this.setState({
Orientation: Orientation
})
}
_orientationDidChange = (orientationStatus) => {
this.setState({orientationStatus})
}
componentDidMount(){
// Add a Listener, every time Orientation changes this method triggers
OrientationModule.addOrientationListener(this._orientationDidChange);
}
componentWillUnmount(){
// Remove the Listener, to avoid setting a State while component was unmounted
OrientationModule.removeOrientationListener(this._orientationDidChange);
}
render() {
// You can Have your Design Logic here
return (
<View style={{flex:1,alignItems:'center',justifyContent:'center'}}>
<Text>{this.state.orientationStatus}</Text>
</View>
)
}
}
module.exports = Example
Why are you doing this in first place, just write component w/o any orientation code and it should rotate automatically. Then if you need some layout adjustments do those like width/height or just check flexbox model more maybe it's already have what you need, while react native mimics flexbox. Can't tell from your description what issue you face.
Also if you want to disable some orientations, it can be done in project config
Android
Android - disable landscape mode?
iOS https://stackoverflow.com/a/24205653/1737158

How to vertically aligned my image to top in my React-native component

I'd like to top aligned my image in React-native. But don't know what to do. Here is my layout and its style:
class layout extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.toolbar}>
<Text style={styles.left}>
Left Button
</Text>
<Text style={styles.title}>
This is my title
</Text>
<Text style={styles.right}>
Right Button
</Text>
</View>
<View style={styles.content}>
<Image style={styles.image}
source={require('./back.jpg')}
resizeMode="contain"
></Image>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
},
toolbar: {
backgroundColor: '#B5AC3a',
height:64,
flexDirection:'row'
},
left: {
marginTop:30,
fontSize: 14,
textAlign: 'center'
},
right: {
marginTop:30,
fontSize: 14,
textAlign: 'center'
},
title: {
marginTop:30,
flex: 1,
fontSize: 14,
textAlign: 'center'
},
content: {
backgroundColor:'#ffecff'
},
image: {
width: Dimensions.get('window').width - 20,
margin: 10,
alignSelf:'center'
}
});
Here is the result:
Apparently there are a lot of space between my image to top of my screen. I tried: flex-start. But doesn't work. What should I do? Thanks.
P.S: It's fine if I use View instead of Image like this:
class layout extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.toolbar}>
<Text style={styles.left}>
Left Button
</Text>
<Text style={styles.title}>
This is my title
</Text>
<Text style={styles.right}>
Right Button
</Text>
</View>
<View style={styles.content}>
<View style={styles.messageBox}>
<Text>This is line one</Text>
<Text>This is line two</Text>
</View>
</View>
</View>
);
}
}
Result:
Your resizeMode='contain' on your image is conflicting with the dimensions you want to define in styles.image.
Simply remove resizeMode='contain' in your <Image> properties and it should work.
Something else: I would strongly advise you, depending on what you're trying to do, to use a ScrollView instead of the simple View when you have extendable content (namely anything that isn't completely static). This seems to be what you need.