Creating Simple Grid Layout in React Native - react-native

I'm trying to create the following layout in my React Native app but haven't been able to get it right. The phone number, address and schedules are currently sitting in the middle of the screen and the "Interactive section" is at the bottom of the screen.
What am I doing wrong here? I'd appreciate some help with this layout. Thank you!
BTW, in the following image, I put the borders to show you what I want to achieve. I actually don't want to see any borders around contact info, schedule or interactive sections. The whole thing should just float on white background.
Here's the code in my component:
<View style={styles.container}>
<View style={styles.title}>
<Text style={styles.titleText}>Global Enterprises</Text>
</View>
<View style={styles.contactInfoSection}>
<View style={styles.phoneNumberSection}>
<View>
<Text>212.555.1234</Text>
</View>
<View>
<Text>1 Global Way</Text>
<Text>New York, NY 12345</Text>
</View>
</View>
<View style={styles.scheduleSection}>
<Text>Mon - Fri 8:30 - 5:30</Text>
<Text>Sat - Sun Closed</Text>
</View>
</View>
<View>
<Text style={styles.interactSection}>Other stuff</Text>
</View>
</View>
Here's my stylesheet for this particular screen:
const styles = StyleSheet.create(
{
contactInfoSection: {
alignContent: 'flex-start',
alignItems: 'flex-start',
alignSelf: 'flex-start',
flex: 2,
flexDirection: 'row',
marginTop: 20
},
container: {
alignContent: 'flex-start',
flex: 1,
flexGrow: 1,
padding: 20
},
interactSection: {
flex: 3
},
locationSection: {
alignItems: 'center',
flex: 1
},
phoneNumberSection: {
alignContent: 'center',
alignItems: 'center',
alignSelf: 'center',
flex: 1
},
scheduleSection: {
alignContent: 'center',
alignItems: 'center',
alignSelf: 'center',
flex: 1
},
title: {
alignItems: 'center',
flex: 1,
marginTop: 5
},
titleText: {
fontSize: 17,
fontWeight: 'bold'
}
}
);
export { styles };

You need to update contactInfoSection and title style. Please amend
This
contactInfoSection: {
alignContent: 'flex-start',
alignItems: 'flex-start',
alignSelf: 'flex-start',
flex: 2,
flexDirection: 'row',
marginTop: 20,
},
title: {
alignItems: 'center',
flex: 1,
marginTop: 5,
},
to this
contactInfoSection: {
flexDirection: 'row',
marginTop: 20,
},
title: {
alignItems: 'center',
marginTop: 5,
},
Good Luck

Related

Grid list with list items of different width

I'm trying to achieve this design
and so far my output looks like this
I can't figure out how to force the list item to take up the full row width
this is my code:
<View style={styles.list}>
{listData.map(item => {
return (
<Pressable style={styles.option}>
<Text style={styles.optionTxt}>{item.name}</Text>
</Pressable>
);
})}
</View>
const styles = StyleSheet.create({
list: {
width: '85%',
alignSelf: 'center',
flexWrap: 'wrap',
flexDirection: 'row',
justifyContent: 'flex-start',
},
option: {
backgroundColor: COLORS.backgroundText,
borderRadius: 5,
paddingVertical: 10,
marginBottom: 10,
justifyContent: 'center',
alignItems: 'center',
marginEnd: 10,
},
optionTxt: {
textAlign: 'center',
minWidth: '30%',
paddingHorizontal: 13.5,
},
})
finally fixed it , just added flexGrow: 1 to styles.option

Top align elements from the bottom view

I have two Views under my main View
<View style={styles.container}>
<View style={styles.header}>
<Image
resizeMode="contain"
source={require("../assets/logo-large.png")}
style={styles.logo}
/>
</View>
<View style={styles.content}>
<TextInput
style={styles.input}
onChangeText={(email) => setEmail(email)}
defaultValue={email}
placeholder="Your email"
autoCompleteType="email"
textContentType="emailAddress"
></TextInput>
</View>
My code for styling looks like this:
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
},
header: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "gold",
},
content: {
flex: 2,
justifyContent: "center",
alignItems: "center",
backgroundColor: "lightblue",
},
});
What I want to achieve is to have a header with logo at the top of the screen and to have the content below, but I want to have all elements in content to be top aligned. At the moment those elements are center aligned in content.
Can someone help with this?
Since your main view defaults to flex-direction = column then your content style justifyContent: "center", will arrange elements from content at vertical middle and alignItems: "center" will make them horizontal center. You need to remove those to keep the content elements to start from the top.
Please update your styles to the following.
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
},
header: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "gold",
},
content: {
flex: 2,
alignItems: "center",
backgroundColor: "lightblue",
},
});
Good Luck
I have adjusted it using justifyContent on header and footer styles.
header: {
flex: 1,
justifyContent: "flex-end",
alignItems: "center",
backgroundColor: "gold",
paddingBottom: "10%",
},
content: {
flex: 2,
justifyContent: "flex-start",
alignItems: "center",
backgroundColor: "lightblue",
},

How to align texts to each Textinput - React Native

I want those texts in the left to be aligned to my Textinputs on the right.
I am fetching textinputs and texts from api so they are dynamic. I need to display each text on the left side of the texinput.
Here is my code:
textfieldsObject = () => {
const obje = this.props.navigation.state.params.item;
var keyvalue_to_json = JSON.parse(obje.keyValues);
var textinputName = [];
var foundTextFields = [];
for (let i = 0; i < keyvalue_to_json.inputFields.length; i++) {
if (keyvalue_to_json.inputFields[i].type === 'textfield') {
foundTextFields.push(<TextInput placeholder={keyvalue_to_json.inputFields[i].placeholderText} style={styles.inputFields}
onEndEditing={(e) => {
keyvalue_to_json.inputFields[i].inputValues = e.nativeEvent.text;
this.myInputFields.myTextFields[i] = keyvalue_to_json.inputFields[i];
}}
></TextInput>) &&
textinputName.push(<Text style={{ textAlign: 'left', flex: 2 }}>{keyvalue_to_json.inputFields[i].title}</Text>)
}
}
return (
<View style={{ flex: 1, flexDirection: 'row', justifyContent: 'space-between' }}>
<View style={{ flex: 1 }}>
{textinputName}
</View>
<View style={{ flex: 1 }}>
{foundTextFields}
</View>
</View>
)
}
I have write small piece of code . You can try this code. It may solve your problem. Please also check this code into snack and directly run to scan QR using Expo app. Please c
import * as React from 'react';
import { TextInput, Text, View, StyleSheet } from 'react-native';
import { Constants } from 'expo';
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<View
style={{
justifyContent: 'center',
alignSelf: 'center',
alignItems: 'center',
alignContent: 'center',
flexDirection: 'row',
}}>
<Text style={{ textAlign: 'center', flex: 1, padding: 15 }}>
{'Full name'}
</Text>
<TextInput
placeholder='Enter Your Full Name'
placeholderTextColor='#303030'
style={{
borderWidth: 1,
borderColor: '#000',
flex: 1,
padding: 15,
}}/>
</View>
<View
style={{
justifyContent: 'center',
alignSelf: 'center',
alignItems: 'center',
alignContent: 'center',
flexDirection: 'row',
}}>
<Text style={{ textAlign: 'center', flex: 1, padding: 15 }}>
{'CRP Number'}
</Text>
<TextInput
placeholder='Enter Your CRP Number'
placeholderTextColor='#303030'
style={{
borderWidth: 1,
borderColor: '#000',
flex: 1,
padding: 15,
}}/>
</View>
<View
style={{
justifyContent: 'center',
alignSelf: 'center',
alignItems: 'center',
alignContent: 'center',
flexDirection: 'row',
}}>
<Text style={{ textAlign: 'center', flex: 1, padding: 15 }}>
{'Company Name'}
</Text>
<TextInput
placeholder='Enter Your Company Name'
placeholderTextColor='#303030'
style={{
borderWidth: 1,
borderColor: '#000',
flex: 1,
padding: 15,
}}/>
</View>
<View
style={{
justifyContent: 'center',
alignSelf: 'center',
alignItems: 'center',
alignContent: 'center',
flexDirection: 'row',
}}>
<Text style={{ textAlign: 'center', flex: 1, padding: 15 }}>
{'Company CVR '}
</Text>
<TextInput
placeholder='Enter Your company CVR'
placeholderTextColor='#303030'
style={{
borderWidth: 1,
borderColor: '#000',
flex: 1,
padding: 15,
}}>
{'Enter Your company CVR'}
</TextInput>
</View>
<Text style={{ color:'#fff',borderRadius:50, width:300, height:50, textAlign:'center', padding:15, marginTop:70,backgroundColor:'#00b5ec'}}>{"Click to sign in"}</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
marginTop: 10,
marginRight:1
},
});
Updated Answer Please Check
import * as React from 'react';
import { FlatList, TextInput, Text, View, StyleSheet } from 'react-native';
import { Constants } from 'expo';
// You can import from local files
import AssetExample from './components/AssetExample';
// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
jsonData: [
{ id: 1, textName: 'Hello', textInputName: 'Full Name' },
{
id: 2,
textName: 'Hello',
textInputName: 'Last Name',
},
{
id: 3,
textName: 'Hello',
textInputName: 'Date of Birth',
},
{
id: 4,
textName: 'Hello',
textInputName: 'Address',
},
{ id: 1, textName: 'Hello', textInputName: 'Full Name' },
{
id: 2,
textName: 'Hello',
textInputName: 'Last Name',
},
{
id: 3,
textName: 'Hello',
textInputName: 'Date of Birth',
},
{
id: 4,
textName: 'Hello',
textInputName: 'Address',
},
{ id: 1, textName: 'Hello', textInputName: 'Full Name' },
{
id: 2,
textName: 'Hello',
textInputName: 'Last Name',
},
{
id: 3,
textName: 'Hello',
textInputName: 'Date of Birth',
},
{
id: 4,
textName: 'Hello',
textInputName: 'Address',
},
],
};
}
render() {
return (
<View style={styles.container}>
<FlatList
style={{ width: '100%', flex: 1 }}
data={this.state.jsonData}
showsVerticalScrollIndicator={false}
renderItem={({ item }) => (
<View
style={{
justifyContent: 'center',
alignSelf: 'center',
alignItems: 'center',
alignContent: 'center',
flexDirection: 'row',
}}>
<Text style={{ textAlign: 'center', flex: 1, padding: 15 }}>
{item.textName}
</Text>
<TextInput
placeholder={item.textInputName}
placeholderTextColor="#303030"
style={{
borderWidth: 1,
borderColor: '#000',
flex: 1,
padding: 15,
}}
/>
</View>
)}
/>
<Text
style={{
color: '#fff',
borderRadius: 50,
width: 300,
height: 50,
textAlign: 'center',
padding: 15,
marginTop: 10,
backgroundColor: '#00b5ec',
marginBottom: 20,
}}>
{'Click to sign in'}
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
paddingTop: Constants.statusBarHeight,
marginTop: 10,
marginRight: 1,
},
});
I have also added updated snack expo please check
https://snack.expo.io/#vishal7008/anxious-candy
I recomend you to create Views with the same size. Once this is done, put inside the Text compnent with tha margin you want or the view style properties to align the text.
Either with View Style properties or Text, if the View containing the 2 texts are the same size the texts components wioll be aligned to each other.
As you can see in the code below, what I do is wrapping your text components with a
view, and both views wrapping the text has the exact same size.
Once this is done, the Text components inside these views has width and height 100%, so it will be the same size as the view. This way you will have 2 components aligned in your main View with flex-direction = row, and the text will be aligned.
Copy the code below and set up the size of the views inside the style props of both views where I commented the code
<View
style={{
flex: 1,
flexDirection: 'row',
justifyContent: 'space-between'
}}
>
<View
style={{
flex: 1,
//Here You set up with and height
}}
>
<Text
style={{
height: '100%',
width: '100%',
textAlign: 'center'
}}
>
{textinputName}
</Text>
</View>
<View
style={{
flex: 1,
//And here you set up the same with and height than the first view
}}
>
<Text
style={{
height: '100%',
width: '100%',
textAlign: 'center'
}}
>
{foundTextFields}
</Text>
</View>
</View>

how set a View background color to red and it should be transparent

I have a Imagebackground for my parentconatiner, I want to set backgroundColor of one of my childView to red and it should be transparent so that the parent container image is visible
it should be like this
sample image
here is my code
import React, { Component } from 'react';
import { View, Text, Image, StyleSheet } from 'react-native';
import colors from '../styles/colors';
import strings from '../localization/strings';
class Appointments extends Component {
render() {
return (
<View style={styles.Container}>
<View style={{ backgroundColor: 'rgba(52, 0, 0, 0.8)', shadowOpacity: 0.2, padding: 5 }}>
<View style={styles.childContainer}>
<Image style={{ justifyContent: 'flex-start', alignItems: 'flex-start' }} source={require('../assets/calender-Icon.png')}
/>
<View style={styles.dateTextContainer}>
<Text style={styles.childText}>Appointment</Text>
<Text style={[styles.childText, { fontSize: 26 }]}>Oct24, 2018</Text>
<Text style={[styles.childText, { fontSize: 16, fontWeight: 'bold' }]}>10:30 am</Text>
</View>
</View>
</View>
<View style={styles.borderText}>
<View style={{ flexDirection: 'row' }}>
<Image source={require('../assets/add-Icon.png')} />
<Text style={[styles.itemName, { fontSize: 16 }]}>New</Text>
</View>
<View style={{ flexDirection: 'row' }}>
<Image source={require('../assets/seeAll-Icon.png')} />
<Text style={[styles.itemName, { fontSize: 16 }]}>See All</Text>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
Container: {
backgroundColor: colors.white,
borderRadius: 4,
borderWidth: 1,
borderColor: colors.red
},
childContainer: {
justifyContent: 'space-between',
alignItems: 'flex-start',
margin: 15,
flexDirection: 'row',
},
textStyle: {
fontSize: 16,
color: colors.black,
justifyContent: 'flex-end',
alignItems: 'flex-end',
fontWeight: 'bold'
},
childText: {
color: colors.white,
fontSize: 18,
},
dateTimeContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
marginHorizontal: 10,
alignItems: 'flex-end',
},
dateTextContainer: {
flexDirection: 'column',
marginHorizontal: 10,
justifyContent: 'flex-end',
alignItems: 'flex-end',
},
listItem: {
borderWidth: 1,
borderColor: colors.pureBlue,
alignItems: 'center',
justifyContent: 'center',
marginHorizontal: 5,
paddingVertical: 5,
margin: 30
},
itemName: {
fontSize: 14,
color: colors.black,
margin: 2,
paddingLeft: 4
},
border: {
borderRadius: 4,
borderWidth: 1,
borderColor: colors.light_gray,
marginHorizontal: 20
},
imageText: {
flexDirection: 'column',
margin: 10,
flexWrap: 'wrap',
flex: 1
},
borderText: {
flexDirection: 'row',
justifyContent: 'space-between',
margin: 10
}
});
export default Appointments;
I have tried rgba and opacity but still not working
please help me how to do this
You need to remove the backgroundColor of the Container to allow for transparency to go all the way through to the parent container, otherwise the transparency will only allow to see the white background behind
Container: {
// backgroundColor: colors.white,
...
},

React native nested flex

I am using react-native-elements. I am having difficulty in layout system of react-native.
<View style={styles.container}>
{canAccessCamera ? (
<Card style={styles.userInfo}>
<Avatar medium source={Logo} rounded />
<Text>Username</Text>
</Card>
) : (
<Text>Give me access to camera</Text>
)}
</View>
Style:
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'stretch',
justifyContent: 'flex-start',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
},
userInfo: {
display: 'flex',
flex: 1,
flexDirection: 'column',
alignItems: 'stretch',
justifyContent: 'flex-start',
},
});
I want items inside Card located by column not by row.
This is my result:
I want that Username stay in the right of the Avatar.
Change just this style
userInfo: {
display: "flex",
alignItems: "center",
flexDirection: "row",
justifyContent: "flex-start"
},
container:{
flex: 1,
alignItems: "stretch",
justifyContent: "flex-start",
paddingTop: 5,
backgroundColor: "#ecf0f1",
alignItems: "center"