justifyContent: 'center' not working React Native - react-native

i want to center the image but it seems the justifyContent: 'center' property is not working as i want
I'm learning about flexbox in react-native
Also, I want to ask how can my code run on many different devices without changing the layout. I code on iphone 14 but when I switch to ipad or iphone se, the layout is changed and almost not as expected.
picture
my code
import React from 'react';
import {Dimensions, Image, Text, View} from 'react-native';
import {colors, sizes, spacing} from '../../constants/config';
import Icon from '../../utils/Icon';
const cardWidth = sizes.width / 3;
const cardHeight = 120;
const CartItem = ({list}) => {
return (
<View style={{flex: 1}}>
{list.map((item, index) => {
return (
<View style={{height: '20%'}} key={index}>
<View
style={{
marginLeft: spacing.l,
marginRight: spacing.l,
backgroundColor: 'white',
height: '90%',
borderRadius: sizes.radius,
}}>
<View style={{flexDirection: 'row'}}>
<View style={{justifyContent: 'center'}}>
<Image
style={{
width: cardWidth,
height: cardHeight,
borderRadius: 12,
marginLeft: spacing.m,
}}
resizeMode="stretch"
source={item.image}
/>
</View>
<View style={{marginTop: spacing.m, marginLeft: spacing.l}}>
<Text style={{marginBottom: 8}}>{item.title}</Text>
<Text>{item.price}</Text>
</View>
</View>
</View>
</View>
);
})}
</View>
);
};
export default CartItem;
this is my desired image

Use alignItems instead of justifyContent.
alignItems controls how children are aligned within the cross axis of their container, so in the case of a row within the vertical axis. justifyContent applies to the main axis, so the horizontal for a row.

you can give justifyContent to the upper view
<View style={{flexDirection: 'row', justifyContent:"center"}}>
<View style={{justifyContent: 'center'}}>
<Text style={{marginBottom: 8}}>Image</Text>
</View>
</View>

Related

react native text horizontal cutting, need full text in one line how to do that?

react-native version: "0.62.0", I tried this code
ui coming like this:
React Native
</View>
<View style={{flex:8}}>
<View style={{padding: 20, height: 200, backgroundColor: 'red'}}>
<Text style={fonts.bold}>View 1</Text>
</View>
<View style={{padding: 20, height: 200, backgroundColor: 'red'}}>
<Text style={fonts.bold}>View 1</Text>
</View>
<View style={{padding: 20, height: 200, backgroundColor: 'yellow'}}>
<Text>View 1</Text>
</View>
<View style={{padding: 20, height: 200, backgroundColor: 'green'}}>
<Text>View 1</Text>
</View>
</View>
</View>
How to do it?
give Text width greater than space occupied by characters with some padding
It looks like you did not provide the code for intended green view. The green view in your code is different. It is under yellow view.
Tip: For intended view try to remove height and width properties.
Created a sample according to your problem
import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
export default function App() {
return (
<View style={styles.container}>
<View style={styles.wrapperStyle}>
<View style={styles.leftStyle}>
<Text style={styles.textStyle}>Trending</Text>
</View>
<View style={styles.rightStyle}>
<Text>View 1</Text>
</View>
</View>
<View style={styles.wrapperStyle}>
<View style={[styles.leftStyle, { backgroundColor: 'green' }]}>
<Text style={styles.textStyle}>Top_games</Text>
</View>
<View style={[styles.rightStyle, { backgroundColor: 'yellow' }]}>
<Text>View 2</Text>
</View>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: 20,
},
wrapperStyle: {
height: 200,
flexDirection: 'row'
},
leftStyle: {
flex: 2,
backgroundColor: 'gray',
alignItems: 'center',
justifyContent: 'center'
},
rightStyle: {
flex: 8,
backgroundColor: 'red',
alignItems: 'center',
justifyContent: 'center'
},
textStyle: {
transform: [{ rotate: '-90deg' }]
}
});
Somtetimes you have to change flex value according to your requirements.
Hope this helps you. Feel free for doubts.

How to set text and an icon above the image with separate view in react-native

I need to show a text and icon above the image. But here the image view and text and loading icon need not to be linked. Both need to have a separate view.
I have set two views (one view for image and another one for and add the marginTop:-300 values in my view. It is a wrong behavior.
<View style={{ aspectRatio: 4 / 5, alignSelf: 'center', width: '100%', paddingLeft: '5%', paddingRight: '5%', opacity: 0.4,}}>
<FastImage resizeMode={FastImage.resizeMode.cover} style={{width: '100%',height: '100%',marginTop:18}} source={["imageUrl"]}/>
</View>
<View style={{flex:1, alignSelf: 'center', alignItems: 'center', justifyContent: 'center', marginTop:-50}}>
<ActivityIndicator size={"large"} color={"#ffffff"} style ={{marginTop:-700,}}/>
<Text style={{ fontSize: 15, fontFamily: "OpenSans-Semibold",marginTop:-300,color:'white'}}>Image captured</Text>
</View>
https://i.stack.imgur.com/MF5kM.png
You can use Position
Example This is an example of an image link I've created.
import React, { Component } from 'react';
import { View, Image,ActivityIndicator, Text } from 'react-native';
export default class App extends Component {
render() {
return (
<View style={{flex:1,justifyContent:"center",alignItems:"center"}}>
<View style={{opacity:0.5}}>
<Image
style={{width: 50, height: 50 }}
source={require('#expo/snack-static/react-native-logo.png')}
/>
<Image
style={{width: 50, height: 50, }}
source={{uri: 'https://facebook.github.io/react-native/img/tiny_logo.png'}}
/>
<Image
style={{width: 66, height: 58}}
source={{uri: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAAAzCAYAAAA6oTAqAAAAEXRFWHRTb2Z0d2FyZQBwbmdjcnVzaEB1SfMAAABQSURBVGje7dSxCQBACARB+2/ab8BEeQNhFi6WSYzYLYudDQYGBgYGBgYGBgYGBgYGBgZmcvDqYGBgmhivGQYGBgYGBgYGBgYGBgYGBgbmQw+P/eMrC5UTVAAAAABJRU5ErkJggg=='}}
/>
</View>
<ActivityIndicator size={"large"} color={"red"} style={{position:"absolute"}}/>
<Text style={{position:"absolute",color:"red",bottom:"45%"}} > Loading </Text>
</View>
);
}
}

Buttons not at the same place in Android and iOS

I have buttons that are not located at the same place in iOS and Android.
In Android they are at the middle bottom of the screen (it is what I expect) but in iOS they are at the top of the screen.
Do you know how I can put buttons in iOS at the same place as Android ?
import { Button } from 'react-native-elements';
...
<ImageBackground
key={`image${i}`}
source={{uri: image}}
style={{ width: deviceWidth, justifyContent:'center', alignItems: 'center', flex: 1 }}>
<View style={{flex: 1}}></View>
<Text style={{flex: 1}}> {textToDisplayEnglish[i]} </Text>
<Button
title={textA[i]}
style={{flex: 1}}
type={typeButton[i]}
onPress={() => this.triggerAction(i)}
/>
<View style={{flex: 1}}></View>
</ImageBackground>
you have too many flex:1. It'd be best to avoid using too many of them,unless you need to. try something like this:
import { Button } from 'react-native-elements';
...
<ImageBackground
key={`image${i}`}
source={{uri: image}}
style={{ width: deviceWidth, justifyContent:'center', alignItems: 'center', flex: 1 }}>
<Text> {textToDisplayEnglish[i]} </Text>
<Button
title={textA[i]}
type={typeButton[i]}
onPress={() => this.triggerAction(i)}
/>
</ImageBackground>
hope it helps!

ScrollView not showing everything and is cut off

I need this screen to show all the contents that is inside the scrollview, I've tried everything setting ScrollView's flexGrow to 1, parent view's flex to 100. The scrollview is scrolling down until iteration of Bark Central Dog Park & Cafe but it is cut off to half and can't scroll any further, also I noticed that the text HELP ME can not be found too. Thank you so much, as this problem has been bothering me and my progress in react native.
Please tell me what I should do and where I have gone wrong. Thank you again!
NotificationScreen.js
export default class NotificationScreen extends Component
{
static navigationOptions = ({ navigation }) => {
const { state } = navigation;
const {} = state;
return {
header: null,
};
}
render() {
return (
<View style={styles.parentContainer}>
<View style={{flexDirection: 'row', marginLeft: '5%', marginTop: '5%'}}>
<TouchableOpacity onPress={() => this.props.navigation.navigate('JobFeedScreen')}>
<Entypo name="chevron-thin-left" color={Colors.red} size={30} />
</TouchableOpacity>
<View style={{marginLeft: 80,}}>
<Text style={{ fontFamily: 'CoreSansD65Heavy',color: Colors.semiGray, fontSize: 25,}}> Ty, Next</Text>
</View>
<TouchableOpacity onPress={()=> this.props.navigation.navigate('LaunchScreenStack')} style={{marginLeft: 87,marginRight: '5%',}}>
<SimpleLineIcons name='bubbles' color={Colors.red} size={30} />
</TouchableOpacity>
</View>
<View style={{marginLeft: '5%'}}>
<Text style={styles.notificationHeader}> Notifications</Text>
</View>
<ScrollView horizontal={false} overScrollMode={'auto'}>
<TouchableOpacity style={styles.notifPlace}>
<View style={styles.notifTextPlace}>
</View>
<Text style ={styles.text}>Anteriore, Inc.</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.notifPlace}>
<View style={styles.notifTextPlace}>
</View>
<Text style ={styles.text}>The Palace Manila</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.notifPlace}>
<View style={styles.notifTextPlace}>
</View>
<Text style ={styles.text}>Boozy.ph</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.notifPlace}>
<View style={styles.notifTextPlace}>
</View>
<Text style ={styles.text}>Pet warehouse PH</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.notifPlace}>
<View style={styles.notifTextPlace}>
</View>
<Text style ={styles.text}>Bark Central Dog Park & Cafe</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.notifPlace}>
<View style={styles.notifTextPlace}>
</View>
<Text style ={styles.text}>Pet warehouse PH</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.notifPlace}>
<View style={styles.notifTextPlace}>
</View>
<Text style ={styles.text}>Bark Central Dog Park & Cafe</Text>
</TouchableOpacity>
<Text> PLEASE HELP ME </Text>
<Text> PLEASE HELP ME </Text>
<Text> PLEASE HELP ME </Text>
<Text> PLEASE HELP ME </Text>
</ScrollView>
</View>
);
}
NotificationScreenStyles.js
import { StyleSheet } from 'react-native'
import { Metrics, ApplicationStyles, Colors } from '../../Themes/'
export default StyleSheet.create({
...ApplicationStyles.screen,
container: {
paddingBottom: Metrics.baseMargin
},
logo: {
marginTop: Metrics.doubleSection,
height: Metrics.images.logo,
width: Metrics.images.logo,
resizeMode: 'contain'
},
centered: {
alignItems: 'center'
},
parentContainer: {
flex:1,
backgroundColor: Colors.white
},
notificationHeader: {
justifyContent: 'flex-start',
fontFamily: 'CoreSansD45Medium',
marginTop: 40,
marginBottom: 20,
fontSize: 20,
color: Colors.gray,
textAlign: 'left',
},
notifPlace: {
flexGrow: 0,
flexDirection: 'row',
backgroundColor: Colors.white,
width: '100%',
height: '12%',
borderWidth: 0.5,
borderColor: Colors.buttonGray,
},
notifTextPlace: {
width:60,
height:60,
borderRadius: 60/2,
backgroundColor:Colors.buttonGray ,
alignSelf: 'center',
justifyContent: 'flex-start',
marginLeft: '5%',
marginRight: 10,
marginBottom: 20,
marginTop: 20,
},
text: {
fontFamily: 'CoreSansD45Medium',
fontSize: 15,
alignSelf: 'center',
color: Colors.gray,
marginBottom: 10,
},
text2: {
alignSelf: 'center',
fontFamily: 'CoreSansD35Regular',
fontSize: 15,
marginBottom: 10,
},
})
Screenshot 1:
SC1
Screenshot 2: ( it only scrolls up to here )
SC2
This is a known issue in scroll view of react native, use a paddingBottom : 100 in the styles of the scroll view.
The 100 can be set to any value according the content you have
Please provide your StyleSheet and screenshot of what you are seeing.
Test to add
height = Dimensions.get('window').height
to your parentContainer
Also try paddingBottom to your scrollview StyleSheet
Just ran into this problem and what finally fixed it for me was making sure my scrollview wasn't the last component in my layout. So I added an invisible component (with a set height to offset the amount of scrollview content I couldn't see) right after my scrollview.
In my case, I have added a height to the wrapped component of the ScrollView.It worked for me
eg:-
<View style={{ height: hp(500) }}>
<ScrollView showsVerticalScrollIndicator={false}>
{filteredItems.map((item) => <Text style={styles.listItemText}>{item.title}</Text>)}
</ScrollView>
</View>
What fixed it for me was adding contentContainerStyle={{flexGrow: 1,paddingRight:100}} to my scrollview.
Hope it helps someone too!!
just add like this :
<ScrollView contentContainerStyle={{ width:'100%',height:'500%'}}>

React Native cropped border

I am very new to React Native and I am trying to create a view as shown in the below image.
Is it possible to create a semi circular cropped border view as highlighted in red in the attached image?
I wrote the example code, output not exactly same but it would be helpful
Output of the code :
import React from 'react';
import {View, Text} from 'react-native';
import {height, width} from 'react-native-dimension';
import moment from 'moment';
const Test5 = () => {
return (
<View style={{flex: 1, backgroundColor: 'white'}}>
<View
style={{
alignSelf: 'center',
height: height(80),
width: width(90),
marginTop: width(20),
}}>
<View
style={{
flex: 1,
backgroundColor: 'grey',
}}>
<View
style={{
alignSelf: 'center',
height: height(27),
width: width(80),
borderRadius: width(4),
marginTop: width(20),
backgroundColor: 'white',
}}>
<View
style={{justifyContent: 'space-between', flexDirection:
'row'}}>
<View>
<Text style={{fontWeight: 'bold', padding: 10}}>CODE200</Text>
</View>
<View
style={{
alignSelf: 'flex-end',
marginTop: height(7),
padding: 10,
}}>
<Text style={{textAlign: 'right'}}>Valid Till</Text>
<Text style={{textAlign: 'right'}}>
{moment().format('dddd, MMMM Do YYYY')}
</Text>
</View>
</View>
<View
style={{
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row',
}}>
<View
style={{
height: height(4),
width: width(8),
borderRadius: width(10),
backgroundColor: 'grey',
}}
/>
<Text style={{color: 'grey'}}>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - -
</Text>
<View
style={{
height: height(4),
width: width(8),
borderRadius: width(10),
backgroundColor: 'grey',
}}
/>
</View>
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
padding: 10,
}}>
<View>
<Text style={{textAlign: 'left'}}>APPLICABLE ON</Text>
<Text style={{textAlign: 'left'}}>Today</Text>
</View>
<View style={{alignSelf: 'flex-end'}}>
<Text style={{fontWeight: 'bold', textAlign: 'right'}}>Rs: 200/-</Text>
</View>
</View>
</View>
</View>
</View>
</View>
);
};
export default Test5;
I think this would require some css wizardery... Maybe you can can get the blank ticket as an asset and have it be the background Image for the view?
https://facebook.github.io/react-native/docs/images.html#background-image-via-nesting
<ImageBackground
imageStyle={{ resizeMode: 'stretch'}}
style={{flex: 1,flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
alignContent: 'center',
height: textEntryHeight, }}
source={
highlightColor === 'magenta' || this.state.keyboardActive ?
textEntryHighlighted : textEntryNonHighlighted}
>
{textEntryHeight > 55 ?
<TextInput style={{
marginLeft: textEntryHeight > 55 ? 48 : 23,
color: 'white',
fontSize: bodyFontSize,
alignSelf: 'center',
width: '100%',
}}
returnKeyType='done'
onSubmitEditing={Keyboard.dismiss}
ref='textInput'
autoFocus={this.props.openKeyboard}
value={body}
onChangeText={(text) => updateHomeTextInputContents(text)}
underlineColorAndroid={'transparent'}
selectionColor={'white'}
keyboardAppearance={'dark'}
onFocus={() => this.setState({keyboardActive: true})}
onBlur={() => this.setState({keyboardActive: false})}
/> :
<Text style={{marginLeft: textEntryHeight > 55 ? 48 : 23,
color: 'white',
fontSize: bodyFontSize,
alignSelf: 'center'}}>
{body}
</Text>
}
<TouchableOpacity style={{position: 'absolute', right: 0}}
onPress={iconOnPress}>
<Image style={{height: 70, width: 70, right:0}} source=
{icon}/>
</TouchableOpacity>
</ImageBackground>
There is no out-of-the-box solution in React Native for this, but I think the simplest way to go is to create a component which you use as separator, only for this part:
For this you only need a parent View with a grey background, 2 semi circles, and a View with a white background which wraps a View with a dashed border style
I think you want something like this.--------------
Used this component for dash ---'react-native-dash'
/**
* Sample React Native App
* https://github.com/facebook/react-native
* #flow
*/
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
TouchableOpacity,
Dimensions,
Image,
FlatList,
AsyncStorage,
TextInput,
ActivityIndicator,
ScrollView,
ImageBackground
} from 'react-native';
import { ListItem, Left, Body, Right, Title } from "native-base";
import Dash from 'react-native-dash';
const window = Dimensions.get('window');
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' +
'Cmd+D or shake for dev menu',
android: 'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
var localizedString;
type Props = {};
export default class App extends Component<Props> {
constructor(Props){
super(Props);
}
render() {
return (
<View style={{flex:1, alignItems:'center', justifyContent:'center', backgroundColor:'grey'}}>
<View style={{width:'80%', backgroundColor:'white', height:'100%', justifyContent:'space-between', flexDirection:'row', alignItems:'center', position:'absolute'}}>
</View>
<View style={{width:'95%', height:'100%', flexDirection:'row', alignItems:'center'}}>
<View style={{height:50, width:50, backgroundColor:'grey', borderRadius:150}}>
</View>
<Dash style={{width:'75%', height:5}}/>
<View style={{height:50, width:50, backgroundColor:'grey', borderRadius:150,}}>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
});