I'm new to react native.
I need to arrange 8 image button in 4 rows and 2 columns.I tried with flexDirection, but it's not work(confuse).
Actually i need to arrange my 8 image like below image in react native.(I need only 4 -rows,2 columns)
Please help me..
my code
`
container:{
backgroundColor: '#ccffcc',
flex:1,
flexDirection: ('row'),
//paddingTop: 40,
//alignItems :'center',
padding:10
},
button1:{
flex:1,
// width : 100,
// height: 100,
flexDirection:'row',
backgroundColor : '#ff9933',
//alignSelf:'flex-start',
marginTop: 70,
margin: 20,
//justifyContent:'center'
},
button2:{
//flex:1,
//flexDirection:'column',
alignSelf:'center',
//height :50,
//backgroundColor : '#ff9933',
//alignSelf:'flex-start',
margin: 20,
marginTop:-30,
//justifyContent:'center'
},
button3:{
//flex:1,
flexDirection: 'column',
// width : 100,
// height: 100,
backgroundColor : '#ff9933',
alignSelf:'flex-start',
// marginTop:100,
margin: 20,
justifyContent:'center'
},
button4:{
//flex:1,
// flexDirection: 'row',
//height :50,
//backgroundColor : '#ff9933',
alignSelf:'flex-start',
margin: 20,
// marginTop:30,
justifyContent:'center'
},
button5:{
flex:1,
flexDirection:'column',
// width : 100,
// height: 100,
backgroundColor : '#ff9933',
alignSelf:'flex-end',
//marginTop:50,
margin: 20,
justifyContent:'center'
},
button6:{
flexDirection:'column',
flex:1,
//height :50,
//backgroundColor : '#ff9933',
alignSelf:'flex-end',
margin: 20,
// marginTop:30,
justifyContent:'center'
},
button7:{
flexDirection:'column',
flex:1,
// width : 100,
// height: 100,
backgroundColor : '#ff9933',
alignSelf:'flex-end',
// marginTop:100,
margin: 20,
justifyContent:'center'
},
button8:{
flexDirection:'column',
flex:1, //height :50,
//backgroundColor : '#ff9933',
alignSelf:'flex-end',
margin: 20,
// marginTop:30,
justifyContent:'center'
},`
You can do this by calculating the width of the container, then simply dividing the width by the number of items you need per row:
Check out this example I set up, also pasted the code below.
"use strict";
var React = require("react-native");
var { AppRegistry, StyleSheet, Text, View, Dimensions, ScrollView } = React;
var width = Dimensions.get("window").width - 20;
var images = [1, 2, 3, 4, 5, 6, 7, 8];
var SampleApp = React.createClass({
render: function() {
var icons = images.map((image, i) => {
return (
<View key={i} style={styles.listItemContainer}>
<View style={styles.listItem}>
<Text style={{ backgroundColor: "transparent" }}>
Image {image}
</Text>
</View>
</View>
);
});
return (
<ScrollView style={{ flex: 1, paddingLeft: 10, paddingRight: 10 }}>
<View style={styles.container}>
<View style={styles.list}>{icons}</View>
</View>
</ScrollView>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1
},
list: {
flexDirection: "row",
flexWrap: "wrap",
flex: 1,
marginTop: 50
},
listItemContainer: {
width: width / 4,
height: width / 4
},
listItem: {
backgroundColor: "#ededed",
borderRadius: 15,
margin: 10,
flex: 1
}
});
AppRegistry.registerComponent("SampleApp", () => SampleApp);
you can use react native grid view here :
https://github.com/lucholaf/react-native-grid-view
Related
I have a screen that is an HRV reading, the styles work well on all new devices, even on SE and Mini versions, my problem is with iPhone 8, the screen gets all messed up. this is the iPhone 13 screen and this is the iPhone 8 screen, I don't know how to change my code to identify it here is how my code is:
Obs. each index has a different style for the image.
const hrvScreensData = [
{
//0
imagePath: require('../../assets/images/hrvanimation1.png'),
styleText: {
fontSize: 28,
fontFamily:'inter-regular',
lineHeight:34,
padding: 20,
color: '#FFF',
textAlign: 'center',
},
title: 'Measure with back camera',
bkgColor: 'rgba(83,58,237,0.7)',
styleLarge: {height: screenWidth/2, width: screenWidth, flex: 1, resizeMode: 'contain'},
styleSmall: {height: 40, width: 40, resizeMode: 'contain'}
},
.
.
.
<View
style={{
marginTop: -150,
flexDirection: 'row',
justifyContent: 'flex-end',
paddingLeft: index == 0 ? 20 : 0,
backgroundColor: 'transparent',
}}
>
{
item.imagePath !== null ?
<Image
source={item.imagePath}
style={item.styleLarge}
/>
:
null
}
</View>
<View
style={{
alignItems: 'center',
justifyContent: 'flex-start',
width: screenWidth,
flex: 0.8,
}}>
<Text
style={item.styleText}
>
{item.title}
</Text>
</View>
any way I can make the app check which device or screen size is and change the style on the Index?
You can access the screen dimensions of the current device using Dimensions from react-native. Then conditionally set the styles of your component.
Here is an example.
import { Dimensions } from "react-native"
const height = Dimensions.get("screen").height
const width = Dimensions.get("screen").width
const SomeComponent = () => {
return (
<View
style={{
paddingLeft: width < 321 ? 10 : 20
}}
></View>
)
}
Edit 3: What worked for me
<View
style={{
minHeight: 200
}}
>
<Image
resizeMode="contain"
source={{ uri: props.item.thumbnailURL }}
style={{
flex: 1,
width: 350,
height: 200,
borderBottomLeftRadius: 30,
borderBottomRightRadius: 30,
borderTopLeftRadius: 30,
borderTopRightRadius: 30,
borderColor: "orange",
borderWidth: 1,
overflow: "hidden",
flexDirection: "row",
alignSelf: "center",
}}
/>
</View>
Edit 2: Added full code
Edit: I added
borderWidth: 3, borderColor: "orange"
and noticed that the parent of the image is rounding screenshot ... But why? I didn't want that.
I also tried with
borderRadius: Platform.OS === "ios" ? 30/2 : 30 but it didn't have any effect. What more can I do?
You must specify border radius value for all of the corners for ios
for example:
{
flex: 1,
width: 350,
height: 200,
borderRadius:30,
overflow: "hidden",
borderBottomLeftRadius: 30,
borderBottomRightRadius: 30,
borderTopLeftRadius: 30,
borderTopRightRadius: 30,
}
React Native alert defaults to showing up at the center of the screen. I've been trying to style it so that it shows up at the bottom of the screen - with no success. Does anyone know how I would go about this? I tried position: absolute and bottom: 0 but it still shows up in the center.
I have tried this out let me know if it helps also have designed according to the image.
Copy Paste the code and try.
import React, { useState } from 'react';
import {
StyleSheet,
View,
Text,
TextInput,
Pressable,
Modal,
Dimensions,
Button,
} from 'react-native';
const App = () => {
const [showWarning, SetshowWarning] = useState(false);
const onPressHandler = () => {
SetshowWarning(true);
}
return (
<View style={styles.body}>
<Modal
visible={showWarning}
transparent
onRequestClose={() =>
SetshowWarning(false)
}
animationType='slide'
hardwareAccelerated
>
<View style={styles.centered_view}>
<View style={styles.warning_modal}>
<View style={styles.warning_title}>
<Text style={{
color: '#000000',
fontSize: 25,
fontWeight:'700',
textAlign: 'center',
}}>Unsaved Changes</Text>
<Text style={styles.text}>Are you sure you want to continue?</Text>
</View>
<Pressable
onPress={() => SetshowWarning(false)}
style={styles.warning_button1}
android_ripple={{ color: '#fff' }}
>
<Text style={styles.textKE}>Keep Editing</Text>
</Pressable>
<Pressable
onPress={() => SetshowWarning(false)}
style={styles.warning_button}
android_ripple={{ color: '#fff' }}
>
<Text style={styles.textDC}>Discard Changes</Text>
</Pressable>
</View>
</View>
</Modal>
<Button onPress={() =>onPressHandler()} title="Text This"></Button>
{/* Add Here full code */}
</View >
);
};
const styles = StyleSheet.create({
body: {
flex: 1,
backgroundColor: '#ffffff',
alignItems: 'center',
},
centered_view: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#00000099',
},
warning_modal: {
width: Dimensions.get('window').width,
height: 200,
backgroundColor: '#eeff0f0',
borderRadius: 20,
top:"31%"
},
warning_title: {
height: 100,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#eff0f0',
borderTopRightRadius: 20,
borderTopLeftRadius: 20,
borderBottomWidth:1,
borderColor:'grey'
},
warning_body: {
height: 100,
justifyContent: 'center',
alignItems: 'center',
},
textKE: {
marginTop:20,
color: '#1485fe',
fontSize: 20,
margin: 5,
textAlign: 'center',
},
textDC: {
marginTop:20,
color: 'red',
fontSize: 20,
margin: 5,
textAlign: 'center',
},
warning_button1: {
height:70,
backgroundColor: '#efeff0',
borderBottomWidth:1,
borderColor:'grey'
},
warning_button: {
height:70,
backgroundColor: '#efeff0',
borderBottomLeftRadius: 20,
borderBottomRightRadius: 20,
}
});
export default App;
I am trying to get a hang of React Native, but for some reason, I can't get the button to centre horizontally in view container.
export default class LoginScreen extends Component {
static navigationOptions = {
header: null,
}
render() {
return (
<View style={styles.container}>
<MKTextField
tintColor={config.colors.accent}
floatingLabelEnabled={true}
autoFocus={true}
textInputStyle={{color: config.colors.primary, height: 32}}
placeholder="Login"
style={styles.loginInput}
onTextChange={(text) => this.setState({text})} />
<MKButton
style={styles.button}
shadowRadius={2}
shadowOffset={{width:0, height:2}}
shadowOpacity={.7}
shadowColor="black"
onPress={() => {
console.log('hi, raised button!');
}}>
<Text pointerEvents="none"
style={{color: 'white', fontWeight: 'bold', alignSelf:'center'}}>
Login
</Text>
</MKButton>
</View>
);
}
}
const styles = StyleSheet.create({
loginInput: {
width: '100%'
},
logo: {
height: '25%'
},
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff',
paddingLeft: 16,
paddingRight: 16
},
button: {
backgroundColor: config.colors.primary,
borderWidth: 0,
height: 40,
justifyContent: 'center',
alignItems: 'center',
}
});
And here's what it looks like:
Can't really tell whats going haywire here!
In your style, you need to change alignItems to alignSelf
button: {
backgroundColor: config.colors.primary,
borderWidth: 0,
height: 40,
justifyContent: 'center',
alignSelf: 'center',
}
I'm building an app that requires a Grid View. So far my grid works fine as you can see on the image below, the red squares are well positioned. Though, when I add an image in each box, it messes up the grid (second picture). Any ideas why ?
This is the screenshots (first image works fine with margins and is centered)
Second image has no more margins in the middle and on the right side
And this is the code that generates the view :
renderData = (data) => {
return (
<View style={styles.box}>
<Text style={styles.boxText}>{data.name}</Text>
// This is what I add to display the image
<Image
source={data.image}
style={styles.boxImage} />
</View>
);
}
renderPage() {
return (
<View style={styles.container}>
<TouchableHighlight style={styles.filterButton}>
<Text style={styles.filterButtonMessage}>
Occasions
</Text>
</TouchableHighlight>
<ListView contentContainerStyle={styles.list}
dataSource={this.state.data2}
renderRow={this.renderData} />
</View>
);
}
var styles = StyleSheet.create({
container: {
flex: 1,
},
list: {
flex: 1,
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'center'
},
box: {
width: 150,
backgroundColor: 'red',
height: 150,
alignItems: 'stretch',
margin: 3
},
boxImage: {
flex: 1
},
boxText: {
flex: 1,
fontWeight: '900',
fontSize: 15,
color: 'white',
position: 'absolute',
bottom: 5,
right: 5,
backgroundColor: 'rgba(0,0,0,0)'
}
});
have you tried...
boxImage: {
flex: 1,
width: 150,
height: 150,
},