React-native define component height with flexbox - css

I'm trying to define a list of elements with the same height, but the result is that the component takes all the available space.
This is the code:
return (
<View style={{flex: 1, flexDirection: 'row', justifyContent: "center"}} key={location.id}>
<Text style={{width: "80%", height: 20}}>{location.name}</Text>
<Button
style={{width: "20%", height: 20}}
title="x"
onPress={() => this.removeLocationFromDevice(location)}
/>
</View>
)
This is the result:
How can I make sure that the button and the text elements have just enough height to display the element and nothing more?

You can set the initial View to flex 1 and wrap the button + text in an other view
return (
<View style={{flex: 1}} key='key'>
<View style={{ flexDirection: 'row', justifyContent: "center"}}>
<Text style={{width: "80%", height: 20}}>text</Text>
<Button
style={{width: "20%", height: 20}}
title="x"
/>
</View>
</View>
);

Related

How to fix the footer on bottom of the page in react-native?

I want to fix the footer on the bottom of the page,
Thats why my whole container was in flex:1 so it takes all the space,
and in my footer, i added position:"absolute",marginBottom:0,, the footer affected so badly which i didnt expect. It goes to up like in the header of the page, then i deleted it,
How can i fix it on bottom of page?
Here is the related code.
<SafeAreaView style={{ flex: 1, backgroundColor:"#ffffff", }}>
{/* Footer */}
<View style={{ flexDirection:"row", alignItems:"center", justifyContent:"space-between",marginHorizontal:35,}}>
<View> // here there are some code
<View>
<Home height={30} width={22} fill={"#1E2439"} />
<Ratio height={30} width={22} fill={"#1E2439"} />
<Time height={30} width={22} fill={"#1E2439"} />
<User height={30} width={22} fill={"#1E2439"} />
</View>
</View>
</SafeAreaView>
Here how the page seen:
Position is from the css language.
{
position:"absolute",
bottom:0
}
here is my code for fixed footer
<View style={{flex:1}}>
<ScrollView style={{flex:1}}>
/// some Code
</ScrollView>
// here is bottom bar
<View style={{height: 60, justifyContent: 'center', borderTopLeftRadius: 20, borderTopRightRadius: 20, flexDirection: 'row', alignContent: 'center', alignItems: 'center'}}>
<View style={{flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center'}}>
//Here is you bottom icon or image
<View>
</View>
</View>
The position is from the CSS language.
{
position:"absolute",
bottom:0,
height: 60
}

How to align the user name at the center of the profile pic in the same row if the text is too long

I need to align the text at center of the profile pic in the same line. But it move below the profile pic.
How can I achieve this?
<TouchableOpacity >
<View style={{flexDirection:'row',marginLeft: "8%", marginBottom:"5%",marginRight:"15%",}}>
<FastImage source={user.profilePicture} style={{width: 45, height: 45, borderRadius: 22.5}}/>
<Text style = {{fontFamily: 'Heebo-Light', fontSize:12, marginLeft:"1.5%",textAlign:'center' }}> { user.firstName } </Text>
</View>
</TouchableOpacity>
I would propose to wrap your text in another View with justifyContent: 'center' and the same height as your userIcon. Additionally, I changed the textAlign to 'left'
Complete Code:
<TouchableOpacity >
<View style={{flexDirection:'row',marginLeft: "8%", marginBottom:"5%",marginRight:"15%",}}>
<Image source={{ uri: 'https://ra.ac.ae/wp-content/uploads/2017/02/user-icon-placeholder.png' }} style={{width: 45, height: 45, borderRadius: 22.5}}/>
// Adding this View
<View style={{justifyContent: 'center', height: 45}}>
// changed textAlign to left
<Text style = {{fontFamily: 'Heebo-Light', fontSize:12, marginLeft:"1.5%",textAlign:'left' }}> Usernamed asdfklshfgkjdhgkjdfhgjk dfhljkghdfjkghfdjkgfhfjk ghsdflkaslödsfsdfdsgdkasälöjda skldjsa </Text>
</View>
</View>
</TouchableOpacity>
Output:
Working Demo:
https://snack.expo.io/#tim1717/vengeful-sandwich
Updated Version
We add another View around the Image component and we are removing the height limitation, so that all of our text is visible and the container increases depending on the text content.
Code:
<TouchableOpacity >
<View style={{flexDirection:'row',marginLeft: "8%", marginBottom:"5%",marginRight:"15%",}}>
<View style={{justifyContent: 'center'}}>
<Image source={{ uri: 'https://ra.ac.ae/wp-content/uploads/2017/02/user-icon-placeholder.png' }} style={{width: 45, height: 45, borderRadius: 22.5}}/>
</View>
<View style={{justifyContent: 'center'}}>
<Text style = {{fontFamily: 'Heebo-Light', fontSize:12, marginLeft:"1.5%",textAlign:'left' }}> Usernamed asdfklshfgkjdhgkjdfhgjk dfhljkghdfjkghfdjkgfhfjk ghsdflkaslödsfsdfdsgdkasälöjda skldjsa dasfsdkhf,jdshfkjdshfkjhdsjkfh TEST ETST fdsfgmd,gndfjghjdfkhg dfkghkjdfhgjkdf gkjdfhgjdf gkjdfhg fdg </Text>
</View>
</View>
</TouchableOpacity>
Output:

react native flex question: how to stretch the wrapped item?

i want to achieve the following:
i am using a module called react-native-easy-grid
the whole thing is wrapped inside a Grid
with:
blue line being Col
red line being Row
orange line being View
here's my code so far:
<Grid>
<Col>
<Thumbnail circular source={selectedChildAvatar} />
</Col>
<Col size={4}>
<Row style={{ flexWrap: 'wrap', alignItems: 'center', justifyContent: 'space-evenly' }}>
{/* name, in review */}
<View>
{!_.isEmpty(selectedChild) && <Text>{selectedChild.firstName}</Text>}
<View
style={{
flexDirection: 'row',
alignItems: 'center'
}}>
<Text note>In review: {inReviewCount <= 0 && 0}</Text>
{inReviewCount > 0 && (
<View
style={{
backgroundColor: 'red',
paddingLeft: 4,
paddingRight: 4,
borderRadius: 50
}}>
<Text note>10</Text>
</View>
)}
</View>
</View>
{/* earnings, penalties */}
<View>
{/* earnings */}
<View
style={{
flexDirection: 'row',
alignItems: 'center'
}}>
<ProgressiveImage
resizeMode='cover'
imageStyle={styles.badge}
style={styles.badge}
source={images.icon.earnings}
/>
<Text style={[styles.digits, styles.earningDigits]}>12</Text>
</View>
{/* penalties */}
<View
style={{
flexDirection: 'row',
alignItems: 'center'
}}>
<ProgressiveImage
resizeMode='cover'
imageStyle={styles.badge}
style={styles.badge}
source={images.icon.penalties}
/>
<Text style={[styles.digits, styles.penaltyDigits]}>12</Text>
</View>
</View>
{/* review button */}
<View style={{ marginVertical: 10 }}>
<Button
small
iconRight
primary
block
disabled={inReviewCount === 0}>
<Text>Review</Text>
<Icon name='arrow-forward' />
</Button>
</View>
</Row>
</Col>
</Grid>
but this is what im getting:
what approach would it be to stretch the wrapped button to 100% of the width?
thank you!
use flex or set width to 100%
<View style={{ marginVertical: 10, flex: 1 }}>
<Button
small
iconRight
primary
block
disabled={inReviewCount === 0}>
<Text>Review</Text>
<Icon name='arrow-forward' />
</Button>
</View>

Align image vertically and horizontally using flexbox

Using React-Native I need to centre my Image and component LYDSceneQuestion:
const SplashScreen = () => {
return (
<View style={styles.container}>
<LYDSceneQuestion text="Welcome to" />
<Image source={theme.images.logo} style={styles.logo} />
<NextButton onPress={onNext} />
</View>
);
};
export default SplashScreen;
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center'
},
logo: {
flex: 1,
width: 300,
height: 100,
resizeMode: 'contain'
}
});
Currently my image aligns to the bottom, how I can I centre it vertically?
I will suggest a small technique using flex,give your parent view flex one and then arrange the components in separate child views inside it and give them equal flex just like i have done below.
<View style={styles.container,{flex: 1}}>
<View style={{flex: 2}}>
<LYDSceneQuestion text="Welcome to" />
</View>
<View style={{flex: 2}}>
<Image source={theme.images.logo} style={styles.logo}/>
</View>
<View style={{flex: 2}}>
<NextButton onPress={onNext} />
</View>
</View>
I think if you follow this you can centre it vertically.

React-Native Align Issue

I just want the Icon to be on the left and the text to be on center.
I have this structure:
<View style={styles.mainContainer}>
<Error screen={screen}/>
<View style={styles.headerContainer}>
{back &&
<Icon
raised
name='arrow-back'
onPress={ onBackPress }
containerStyle={styles.back}/>
}
<Text style={styles.header}>{ title }</Text>
</View>
<View style={styles.contentContainer}>
{ children }
</View>
</View>
Kinda like this:
-> MainContainer
--> HeaderContainer
----> Back button
----> Title
mainContainer: {
flex: 1,
flexDirection: 'column',
alignItems: 'stretch',
justifyContent: 'flex-start'
},
headerContainer: {
flex: 1,
padding: 10,
flexDirection: 'row',
// ommited for clarity
},
back: {
alignSelf: 'flex-start'
},
header: {
fontSize: 24,
alignSelf: 'center'
},
But it doesnt work as it produces:
I also tried adding in HeaderContainer:
justifyContent: 'space-between',
and a blank <View />, but the result is not good:
As you can see, it's not centered really.
Edit Produces:
It's not really centered, because we put an imaginary thrid element. Can we do it with only two elemets? One in start and other on center?
Why is it not posible?
Try the following:
<View style={{ flex: 1, flexDirection: 'row', alignItems: 'center' }}>
<View style={{ flex: 1 }}>
{leftContent}
</View>
<View style={{ flex: 1, alignItems: 'center' }}>
{centerContent}
</View>
<View style={{ flex: 1, alignItems: 'flex-end' }}>
{rightContent}
</View>
</View>
Don't forget to set textAlign: 'center' if center component is <Text> or textAlign: 'right' if the right component is Text.
Put your Icon and text in separate Views and asign a flex to it something like this
<View style={styles.mainContainer}>
<Error screen={screen}/>
<View style={styles.headerContainer}>
<View style={{flex:.3,alignSelf:'flex-start}}>
{back &&
<Icon
raised
name='arrow-back'
onPress={ onBackPress }
containerStyle={styles.back}/>
}
</View>
<View style={{flex:.7,alignSelf:'flex-end',alignItems:'center'}}>
<Text style={styles.header}>{ title }</Text>
</View>
</View>
<View style={styles.contentContainer}>
{ children }
</View>
</View>
I think it shoud fix the problem

Resources