Layouting textbox with Flexbox in react native - flexbox

I am trying to layouting my textbox like this
And here is my code that I am trying to do
render() {
return (
<View style={styles.container}>
<View style={styles.row}>
<Headline style={styles.headlineText}>Adresse</Headline>
</View>
<View style={styles.row}>
<Input style={styles.text}
placeholder="Strasse"
/>
<Input style={styles.text}
placeholder="Nr"
/>
</View>
<View style={styles.row}>
<Input style={styles.text}
placeholder="PLZ"
/>
<Input style={styles.text}
placeholder="Stadt"
/>
</View>
<View style={styles.row}>
<Button
onPress={() => this.props.navigation.navigate('PaymentInfo', { name: 'PaymentInfo' })}
title="Weiter"
style={styles.button}
/>
</View>
</View>
);
}
and here is the stylesheet
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'flex-start',
justifyContent: 'flex-start',
backgroundColor: Colors.white,
paddingBottom: normalize(20),
},
row: {
flexDirection: 'row',
alignItems: 'stretch',
justifyContent: 'flex-start',
paddingHorizontal: normalize(20)
},
button: {
marginTop: 20
},
text: {
marginTop: 10
},
headlineText: {
marginTop: normalize(30),
marginBottom: 10,
color: "black"
}});
However I got result like this
Can anyone help me what should I do in order to make my textbox rendered as first image? Many thanks

You can use flex property like this:
<View style={styles.row}>
<Input style={[styles.text,{flex: 4, marginRight: 2}]}
placeholder="Strasse"
/>
<Input style={[styles.text,{flex: 1, marginLeft: 2}]}
placeholder="Nr"
/>
</View>
<View style={styles.row}>
<Input style={[styles.text,{flex: 3, marginRight: 2}]}
placeholder="PLZ"
/>
<Input style={[styles.text,{flex: 7, marginLeft: 2}]}
placeholder="Stadt"
/>
</View>

This is my approach for your solution using flexbox to you can try with this link in repl.it an see the result with expo application on your device repl.it
<script src="//repl.it/embed/K58G/6.js"></script>
Here i paste my code or you can try with link that i gave
This For View
<View style={styles.container}>
<Text style={{fontSize:20}}> Adresses </Text>
<View style={{flexDirection:'row'}}>
<TextInput
placeholder="Strase"
underlineColorAndroid = 'transparent'
style={[styles.text,{flex: 7}]}
/>
<TextInput
placeholder="Nr"
underlineColorAndroid = 'transparent'
style={[styles.text, {flex: 3}]}
/>
</View>
<View style={{flexDirection:'row', marginBottom: 10}}>
<TextInput
placeholder="PLZ"
underlineColorAndroid = 'transparent'
style={[styles.text,{flex: 3}]}
/>
<TextInput
underlineColorAndroid = 'transparent'
placeholder="Stadt"
style={[styles.text,{flex: 7}]}
/>
</View>
<TouchableOpacity style={styles.button}>
<Text style={{fontSize: 20}}>WEITHER</Text>
</TouchableOpacity>
</View>
<View style={styles.container}>
<Text style={{fontSize:20}}> Adresses </Text>
<View style={{flexDirection:'row'}}>
<TextInput
placeholder="Strase"
underlineColorAndroid = 'transparent'
style={[styles.text,{flex: 7}]}
/>
<TextInput
placeholder="Nr"
underlineColorAndroid = 'transparent'
style={[styles.text, {flex: 3}]}
/>
</View>
<View style={{flexDirection:'row', marginBottom: 10}}>
<TextInput
placeholder="PLZ"
underlineColorAndroid = 'transparent'
style={[styles.text,{flex: 3}]}
/>
<TextInput
underlineColorAndroid = 'transparent'
placeholder="Stadt"
style={[styles.text,{flex: 7}]}
/>
</View>
<TouchableOpacity style={styles.button}>
<Text style={{fontSize: 20}}>WEITHER</Text>
</TouchableOpacity>
</View>
This for style
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: Constants.statusBarHeight,
paddingLeft: 10,
paddingRight: 10,
backgroundColor: '#ecf0f1',
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
color: '#34495e',
},
button: {
height: 50,
marginTop: 10,
backgroundColor: 'orange',
alignItems: 'center',
justifyContent: 'center'
},
text: {
fontSize: 24,
margin: 4,
padding: 10,
height: 100,
borderWidth: 0.5,
borderColor: '#0f0f0f',
},
headlineText: {
marginTop: 30,
marginBottom: 10,
color: "black"
}
}
);

Related

Avoid Keyboard on items on the Bottom - React Native

I have this React Native proyect (with Expo), and i have 2 buttons on the bottom of the container. When i open the keyboard to write something, those items come up with the keyboard. ¿How can i avoid that?
Here's the container code ->
render() {
return (
<View style={{flex: 1}}>
<Text style={styles.titulo}>Nueva Tarjeta</Text>
<View style={{ flex: 1}}>
<CreditCardInput
requiresName={true}
allowScroll={true}
labels={{
name: "Nombre del titular",
number: "Número de tarjeta",
expiry: "Venc",
cvc: "CVC",
}}
cardScale={1}
labelStyle={{ fontFamily: "OpenSansRegular" }}
placeholders={{
name: "Juan Perez",
number: "1234 1234 1234 1234",
expiry: "MM/AA",
cvc: "123",
}}
onChange={this._onChange}
/>
</View>
<View style={{ flexDirection: "row", alignSelf: "center",
justifyContent: 'flex-end', marginBottom: 10}}>
<TouchableOpacity style={styles.botonVolver}>
<Text style={styles.textoVolver}>Volver</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.botonAceptar}>
<Text style={styles.textoAceptar}>Aceptar</Text>
</TouchableOpacity>
</View>
</View>
);
}
Thanks in advance!
You could try using a KeyboardAvoidingView with its behavior prop set to 'height' on Android:
const screenHeight = Dimensions.get("window").height;
export default function App() {
return (
<KeyboardAvoidingView behavior="height" style={{ flex: 1 }}>
<View style={{ height: screenHeight - 75 }}>
<Text style={styles.titulo}>Nueva Tarjeta</Text>
<CreditCardInput
requiresName={true}
allowScroll={true}
labels={{
name: "Nombre del titular",
number: "Número de tarjeta",
expiry: "Venc",
cvc: "CVC",
}}
cardScale={1}
labelStyle={{ fontFamily: "OpenSansRegular" }}
placeholders={{
name: "Juan Perez",
number: "1234 1234 1234 1234",
expiry: "MM/AA",
cvc: "123",
}}
/>
</View>
<View
style={{
flexDirection: "row",
alignSelf: "center",
justifyContent: "flex-end",
marginBottom: 10,
}}
>
<TouchableOpacity style={styles.botonVolver}>
<Text style={styles.textoVolver}>Volver</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.botonAceptar}>
<Text style={styles.textoAceptar}>Aceptar</Text>
</TouchableOpacity>
</View>
</KeyboardAvoidingView>
);
}
I also changed the height of the View that contains the CreditCardInput to be based on the screen height using Dimensions, because the transition was glitchy when flex was set on that View.
Be sure to import Dimensions and KeyboardAvoidingView from 'react-native'.

Flexbox set height to 0

I'm currently working on a react native application and I have an issue with flex.
At each time I add flex: 1 in the style of a View or a Text element, its height equal 0, which cause the element to disapear.
This is what I'm trying to design:
(of course the colors are only here to help the comprehension)
My code:
return (
<View style={{paddingVertical: 7, display: 'flex', flexDirection: 'row', alignItems: "center", justifyContent: 'center', height: 50, borderWidth: 1}}>
<View style={{flex: 1}}>
<View style={{display: 'flex', flexDirection: 'column'}}>
<View style={{flex: 1}}>
<View style={{display: 'flex', flexDirection: 'row'}}> <View style={{flex: 1, backgroundColor: '#0f0'}}>
<Text>
First name Last name
</Text>
</View>
<View style={{flex: 1}}>
<Text style={{textAlign: 'center'}}>
27 km
</Text>
</View>
</View>
</View>
<View style={{flex: 1}}>
<Text style={{textAlign: 'center'}}>Booked ride</Text>
</View>
</View>
</View>
<View style={{flex: 0}}>
<TouchableOpacity onPress={this.onTake}>
<Text style={styles.btnBox}>
Take
</Text>
</TouchableOpacity>
</View>
</View>
)
This is what I currenlty have (with two elements repeated):
The first nested <View> is useless, remove it and adjust the next one like this:
return (
<View
style={{
paddingVertical: 7,
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
height: 50,
borderWidth: 1,
}}>
<View style={{ flex: 1, flexDirection: 'column' }}>
<View style={{ flex: 1 }}>
<View style={{ display: 'flex', flexDirection: 'row' }}>
<View style={{ flex: 1, backgroundColor: '#0f0' }}>
<Text>First name Last name</Text>
</View>
<View style={{ flex: 1 }}>
<Text style={{ textAlign: 'center' }}>27 km</Text>
</View>
</View>
</View>
<View style={{ flex: 1 }}>
<Text style={{ textAlign: 'center' }}>Booked ride</Text>
</View>
</View>
<View style={{ flex: 0 }}>
<TouchableOpacity onPress={this.onTake}>
<Text>Take</Text>
</TouchableOpacity>
</View>
</View>
I've also created a snack in case you want to take a look: example.
Now you can align your texts as you pref :)

How to have dynamic width for a group of views in react native?

I am learning React Native and in the process am trying to achieve a layout design which will have 8 views in a row whose width should be automatically adjusted based on the width of the device.
I am experimenting with some boilerplate code from https://facebook.github.io/react-native/docs/flexbox.html . With my changes the code looks like this:
export default class FlexDirectionBasics extends Component {
render() {
return (
// Try setting `flexDirection` to `column`.
<View>
<View style={{flex: 1, flexDirection: 'row', flexWrap:'wrap'}}>
<View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />
<View style={{width: 50, height: 50, backgroundColor: 'skyblue'}} />
<View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} />
<View style={{width: 50, height: 50, backgroundColor: 'red'}} />
<View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />
<View style={{width: 50, height: 50, backgroundColor: 'skyblue'}} />
<View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} />
<View style={{width: 50, height: 50, backgroundColor: 'red'}} />
</View>
</View>
);
}
};
And it looks like this:
I'd like to wrap the colour boxes around in two rows but I am not sure how to achieve that.
Thanking you in anticipation
Amby
Use flex: 0.25 for each small view/block:
<View style={{flexDirection: 'column', height: 120}}>
<View style={{flex: 1, flexDirection: 'row', height: 50, top: 100}}>
<View style={{flex: 0.25, height: 50, backgroundColor: 'powderblue'}} />
<View style={{flex: 0.25, height: 50, backgroundColor: 'skyblue'}} />
<View style={{flex: 0.25, height: 50, backgroundColor: 'steelblue'}} />
<View style={{flex: 0.25, height: 50, backgroundColor: 'red'}} />
</View>
<View style={{flex: 1, flexDirection: 'row', height: 50}}>
<View style={{flex: 0.25, height: 50, backgroundColor: 'red'}} />
<View style={{flex: 0.25, height: 50, backgroundColor: 'steelblue'}} />
<View style={{flex: 0.25, height: 50, backgroundColor: 'skyblue'}} />
<View style={{flex: 0.25, height: 50, backgroundColor: 'powderblue'}} />
</View>
</View>
Live Demo: https://snack.expo.io/SkFLS8-df
I have figured it out. Just needed to divide the width into 4 equal parts.
I am posting the change only:
import { AppRegistry, View, Dimensions } from 'react-native';
const {width} = Dimensions.get('window');
And each View's width should be replaced by :
width * 0.25
Thanks, guys. I need to get the programmer's (rubber) duck!

having some issue with flexbox layout in react-native

I am trying to make this highlighted line to stretch full width. I have tried many possible ways to come around it but could not get it as desired.
Here is my code from render() method.
return (
<ScrollView style={{backgroundColor: 'rgba(255,255,255,1)'}}>
<View style={styles.container}>
<Image style={styles.image} source={{uri: imageURI}} />
<View style={styles.content}>
<Text style={styles.title}>{news.title}</Text>
<View style={styles.itemIcons}>
<View style={styles.companyView}>
<Image
source={{uri: news.category[0].sourceImage}}
style={ styles.iconsImage} />
<Text style={styles.iconsText}>{news.category_name}</Text>
</View>
<Text style={styles.iconsTextRight}>{this.formatDate(news.pubDate)}</Text>
</View>
<Text style={styles.description}>{description}</Text>
</View>
</View>
</ScrollView>
);
And here are my styles for this.
container: {
flex: 1,
alignItems: 'center',
},
content: {
flex: 1,
flexDirection: 'column',
padding: 10
},
image: {
width: width,
height: custom_height,
// padding: 10
},
title: {
marginBottom: 15
},
itemIcons: {
flex: 1,
flexDirection: 'row',
alignSelf: 'stretch',
marginBottom: 15
},
iconsImage: {
height: 25,
width: 30,
marginRight: 10,
},
iconsText: {
fontSize: 12,
color: "#686666",
alignSelf: 'flex-end'
},
companyView: {flex:1,flexDirection:'row'},
dateView: {flex:1,alignSelf: 'flex-end'},
iconsTextRight: {
fontSize: 12,
color: "#686666",
alignSelf: 'flex-end'
},
description: {
padding: 2,
fontSize: 12,
color: '#323'
}
Change container style to
container: {
flex: 1,
}
Then the view will be aligned as per your requirement.
try this in itemIcons class
justify-content:'space-between'
I couldn't work with whole of your code, but as per the screenshot you posted, I was able to achieve this.
<View style={{flex:1}}>
<View style={{flexDirection:'row',paddingTop:100,justifyContent:'space-between',alignItems:'center'}}>
<View style={{flexDirection:'row',alignItems:'center'}}>
<Image source={require('./images/tiger.jpg')} style={{width:40,height:40}} />
<Text>Roche</Text>
</View>
<Text>25 Mar 2016, 18:16</Text>
</View>
</View>
Try to replicate this in your code and see if it works.

Place icon on same line as text

I'm trying to place my icon on the same line as 2 Replies.
Here's a screenshot of how it looks:
I'd like to have it to the right of the line, as the arrow shows.
Here's how my Component's render function looks:
render: function() {
return (
<TouchableHighlight onPress={this.props.onSelect}>
<View style={styles.container}>
<Image source={{uri: this.state.image}}
style={styles.image} />
<View style={styles.postDetailsContainer}>
<Text style={styles.postTitle}>
{this.state.name}
</Text>
<Text style={styles.postDetailsLine}>
{this.state.comment}
</Text>
<View>
<Text style={styles.postChildrenDetails}>
{this.props.comment.child_comments_count} Replies
</Text>
<Icon
name='fontawesome|comments-o'
size={25}
color='#D6573D'
style={styles.icon}
/>
</View>
<View style={styles.separator} />
</View>
</View>
</TouchableHighlight>
)
}
Here's how my StyleSheet looks:
container: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#FFFFFD',
},
image: {
height: 48,
width: 48,
borderRadius: 25,
marginTop: 10,
alignSelf: 'center',
marginRight: 15,
marginLeft: 15
},
postDetailsContainer:{
flex: 1,
},
postTitle: {
fontSize: 15,
textAlign: 'left',
marginTop: 10,
marginBottom: 4,
marginRight: 10,
color: '#D6573D'
},
postDetailsLine: {
fontSize: 12,
marginRight: 10,
color: 'gray',
},
postChildrenDetails: {
fontSize: 12,
marginTop: 5,
marginBottom: 10,
marginRight: 10,
color: 'gray',
textAlign: 'right',
flex: 1
},
separator: {
height: 0.5,
backgroundColor: '#CCCCCC',
},
icon: {
flex: 1,
textAlign: 'right',
width: 25,
height: 25
}
})
I had the same challenge and for anyone having the same issue, here is how I went about solving it.
Wrap the Icon and the Text in a View like #Colin Ramsey said above in the comments
It will look like this:
<View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
<Icon name="ios-chat" size={20} />
<Text>
2 replies
</Text>
</View>
Shalom!
TRY THIS
<View
style={{
backgroundColor: "#ffc8cc",
borderRadius: 30,
margin: 5,
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
paddingRight : 25
}}
>
<Text style={styles.title}>Leagues</Text>
<MaterialIcons name="sports-baseball" size={26} color="#cd000f" />
</View>
css for title :
title: {
fontSize: 18,
margin: 20,
color: "#cd000f",
},
Center image and text same line in props reactjs
<div style={{ alignItems: "center", display: "flex", justifyContent: "center" }} >
<img src={this.props.location.state.image} style={{ height: "100px", width: "100px", border: "4px solid white", verticalAlign: 'middle' }} />
<span style={{ fontSize: '40px', fontWeight: 'bolder', textAlign: 'center', verticalAlign: 'middle' }}>
{this.props.location.state.subject}</span>
</div>

Resources