Dynamic horizontal rule with centered text react native - react-native

I would like to have the grow horizontally not vertically. My current code is this
<View style={{ flexDirection: 'row', alignItems: 'center', backgroundColor: theme.backgroundColor }}>
<View style={{ flex: 1, height: 1, backgroundColor: 'white' }} />
<View>
<Text style={{ width: 50, textAlign: 'center', color: 'white' }}>Here is the problem</Text>
</View>
<View style={{ flex: 1, height: 1, backgroundColor: 'white' }} />
</View>
but this grows vertically any ideas.

You are limiting the width of the Text component, thus the text will not have enough space. Therefore, it will be breaking into several lines.
If we remove the fixed width, it will use the available vertical space.
<View style={{ flexDirection: 'row', alignItems: 'center', backgroundColor: theme.backgroundColor }}>
<View style={{ flex: 1, height: 1, backgroundColor: 'white' }} />
<View>
<Text style={{ textAlign: 'center', color: 'white' }}>Here is the problem</Text>
</View>
<View style={{ flex: 1, height: 1, backgroundColor: 'white' }} />
</View>
Before change
After change

Related

How to align flexwrap to the left?

The component that was left alone should be on the left. I tried to use align-items: 'flex-start', but it didn't work.
I'm trying to make a responsive layout where it fits several components in line, depending on the dimensions of the user's device.
<View style={{flex: 1}}>
<ScrollView showsHorizontalScrollIndicator={false}>
<View
style={{
justifyContent: 'space-evenly',
flex: 1,
flexDirection: 'row',
flexWrap: 'wrap',
}}>
<View
style={{
backgroundColor: 'red',
width: width / 2.5,
height: 200,
justifyContent: 'center',
alignItems: 'center',
}}>
<Image
style={{
width: 100,
height: 100,
}}
source={{
uri:
'https://images-na.ssl-images-amazon.com/images/I/51JZpJPClEL._AC_SL1000_.jpg',
}}
/>
<Text>Xiaomi Redmi note 8</Text>
</View>
</View>
</ScrollView>
</View>
Hello :) I would suggest doing the following:
calculate the left/right margin according to the container's & item's width
const marginHorizontal = (width / 2.5) / 6; // where 6 is 2 blocks in a row multiplying 3 spaces (left-right-center)
use the space-between & marginHorizontal applied to the container:
<View style={{flex: 1}}>
<ScrollView showsHorizontalScrollIndicator={false}>
<View
style={{
justifyContent: 'space-between',
flex: 1,
flexDirection: 'row',
flexWrap: 'wrap',
marginHorizontal,
}}>
<View
style={{
backgroundColor: 'red',
width: width / 2.5,
height: 200,
justifyContent: 'center',
alignItems: 'center',
}}>
<Image
style={{
width: 100,
height: 100,
}}
source={{
uri:
'https://images-na.ssl-images-amazon.com/images/I/51JZpJPClEL._AC_SL1000_.jpg',
}}
/>
<Text>Xiaomi Redmi note 8</Text>
</View>
</View>
</ScrollView>
</View>
let me know if it helped or not ;)

Rendering two images in one row react native

I have visited so many websites and read many articles but couldn't find any solution. I want a ui in which each row contains two images. I have done this by adding view tag in every new row but i want to do in in a single View tag. Kindly help
<View style={{ marginTop: 10 }}>
<View style={{ flexDirection: 'row', justifyContent: "space-between" }}>
<View style={{ flexDirection: 'column', alignItems: 'center', justifyContent: 'center', padding: 5, alignSelf: 'flex-start' }}>
<Image
resizeMode="stretch"
source={require('../assets/images/Home/zinger_ratha.jpg')}
style={{ width: width / 2.2, height: width / 2, resizeMode: 'stretch', }} />
<Text style={{ fontSize: 18 }}>Zinger Ratha</Text>
</View>
<View style={{ flexDirection: 'column', alignItems: 'center', justifyContent: 'center', padding: 5, alignSelf: 'flex-start' }}>
<Image
source={require('../assets/images/Home/arabian_delight.png')}
style={{ width: width / 2.2, height: width / 2, resizeMode: 'stretch' }} />
<Text style={{ fontSize: 18 }}>Arabian Delight</Text>
</View>
</View>
<View style={{ flexDirection: 'row', justifyContent: "space-between" }}>
<View style={{ flexDirection: 'column', alignItems: 'center', justifyContent: 'center', padding: 5, alignSelf: 'flex-start' }}>
<Image
resizeMode="stretch"
source={require('../assets/images/Home/mingle_bucket.jpg')}
style={{ width: width / 2.2, height: width / 2, resizeMode: 'stretch', }} />
<Text style={{ fontSize: 18 }}>Mingle Bucket</Text>
</View>
<View style={{ flexDirection: 'column', alignItems: 'center', justifyContent: 'center', padding: 5, alignSelf: 'flex-start' }}>
<Image
source={require('../assets/images/Home/xtreme_box.jpeg')}
style={{ width: width / 2.2, height: width / 2, resizeMode: 'stretch' }} />
<Text style={{ fontSize: 18 }}>Xtreme Box</Text>
</View>
</View>
</View>
I want like this but in single View tag
You can do the following:
import Dimensions from react-native,
The idea here is to give the View a width of whatever percentage of the phone screen size and then give the image a percentage of the View that would give you what you want.
For example, if I want to display three images in a row, I can give my View a width of 90% and flexWrap: wrap, and give each image inside it a width of 30% so it can only contain 3 images in a row.
You can do the following for your case.
import { Dimensions } from 'react-native';
const { width, height } = Dimensions.get('screen');
// inside your render or return
<View style={{ width: width * 0.9, display: "flex", flexDirection: "row", flexWrap: "wrap", alignContent: "center", alignItems: "center", justifyContent: "center" }}>
<Image source={require("img.png")}style={{ width: "45%", height: 200 }} />
<Image source={require("img.png")}style={{ width: "45%", height: 200 }} />
<Image source={require("img.png")}style={{ width: "45%", height: 200 }} />
<Image source={require("img.png")}style={{ width: "45%", height: 200 }} />
</View>
or if you need to put it in a flatlist you can do something like this:
numColumns={2} // you need to specify the number of columns here
<FlatList
style={styles.dashboard}
data={this.categories}
renderItem={this.renderItem}
keyExtractor={item => item.id}
numColumns={2}
/>

How to align a View to center without changing children's position?

<View style={{height: '100%', width: '100%'}}>
{OtherContent}
<ScrollView>
<View style={{flexDirection: 'row', flexWrap: 'wrap', padding: 20}}>
{children}
</View>
<ScrollView>
</View>
I want the View inside ScrollView to be in center of the screen without changing it's children position
Children:
<View style={Styles.docsContainer}>
{arr.map((document, index) => {
return (
<TouchableOpacity key={index} style={[Style.docStyle} onPress={null}>
<Icon name='file-text-o'/>
<Text>{document.name}</Text>
</TouchableOpacity>
);
})}
</View>
const Styles: {
docsContainer: {
flexDirection: 'row',
flexWrap: 'wrap',
padding: 20,
},
docStyle: {
alignItems: 'center',
padding: 20,
margin: 5,
marginBottom: 10,
borderRadius: 8,
width: 170
}
}
Indeed, we don't need parent View, we just need scrollView as flewGrow 1 which makes scrollView have full height and width as per device.
My approach:
<ScrollView contentContainerStyle={{ flexGrow: 1, justifyContent: 'center', alignItems: 'center' }}>
<View style={{
alignItems: 'center',
justifyContent: 'center',
flex: 1,
}}>
<Text>mahesh nandam</Text>
</View>
</ScrollView>
Try this.
The Viewis your ScrollView's immediate children. So you can style ScrollView using contentContainerStyle and align it in center like this.
<View style={{height: '100%', width: '100%'}}>
<ScrollView
contentContainerStyle={{
flexGrow: 1,
justifyContent: 'center',
alignItems: 'center',
}}
>
<View style={{flexDirection: 'row', flexWrap: 'wrap', padding: 20}}>
{children}
</View>
<ScrollView>
</View>
I guess it's what you're loooking for.
Center Horizontally
If you only want to center the View horizontally, you can set alignItems: 'center' to ScrollView's contentContainerStyle.
<View style={{ height: '100%', width: '100%' }}>
<Text>Other content</Text>
<ScrollView contentContainerStyle={{ alignItems: 'center' }}>
<View style={{ flexDirection: 'row', flexWrap: 'wrap', padding: 20, backgroundColor: 'red' }}>
<Text>children</Text>
</View>
</ScrollView>
</View>
The inner View is highlighted in red.
Center Horizontally and Vertically
In this case, you can set { flex: 1, justifyContent: 'center', alignItems: 'center' } for the same contentContainerStyle.
<View style={{ height: '100%', width: '100%' }}>
<Text>Other content</Text>
<ScrollView contentContainerStyle={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<View style={{ flexDirection: 'row', flexWrap: 'wrap', padding: 20, backgroundColor: 'red' }}>
<Text>children</Text>
</View>
</ScrollView>
</View>
If what you expect is different layout than in either of the screenshots, please share a simple sketch of the layout.

React Native - View won't take all available space with header and footer

I am trying to create a page with a header and a footer, and between them have a View taking all the remaining space, but the View only takes half of the available space, like in the picture below :
Screenshot of the problem
Here is the pertinent code :
<View style={{ flex: 1, flexDirection: "column" }}>
<View style={{ flex: 1, flexDirection: "column", height: 120 }}>
<View style={{ height: 70, borderWidth: 2 }}>
<Text> HEADER </Text>
</View>
<View style={{
flexDirection: "row",
justifyContent: "space-between",
height: 50,
alignItems: "center",
borderWidth: 2,
}}>
// Buttons and stuff ..
</View>
</View>
<View style={{ padding: 15, borderWidth: 2, flex: 1 }}>
<View style={{ flex: 1 }}><Text>Desired Content</Text></View>
</View>
<View style={{
height: 50,
backgroundColor: "lightgrey",
justifyContent: "center",
alignItems: "center",
flexDirection: "row",
}}>
// Here is my footer ..
</View>
</View>
The problem is that you set flex: 1 to the view wrapping your header.
Try simply removing it :
<View style={{flexDirection: 'column', height: 120}}>

React Native: How to align inline text?

I want to do this, where both of the texts are on 1 horizontal line and one of them is in the center and the other is on the right:
This is what I have, ignore the colors (it doesn't work at all):
styles:
rowLabelText: {
color: "#0B1219",
fontFamily: Fonts.Knockout31JuniorMiddleWeight,
fontSize: 16.0,
},
Markup:
<View style={{flexDirection: 'row', height: 30, flexWrap: 'wrap', backgroundColor: 'green'}}>
<View style={{ flex: 1, backgroundColor: 'red', justifyContent: 'center', alignSelf: 'center'}}>
<Text style={styles.rowLabelText}>
adjkfdasjkfaskj
</Text>
</View>
<View style={{ flex: 1, backgroundColor: 'blue', justifyContent: 'center', alignSelf: 'flex-end' }}>
<Text style={styles.rowLabelText}>
adjkfdasjkfaskj
</Text>
</View>
</View>
I am having trouble. Could someone assist me?
It looks like your problem is with with alignSelf you want to use alignItems.
This is how your code will look like.
Your View:
<View style={styles.textContainer}>
<View style={styles.leftContainer}>
<Text style={styles.rowLabelText}>
adjkfdasjkfaskj
</Text>
</View>
<View style={styles.rightContainer}>
<Text style={styles.rowLabelText}>
adjkfdasjkfaskj
</Text>
</View>
</View>
Your styles:
textContainer:{
flexDirection: 'row',
height: 30,
backgroundColor: 'green'
},
leftContainer:{
flex: 1,
justifyContent: 'center',
alignItems:'center',
backgroundColor: 'red',
},
rightContainer:{
flex: 1,
justifyContent: 'center',
alignItems: 'flex-end',
backgroundColor: 'blue',
},
rowLabelText: {
color: "#0B1219",
fontSize: 16.0,
},
One way to center text with respect to the entire width of the screen while also having some text to the right side of the centered text is to use absolute positioning on the right text.
By using absolute positioning on the right text, it will not affect the position of the centered text.
<View style={{flexDirection: 'row'}}>
<View style={{flex:1, alignItems: 'center', backgroundColor: 'red'}}>
<Text>50%</Text>
</View>
<View style={{position:'absolute', right: 20, backgroundColor: 'blue'}}>
<Text>+$0.80</Text>
</View>
</View>