How to align views with top and bottom and left and right? - react-native

I am trying to create ui designs, Here is my current design:
In this, i Have two views for of rectangle shape, here is my code :
<View style={{ flexDirection: 'column', alignContent: 'space-between' }}>
<View style={{ flexDirection: 'row' }}>
<View style={{ width: 100 * 2, height: 100, backgroundColor: 'red'}}/>
</View>
<View style={{ flexDirection: 'row', justifyContent: 'flex-end' }}>
<View style={{ width: 100 * 2, height: 100, backgroundColor: 'red'}}/>
</View>
</View>
The expected , output is the first rectangle should be on top left ( which is looking correct by below image ) and the second rectangle should be at bottom right. But its placing next to the first rectangle.
How to move, the rectangle to bottom right corner ? I tried all, like justify content , with flex end but nothing changing. Please help

<View style={{ flexDirection: 'column', justifyContent: 'space-between', flex: 1 }}>
<View style={{ flexDirection: 'row' }}>
<View style={{ width: 100 * 2, height: 100, backgroundColor: 'red'}}/>
</View>
<View style={{ flexDirection: 'row', justifyContent: 'flex-end' }}>
<View style={{ width: 100 * 2, height: 100, backgroundColor: 'red'}}/>
</View>
</View>
First you have to set flex: 1 so it can grow. And second step is to add justifyContent: 'space-between' instead of alignContent: 'space-between'

You need to add flex:1 property to the parents style.
Also the property is called justifyContent and not alignContent
Here is your code corrected
<View style={{ flex:1, flexDirection: 'column', justifyContent: 'space-between' }}>
<View style={{ flexDirection: 'row' }}>
<View style={{ width: 100 * 2, height: 100, backgroundColor: 'red'}}/>
</View>
<View style={{ flexDirection: 'row', justifyContent: 'flex-end' }}>
<View style={{ width: 100 * 2, height: 100, backgroundColor: 'red'}}/>
</View>
</View>
You can view an example here: https://snack.expo.io/#clemband/funny-cereal

Using Dimensions will do the trick for you.
Modify your code as below
import React, { Component } from 'react';
import { AppRegistry, View , Dimensions} from 'react-native';
const width = Dimensions.get('window').width
export default class FlexDirectionBasics extends Component {
render() {
return (
// Try setting `flexDirection` to `column`.
<View style={{ flex:1,flexDirection: 'column', alignContent: 'space-between' }}>
<View style={{ flex: 1,flexDirection: 'row' }}>
<View style={{ width: width/2, height: 100, backgroundColor: 'red'}}/>
</View>
<View style={{flexDirection: 'row' , justifyContent: 'flex-end'}}>
<View style={{ width: width/2 , height:100 ,backgroundColor: 'red'}}/>
</View>
</View>
);
}
};
// skip this line if using Create React Native App
AppRegistry.registerComponent('AwesomeProject', () => FlexDirectionBasics);
Output:

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

React Native: Vertically and Horizontally align image and text in same row

<View>
<Text>Hello</Text>
<Image source={require('./Vector.png')} />
</View>
I have the above View. I want to vertically and horizontally align image and text. Image should be on the right and text left in the same row. But I am not able to do align them correctly.
You can check here live dmeo here expo-snack:
Also the code is pretty ismple :
import * as React from 'react';
import { Text, View, StyleSheet,Image } from 'react-native';
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<Text>Hey</Text>
<Image source={{uri:'https://source.unsplash.com/random'}} style={{height:50,width:50}}/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'space-between',
backgroundColor: '#ecf0f1',
padding: 8,
flexDirection:'row',
alignItems:'center'
}
});
hope it helps. feel free for doubts , and if you want the text and image to be not so distant you can have justifyContent: 'center',
For horizontal:
flexDirection: 'row',
For vertical:
flexDirection: 'column',
Use flexDirection: 'row' to make it in the same row & use justifyContent: 'center', alignItems: 'center' to align it vertically & horizontally.
<View style={{ flex: 1, flexDirection: 'row', justifyContent: 'center', alignItems: 'center' }}>
<Text>Hello</Text>
<Image style={{ width: 50, height: 50 }} source={require('./Vector.png')} />
</View>
You can simply do it by using flexDirection
Example :
<View
style={{
flex: 1,
flexDirection: "row",
alignItems: "center",
justifyContent: "center",
}}
>
<Text>Hello</Text>
<Image
style={{ width: 30, height: 30, resizeMode: "contain" }}
source={require("./Vector.png")}
/>
</View>
If you want to make the image right corner on the screen and text on the left corner try this. It can be done using position
<View
style={{
flex: 1,
flexDirection: "row"
}}
>
<Text style={{ position: "absolute", right: 0 }}>Hello</Text>
<Image
style={{
width: 30,
height: 30,
resizeMode: "contain",
position: "absolute",
left: 0
}}
source={require("./Vector.png")}
/>
</View>

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.

How to vertically stretch two Views that are in a column formation - React Native Flex?

I have this view, where there are two Views in a column:
<View style={styles.container}>
<View style={styles.content}>
<View style={{backgroundColor: 'orange'}}>
<Text>Test</Text>
</View>
<View style={{backgroundColor: 'yellow'}}>
<Text>Test2</Text>
</View>
</View>
</View>
This is my Styles:
container: {
flexDirection: 'row',
backgroundColor: 'white',
justifyContent:'center',
alignItems:'center',
height: 80,
margin: 8,
},
content: {
flex:1,
alignItems: 'stretch',
flexDirection: 'column',
},
Here is a ScreenShot of what it looks with the above code.
How can I get the orange and yellow View to expand vertically equally, without manually defining the height for each of them?
The Text inside should be centered, but left-aligned.
Add flex: 1 to each cell to make them expand and add justifyContent: 'center' to make the text vertically centered. Like this:
<View style={styles.container}>
<View style={styles.content}>
<View style={{ backgroundColor: 'orange', flex: 1, justifyContent: 'center' }}>
<Text>Test</Text>
</View>
<View style={{ backgroundColor: 'yellow', flex: 1, justifyContent: 'center' }}>
<Text>Test2</Text>
</View>
</View>
</View>

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