Multi Column and Rows Flex Box issue in React Native - css

I'm having and Issue with Flex Box I'm trying to get this type of Grid View:
And I have tried a bunch of positions without any luck, the last one I tried is:
return (
<>
<View style={styles.container}>
<View style={styles.column}>
<View style={styles.row}>
<View style={[styles.box, { backgroundColor: 'yellow' }]} />
</View>
</View>
<View style={styles.column}>
<View style={[styles.box, { backgroundColor: 'green' }]} />
<View style={[styles.box, { backgroundColor: 'lightblue' }]} />
</View>
<View style={styles.space_between_columns} />
<View style={styles.column}>
<View style={[styles.box, { backgroundColor: 'grey' }]} />
<View style={[styles.box, { backgroundColor: 'purple' }]} />
</View>
</View>
</>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection:'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
row: {
flexDirection:'row',
justifyContent:'space-between',
alignItems:'center',
height:200,
width:50
},
column: {
flexDirection:'column',
justifyContent:'space-between',
alignItems:'center',
height:200,
width:50
},
space_between_columns:{
width:100
},
box: {
height:50,
width:50
}
});
But instead of giving me the Top Section where I want it, it puts it like this:
Any Ideas?
Kind Regards

Working App: Expo Snack
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
export default function App() {
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.text}>Header</Text>
</View>
<View style={{ padding: 10, flex: 1 }}>
<View style={styles.topSection}>
<Text style={styles.text}>Top Section</Text>
</View>
<View style={{ flexDirection: 'row', flex: 1 }}>
<View style={styles.leftColumn}>
<Text style={styles.text}>Left Column</Text>
</View>
<View style={styles.rightColumn}>
<Text style={styles.text}>Right Column</Text>
</View>
</View>
<View
style={{
height: 70,
flexDirection: 'row',
}}>
<View style={styles.subLeft}>
<Text style={styles.text}>Sub Left</Text>
</View>
<View style={styles.subRight}>
<Text style={styles.text}>Sub Right</Text>
</View>
</View>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'start',
backgroundColor: 'black',
},
header: {
height: 100,
backgroundColor: 'lightblue',
justifyContent: 'center',
},
topSection: {
height: 70,
backgroundColor: 'yellow',
justifyContent: 'center',
},
leftColumn: {
flex: 1,
backgroundColor: 'green',
justifyContent: 'center',
},
rightColumn: {
flex: 1,
backgroundColor: 'lightblue',
justifyContent: 'center',
},
subLeft: {
flex: 1,
backgroundColor: 'grey',
justifyContent: 'center',
},
subRight: {
flex: 1,
backgroundColor: 'purple',
justifyContent: 'center',
},
text: {
alignSelf: 'center',
color: 'black',
fontWeight: '800',
fontSize: 20,
},
});

Related

react native list view design

I have tried to design the screen in react native but its hard divide a screen column and row like ionic and angular
Output of that below code
<View style={styles.listItemContainer}>
<View style={{flex:1}}>
<View style={{flexDirection:"column"}}>
<Text>1003234</Text>
<Text>Info Test........</Text>
</View>
</View>
<View style={{flexDirection:"column"}}>
<Text style={{backgroundColor:"red",borderRadius:10,padding:2,
marginRight:10,marginTop:5,fontSize:12}}>Inprogress</Text>
</View>
</View>`
exactly what i need
You can use Flatlist
Use below code to apply styles to your List
<FlatList style={Styles.container}
data={this.props.listData}
extraData={this.state.selectedItem}
keyExtractor={(item, index) => item.id}
renderItem={({ item, index }) => (
this.renderRow(item, index)
)}
ListEmptyComponent={this.showEmptyListView()}
/>
Your row render can look like this.
renderRow = (item, index) => {
return (
<TouchableHighlight key={index} onPress={() => this.onPressAction(item)} >
<View style={Styles.listItemContainer}>
<View style={Styles.listDesign}>
<View style={Styles.imageLength}>
{
item.imageUrl == '' ?
<Image
source={require("./../../assets/images/default-display.png")}
style={{ resizeMode: "cover", width: '100%', height: 100 }} /> :
<Image
source={{ uri: item.thumbnailImageUrl }}
style={{ resizeMode: "cover", width: '100%', height: 100 }} />
}
</View>
<View style={[Styles.columnAlign, Styles.contentWidth]}>
<View style={[Styles.justifyEnd, Styles.imageContainer]}>
<View style={Styles.justifySpaceAround}>
<Image
source={require("./../../assets/images/anyImage.png")}
style={{ resizeMode: "cover", width: 25, height: 25 }} />
</View>
</View>
<View style={Styles.buttonContainer}>
<Text style={Styles.vehicleDesc}>{"test"}</Text>
</View>
<View style={Styles.buttonContainer}>
<Text style={Styles.text}>{item.mileage}{translate("Test 1")}</Text>
</View>
<View style={Styles.buttonContainer}>
<Text style={Styles.text}>{"Test2"}</Text>
</View>
<View style={Styles.buttonContainer}>
<Text style={Styles.watchListtext}>{"Test 3"}</Text>
</View>
</View>
</View>
</View>
</TouchableHighlight>
);
}
Your Style
listItemContainer: {
marginTop: 7,
// marginBottom: 7,
flexDirection: 'row',
alignItems: 'flex-start',
},
listDesign: {
backgroundColor: Colors.white,
flexDirection: 'row',
alignItems: 'center',
padding: 6,
width: '100%'
},
vehicleDesc: {
marginLeft: 12,
fontSize: 16,
color: '#AA2328',
fontWeight: 'bold'
},
text: {
marginLeft: 12,
fontSize: 14,
color: Colors.black,
},
watchListtext: {
marginLeft: 12,
fontSize: 14,
color: Colors.brandPrimary,
},
imageLength: {
width: '35%'
},
contentLength: {
width: '65%'
},
container: {
flex: 1,
backgroundColor: Colors.creamyWhite,
},
buttonContainer: {
flexDirection: 'row',
padding: 2
}, imageContainer: {
flexDirection: 'row',
},
justifySpaceAround:
{
justifyContent:'space-around'
}
justifyEnd:
{
justifyContent:'flex-end'
},
coulmnAlign:
{
flexDirection:'coumn'
}
You show empty data
showEmptyListView = () => {
return (
<View style={{ flex: 1, flexDirection: 'column', alignItems: 'center', justifyContent: 'center', padding: 10 }}>
<Text style={{ color: Colors.white }}>{"No Data to Display"}</Text>
</View>
)
}
You have to handle a lot in style.
You only need 3 common tag:
HorizontalContainer,
VerticalContainer,
ItemContainer,
export const VerticalContainer = styled.View({
flex: 1,
flexDirection: "column",
backgroundColor: (props) => props.backgroundColor || "#000"
});
export const HorizontalContainer = styled.View({
flex: 1,
flexDirection: "row",
backgroundColor: (props) => props.backgroundColor || "#000"
});
export const ItemContainer = styled.View({
flex: (props) => props.flex || 1
});
Inside handle as you like:
<HorizontalContainer backgroundColor="#fff">
<ItemContainer/>
<ItemContainer flex=2></ItemContainer>
<ItemContainer />
</HorizontalContainer >
Hope to help you!
I have done it with My Own code(FlatList view with design and search bar data from api) In that code i have removed my api call services you can try your own
Output Here(Check Before You Try)
import React, { Component } from 'react';
import { View, Text, StyleSheet,FlatList,ScrollView,ActivityIndicator} from 'react-native';
import { Picker } from 'react-native-picker-dropdown';
import Icon5 from 'react-native-vector-icons/FontAwesome5';
import { SearchBar, Avatar } from 'react-native-elements';
// create a component
class Home extends Component {
constructor(props){
super(props);
this.state={
data:[],
text: '',
language:'self',
ready: false,
searchText: "",
filteredData: [],
backupData:[]
}
this.onValueChange = this.handleValueChange.bind(this)
this.arrayholder=[];
}
search (searchText) {
this.setState({searchText: searchText});
console.log(searchText)
let text = searchText.toLowerCase();
let trucks = this.state.data;
// search by food truck name
let filteredName = trucks.filter((truck) => {
return truck.title.toLowerCase().match(text);
});
// if no match and text is empty
if( text == '') {
console.log('change state');
this.setState({
data: this.state.backupData
});
}
else if(!text ){
console.log("myname")
this.setState({data:[]})
}
// if no name matches to text output
else if(!Array.isArray(filteredName) && !filteredName.length) {
console.log("not name");
this.setState({
data: [],
});
}
// if name matches then display
else if(Array.isArray(filteredName)) {
console.log('Name');
this.setState({
data: filteredName,
});
}
};
showEmptyListView = () => {
return (
<View style={{ flex: 1, flexDirection: 'column', alignItems: 'center', justifyContent: 'center', padding: 10 }}>
<Text style={{ color: "black" }}>{"No Data to Display"}</Text>
</View>
)
}
handleValueChange(language) {
this.setState({ language })
}
renderRow = (item, index) => {
return (
<View style={styles.listItemContainer}>
<View style={{flex:1}}>
<View style={{flexDirection:"column",alignItems:"flex-start"}}>
<View style={{flexDirection:"row"}}>
<Text style={{marginLeft:12,marginTop:5,marginBottom:5,fontWeight:"bold",color:"black"}}>{item.number}</Text>
<Icon5 name="arrow-circle-up" size={15} style={{ marginTop:8,marginLeft:8, color: '#af2a5b', }}/>
<Text style={{marginLeft:5,marginTop:5,marginBottom:5,fontWeight:"bold",color:"#af2a5b"}}>{item.priority}</Text>
</View>
<Text style={{marginLeft:12,marginBottom:5,fontWeight:"bold",color:"black"}}>{item.title}</Text>
<View style={{flexDirection:"row"}}>
<Icon5 name="calendar-alt" size={15} style={{ marginLeft:12,marginBottom:5 }}/>
<Text style={{marginLeft:8,marginBottom:5}}>{item.assignedDate}</Text>
</View>
<View style={{flexDirection:"row"}}>
<Icon5 name="user-alt" size={15} style={{ marginLeft:12,marginBottom:5 }}/>
<Text style={{marginLeft:8,marginBottom:5}}>{item.createdBy}</Text>
</View>
</View>
</View>
<View style={{flexDirection:"column"}}>
<Text style={{color:"#f6b073",backgroundColor:"#fcd8b8",borderRadius: 10,padding:3,marginRight:10,marginTop:5,fontSize:12}}>{item.state}</Text>
</View>
</View>
);
}
renderHeader = () => {
return(
<View style={{ flex: 1, flexDirection: 'column' }}>
<View style={styles.listItemContainer}>
<View style={{flexDirection: 'row',backgroundColor: '#fff',color: '#fff',
width: '100%',height: 43,}}>
<View style={{ flex: 1, flexDirection: 'row' }}>
<View style={{ width: '45%', justifyContent: 'center', paddingTop: 7, paddingBottom: 7, color: '#fff', }} >
<Picker mode="dropdown" style={{width:150}}
selectedValue={this.state.language}
onValueChange={this.onValueChange} >
<Picker.Item label="My Task" value="self" />
<Picker.Item label="My Group" value="group" />
</Picker>
</View>
<View style={{ width: '55%', justifyContent: 'center', paddingTop: 7, paddingBottom: 7, color: '#fff', }} >
<SearchBar
inputStyle={{ backgroundColor: 'white', borderRadius: 55, borderWidth: 0, borderTopWidth: 0, borderBottomWidth: 0, height: 25 }}
containerStyle={{ backgroundColor: 'white', borderColor: '#g5g5g5', borderWidth: 0, borderRadius: 55, borderTopWidth: 0, borderBottomWidth: 0, width: '100%', marginLeft: 0, marginRight: 0, marginTop: 10, marginBottom: 10, height: 40 }}
placeholder="Search Here..."
inputContainerStyle={{ backgroundColor: 'white', borderWidth: 0, borderRadius: 55, height: 25 }}
onChangeText={searchText => this.search(searchText)}
value={this.state.searchText}
/>
</View>
</View>
</View>
</View>
</View>
)
}
renderFooter = () => {
return(
<View>
<Text>Footer</Text>
</View>
)
}
render() {
if (this.state.ready) {
return (
<FlatList style={{flex: 1,}}
data={this.state.filteredData && this.state.filteredData.length > 0 ? this.state.filteredData : this.state.data}
renderItem={({ item, index }) => (
this.renderRow(item, index)
)}
keyExtractor={(item, index) => index}
ListHeaderComponent={this.renderHeader}
ListEmptyComponent={this.showEmptyListView()}
enableEmptySections={true}
style={{flexWrap: 'wrap'}}
/>
);
}
else{
return (
<ActivityIndicator
animating={true}
style={{flex: 1,
alignItems: 'center',
justifyContent: 'center',
height: 80}}
color="#2F80ED"
size="large"
/>
);
}
}
}
const styles = StyleSheet.create({
listItemContainer: {
flex: 1,
marginBottom:10,
// paddingBottom: 15,
flexDirection: 'row',
backgroundColor: '#fff',
alignItems: 'flex-start',
},
})
export default Home;

How to get rounded image tag on top of two div in react native?

I want to create a profile page where rounded image will be on top of two div in 30:70 ratio where top div will be empty and bottom div will contain few items. I have tried few code snippets from web.
<View style={{ flex: 1, flexDirection: 'column', backgroundColor: 'green', justifyContent: 'flex-start', alignItems: 'flex-start' }}>
<View style={{ flexDirection: 'column', justifyContent: "flex-start", alignItems: "flex-start", alignSelf: "flex-start" }}>
{/* <View style={{ flex: 1, flexDirection: 'row', alignItems: 'flex-end', alignSelf: 'flex-start', margin: 1 }}> */}
<View style={{ backgroundColor: 'white', borderRadius: 10, flexDirection: 'column', height: "30%", width: "100%" }}></View>
{/* </View> */}
<View style={{ flex: 1, flexDirection: 'row', alignItems: 'flex-start', alignSelf: 'flex-start', margin: 1 }}>
<View style={{ backgroundColor: 'white', borderRadius: 10, flexDirection: 'column', height: "70%", width: "100%" }}></View>
</View>
</View>
<View style={{ justifyContent: 'center', alignItems: 'center', alignSelf: 'center', position: 'absolute' }}>
<View style={{
backgroundColor: 'blue',
borderRadius: 10, height: 100, width: 100, borderRadius: 100 / 2
}}></View>
</View>
</View>
reference image
Taking as reference the image you share with us... You can have two views and set the profile avatar style to:
{
position: 'absolute',
alignSelf: 'center',
etc..
}
Check a quick demo: https://snack.expo.io/#abranhe/avatar-example
App.js
import React, { Component } from 'react';
import { Text, View, StyleSheet, ImageBackground, Image } from 'react-native';
import { Ionicons, FontAwesome } from '#expo/vector-icons';
import Constants from 'expo-constants';
import { avatar, background } from './data';
export default class App extends Component {
renderRow({ action, icon, fontAwesome = false }) {
return (
<View style={styles.actionRow}>
{fontAwesome
? <FontAwesome name={icon} size={25} />
: <Ionicons name={icon} size={32} />}
<Text style={styles.text}>{action}</Text>
</View>
);
}
render() {
return (
<View style={styles.container}>
<ImageBackground source={background} style={styles.background}>
<Image source={avatar} style={styles.avatar} />
</ImageBackground>
<View style={styles.content}>
{this.renderRow({ action: 'Scanner', icon: 'ios-camera' })}
{this.renderRow({
action: 'Profile',
icon: 'user-circle-o',
fontAwesome: true,
})}
</View>
</View>
);
}
}
You styles:
const styles = StyleSheet.create({
container: {
flex: 1,
},
background: {
width: '100%',
height: 130,
},
content: {
flex: 1,
marginTop: 80,
marginHorizontal: 10,
},
avatar: {
width: 100,
height: 100,
borderRadius: '50%',
borderWidth: 0.7,
borderColor: 'black',
position: 'absolute',
alignSelf: 'center',
marginTop: 80,
},
actionRow: {
width: '100%',
height: '50',
flexDirection: 'row',
alignItems: 'center',
margin: 10,
},
text: {
marginLeft: 15,
fontWeight: '500',
},
});

Adding margins to the right edge of Flex-Start React-Native

import React, { Component } from 'react';
import { StyleSheet, Dimensions } from 'react-native';
import {
CheckBox, Container, Content, Text, View, Button,
} from 'native-base';
import Fonts from '../common/Fonts';
const checkAllMargin = Dimensions.get('window').height / 3.14;
const styles = StyleSheet.create({
wrapper: {
justifyContent: 'flex-end',
flexDirection: 'column',
flex: 1,
},
moveButtonContainer: {
flexDirection: 'row',
alignSelf: 'flex-end',
marginTop: checkAllMargin,
},
prevButton: {
justifyContent: 'center',
flex: 1,
alignSelf: 'flex-end',
alignItems: 'center',
backgroundColor: '#333333',
height: 55,
},
nextButton: {
justifyContent: 'center',
flex: 1,
alignSelf: 'flex-end',
alignItems: 'center',
backgroundColor: '#4FCBFF',
height: 55,
},
moveButtonText: {
textAlign: 'center',
fontFamily: Fonts.NANUMGOTHICBOLD,
fontSize: 18,
},
termsView: {
flexDirection: 'row',
justifyContent: 'flex-start',
marginTop: 21,
},
checkBox: {
marginLeft: 10,
},
termsText: {
alignSelf: 'center',
marginLeft: 17,
height: 16,
fontFamily: Fonts.NANUMGOTHIC,
fontSize: 11,
},
termsTextEnd: {
fontFamily: Fonts.NANUMGOTHIC,
fontSize: 11,
alignSelf: 'center',
},
requiredText: {
color: '#4FCBFF',
},
choiceText: {
color: '#999999',
},
checkAllView: {
flexDirection: 'row',
alignItems: 'flex-end',
justifyContent: 'flex-end',
marginTop: 35,
},
checkAllText: {
alignSelf: 'flex-end',
marginRight: 42,
},
checkAllBox: {
marginRight: 17,
},
openLinkButton: {
borderColor: '#CCCCCC',
borderRadius: 10,
height: 18,
width: 35,
alignSelf: 'flex-end',
borderWidth: 1,
},
openLinkText: {
fontFamily: Fonts.NANUMGOTHIC,
fontSize: 9,
paddingTop: 3,
paddingLeft: 5,
},
});
class TermsAgreeContainer extends Component {
constructor(props) {
super(props);
this.state = {
agreeTermsOfServices: false,
agreeTermsOfPrivacy: false,
agreeTermsOfCopyright: false,
agreeAll: false,
};
}
checkTermsOfServices = (isCheck) => {
this.setState({
agreeTermsOfServices: isCheck,
});
}
checkTermsOfPrivacy = (isCheck) => {
this.setState({
agreeTermsOfPrivacy: isCheck,
});
}
checkTermsOfCopyright = (isCheck) => {
this.setState({
agreeTermsOfCopyright: isCheck,
});
}
checkAll = (isCheck) => {
this.setState({
agreeTermsOfServices: isCheck,
agreeTermsOfPrivacy: isCheck,
agreeTermsOfCopyright: isCheck,
agreeAll: isCheck,
});
}
render() {
const {
agreeTermsOfServices, agreeTermsOfPrivacy, agreeTermsOfCopyright, agreeAll,
} = this.state;
const {
checkTermsOfServices, checkTermsOfPrivacy, checkTermsOfCopyright, checkAll,
} = this;
return (
<Container>
<Content scrollEnabled={false} contentContainerStyle={styles.wrapper}>
<View style={styles.termsView}>
<CheckBox
checked={agreeTermsOfServices}
onPress={() => checkTermsOfServices(!agreeTermsOfServices)}
style={styles.checkBox}
/>
<Text
style={styles.termsText}
onPress={() => checkTermsOfServices(!agreeTermsOfServices)}
suppressHighlighting
>
TermsOfServices
</Text>
<Text style={[styles.termsTextEnd, styles.requiredText]}> (required)</Text>
<Text style={[styles.openLinkButton, styles.openLinkText]}>Show</Text>
</View>
<View style={styles.termsView}>
<CheckBox
checked={agreeTermsOfPrivacy}
onPress={() => checkTermsOfPrivacy(!agreeTermsOfPrivacy)}
style={styles.checkBox}
/>
<Text
style={styles.termsText}
onPress={() => checkTermsOfPrivacy(!agreeTermsOfPrivacy)}
suppressHighlighting
>
TermsOfPrivacy
</Text>
<Text style={[styles.termsTextEnd, styles.requiredText]}> (required)</Text>
<Text style={[styles.openLinkButton, styles.openLinkText]}>Show</Text>
</View>
<View style={styles.termsView}>
<CheckBox
checked={agreeTermsOfCopyright}
onPress={() => checkTermsOfCopyright(!agreeTermsOfCopyright)}
style={styles.checkBox}
/>
<Text
style={styles.termsText}
onPress={() => checkTermsOfCopyright(!agreeTermsOfCopyright)}
suppressHighlighting
>
TermsOfCopyright
</Text>
<Text style={[styles.termsTextEnd, styles.choiceText]}> (choice)</Text>
<Text style={[styles.openLinkButton, styles.openLinkText]}>Show</Text>
</View>
<View style={styles.checkAllView}>
<CheckBox
checked={agreeAll}
onPress={() => checkAll(!agreeAll)}
style={styles.checkAllBox}
/>
<Text
style={styles.checkAllText}
onPress={() => checkAll(!agreeAll)}
suppressHighlighting
>
Check All
</Text>
</View>
<View style={styles.moveButtonContainer}>
<Button full style={styles.prevButton}>
<Text style={styles.moveButtonText}>back</Text>
</Button>
<Button full style={styles.nextButton}>
<Text style={styles.moveButtonText}>next</Text>
</Button>
</View>
</Content>
</Container>
);
}
}
export default TermsAgreeContainer;
Currently, the position of the Show button is determined by the Text length on the left.
I would like to move it to the right like the photo.
I want the Show button to be margin 30 based on the right.
However, I do not use margin-right if I use Flex-Start.
How can I place the show button on the right side?
One way could be to divide your current terms row in 2 views, assigning the first one a flex property and just a margin to the second one. Simply wrap the first elements in a <View>, like this:
<View style={styles.termsView}>
<View style={{ flex: 1, flexDirection: 'row' }}>
<CheckBox
checked={agreeTermsOfServices}
onPress={() => checkTermsOfServices(!agreeTermsOfServices)}
style={styles.checkBox}
/>
<Text
style={styles.termsText}
onPress={() => checkTermsOfServices(!agreeTermsOfServices)}
suppressHighlighting>
TermsOfServices
</Text>
<Text style={[styles.termsTextEnd, styles.requiredText]}>
(required)
</Text>
</View>
<Text
style={[
styles.openLinkButton,
styles.openLinkText,
{ marginRight: 10 },
]}>
Show
</Text>
</View>
And repeat this structure for each row.

How to align 2 react native elements, 1 being in the center and 1 on the beginning

Lets assume that we have those react native styles:
var styles = StyleSheet.create({
parentView:{
width: 400,
height:150
},
containerView:{
flex:1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
backgroundColor:'black'
},
child1:{
backgroundColor: 'red',
alignSelf: 'flex-start'
},
child2: {
backgroundColor: 'yellow'
}
}
and that we have this render function:
render: function(){
return(
<View style={styles.parentView}>
<View style={styles.containerView}>
<Text style={styles.child1}>Child1</Text>
<Text style={styles.child2}>Child2</Text>
</View>
</View>
);
}
This code produces this image:
but what I want to achieve is this image:
VERY VERY IMPORTANT: I want child2 to be TOTALLY centered within the containerView, and not a bit far to the right because of the space that child1 occupies.
How can I achieve that in react-native?*
p.s.: Keep in mind that this will run on many resolutions (many screens with different aspect ratios) thus, it cannot be set in an absolute way that would result on it only working on "some" screens but not on others.
not sure if this is the best way to do it, but it works for me
render() {
return(
<View style={styles.parentView}>
<View style={styles.containerView}>
<View style={styles.rowContainer}>
<View style={styles.rowItem}>
<Text style={styles.child1}>Child1</Text>
</View>
<View style={styles.rowItem}>
<Text style={styles.child2}>Child2</Text>
</View>
</View>
</View>
</View>
);
const styles = StyleSheet.create({
parentView:{
width: 400,
height:150
},
containerView:{
flex:1,
flexDirection: 'row',
alignItems: 'center',
backgroundColor:'black'
},
rowContainer: {
flex: 1,
flexDirection: 'column',
},
rowItem:{
flex: 1
},
child1:{
backgroundColor: 'red',
position: 'absolute',
},
child2: {
alignSelf: 'center',
backgroundColor: 'yellow'
}
});

Unable to align text to the right of the row

I'm looking to set the position of the date on the right of my row.
Here is the code:
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: 64,
},
row: {
flexDirection: 'row',
alignItems: 'center',
margin: 5,
backgroundColor: 'green',
},
horizontalAlign: {
flexDirection: 'row',
alignItems: 'center',
backgroundColor: 'red',
},
rowTitle: {
fontFamily: 'Helvetica',
fontSize: 17,
marginLeft: 10,
flex: 1,
textAlign:'center'
},
rowDate: {
textAlign: 'right',
},
rowText: {
fontFamily: 'Helvetica',
fontSize: 17,
marginLeft: 10,
},
rowImage: {
width: 35,
height: 35,
borderRadius: 17.5,
},
});
renderRow(rowData, sectionID, rowID, _highlightRow) {
return (
<TouchableHighlight onPress={ () => { this.rowPressed(rowData.recordID || null); }}>
<View style={styles.row}>
<Image source={ require('myImage.png') } style={styles.rowImage} />
<View>
<View style={styles.horizontalAlign}>
<Text style={styles.rowTitle}>{rowData.title}</Text>
<Text style={styles.rowDate}>{rowData.date}</Text>
</View>
<Text style={styles.rowText}>{rowData.text}</Text>
</View>
</View>
</TouchableHighlight>
);
}
render() {
return (
<View style={styles.container}>
<ListView
dataSource={this.state.dataSource}
renderRow={::this.renderRow}
/>
</View>
);
}
A screenshot:
Any suggestion?
You need to add a flex:1 property to the view containing the rowData.title and rowData.date. I've set up an example with your data here.
<View style={{flex:1}}>
<View style={styles.horizontalAlign}>
<Text style={styles.rowTitle}>{rowData.title}</Text>
<Text style={styles.rowDate}>{rowData.date}</Text>
</View>
<Text style={styles.rowText}>{rowData.text}</Text>
</View>
https://rnplay.org/apps/mWO0LQ
I'm not familiar with React, but in standard CSS, apply margin-left: auto to the date item.
You could also apply justify-content: space-between to the flex container which would align both flex items on opposite edges.
(These solutions assume that the line on which the flex items exist is free to extend the full width of the container.)

Resources