React Native Button Not Visible Android - react-native

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).

Related

how to use inline flex property in react native

Here's what I'm trying to achieve:
I want to display inline all these three elements inside each Block.
Here's what I have done so far:
export default function App() {
const cheers = [
'cheers',
'high five'
]
return (
<Block style={{ flex: 1,
flexDirection: "row",
alignContent: "space-between",
marginTop: 50}}
>
<Block style={{ flex: 2, justifyContent: 'center'}}>
<Text style={{marginRight: 5}}>Send a</Text>
</Block>
<Block style={[styles.dropdownsRow, {flex: 2}]}>
<SelectDropdownMenu
data={cheers}
onSelect={(selectedItem, index) => {
console.log(selectedItem, index);
}}
defaultButtonText={'Cheers'}
buttonTextAfterSelection={(selectedItem, index) => {
return selectedItem.name;
}}
rowTextForSelection={(item, index) => {
return item.name;
}}
buttonStyle={styles.dropdown1BtnStyle}
buttonTextStyle={styles.dropdown1BtnTxtStyle}
renderDropdownIcon={isOpened => {
return <FontAwesome name={isOpened ? 'chevron-up' : 'chevron-down'} color={'#8898aa'} size={10} />;
}}
dropdownIconPosition={'right'}
dropdownStyle={styles.dropdown1DropdownStyle}
rowStyle={styles.dropdown1RowStyle}
rowTextStyle={styles.dropdown1RowTxtStyle}
/>
</Block>
<Block style={{flex: 2, justifyContent: 'center' }}>
<Text style={{marginLeft: 5, marginRight: 5}}>to</Text>
</Block>
<Block style={{ justifyContent: 'center'}}>
<Input
right placeholder="Type your custom question here."
iconContent={<Block />}
/>
</Block>
);
}
const styles = StyleSheet.create({
dropdownsRow: {justifyContent: 'center', maxWidth: '10%'},
dropdown1BtnStyle: {
height: 45,
backgroundColor: '#edeff2',
borderRadius: 5,
borderColor: '#444',
},
dropdown1BtnTxtStyle: {color: '#8898aa', textAlign: 'left', fontSize: 14},
dropdown1DropdownStyle: {backgroundColor: '#EFEFEF'},
dropdown1RowStyle: {backgroundColor: '#EFEFEF', borderBottomColor: '#C5C5C5'},
dropdown1RowTxtStyle: {color: '#444', textAlign: 'left'}
});
I'm new to react native and don't know how flex property works.
Here's my sample output:
The whole code found here whole code
You can use Yoga layout website to find your desired UI and then migrate your styles to your code.

How to define height to an element in react native in android?

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,
}
}

Keyboard changing flex layout in react native with textInput

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

Wrap text inside view without overflowing

I am new to react native. I am trying to set the text in a view, but the text is overflowing outside the view like in the image below. Tried flexWrap:'wrap and flexShrink:1 but it is also not working. I have implemented as follows:
<View style={styles.container}>
<FlatList
data={this.state.data}
renderItem={({ item, index }) => (
<TouchableOpacity onPress={() => navigate('BlogDetails', item)}>
<Card style={{flexDirection:'row',flexShrink:1}}>
<CardSection>
<View style={styles.thumbnailContainerStyle}>
<Image
style={styles.thumbnailStyle}
source={{ uri: item.imagepath }}
/>
</View>
<CardSection>
<View style={styles.headerContentStyle}>
<Text numberOfLines={5} style={styles.headerTextStyle}>{item.news_title}</Text>
{/* <Text>{item.blog_description}</Text> */}
</View>
</CardSection>
</CardSection>
</Card>
</TouchableOpacity>
)}
keyExtractor={(item, index) => index.toString()}
>
</FlatList>
</View>
My style :
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#FFF',
minHeight: 1,
minWidth: 1,
},
thumbnailContainerStyle: {
justifyContent: 'center',
alignItems: 'center',
marginRight: 10
},
headerContentStyle: {
flexDirection: 'column',
flexWrap:'wrap',
flexShrink:1,
justifyContent: 'center',
alignItems: 'flex-start'
},
thumbnailStyle: {
width: 130,
height: 130,
resizeMode:"contain"
},
imageStyle: {
height: 100,
flex: 1,
width: null
},
cardStyle:{
width:'100%',
height:200,
},
headerTextStyle:{
fontSize:20,
flexWrap: 'wrap',
flexShrink: 1
},
infoText: {
fontSize: 14
}
});
Try this :
Add flexShrink:1 in parent wrapper and in text component do flex:1 and flexWrap:'wrap'
use only flexWrap : 'wrap' for wrap your text use this css for your view and text
headerContentStyle: {
flexWrap : 'wrap'
},
headerTextStyle:{
fontSize:20,
},

React-Native TouchableHighlight activates wrong button when next to each other

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