I imported some custom checkboxes but they're displaying vertically and I want them to be displayed horizontally in a row
I tried changing the flex-direction and some other things but none of that has changed anything.
Here is my code:
import { Pressable, StyleSheet, Text, View } from "react-native";
import React from "react";
import { MaterialCommunityIcons } from "#expo/vector-icons";
const CheckBox = (props) => {
const iconName = props.isChecked
? "checkbox-marked"
: "checkbox-blank-outline";
return (
<View style={styles.container}>
<Pressable onPress={props.onPress}>
<MaterialCommunityIcons name={iconName} size={40} color="#000" />
</Pressable>
<Text style={styles.title}>{props.title}</Text>
</View>
);
};
export default CheckBox;
const styles = StyleSheet.create({
container: {
justifyContent: "center",
alignItems: "center",
flexDirection: "column",
width: 150,
marginTop: 5,
marginHorizontal: 5,
flexWrap: "wrap",
},
title: {
fontSize: 20,
color: "#000a",
textAlign: "left",
top: 50,
marginTop: 40,
},
});
You need to change the flexDirection from column to row. Furthermore, you need to remove the topMargin from the title style for otherwise the checkbox and the text won't be aligned.
The adapted styles:
const styles = StyleSheet.create({
container: {
justifyContent: "center",
alignItems: "center",
flexDirection: "row",
width: 150,
marginTop: 5,
marginHorizontal: 5,
flexWrap: "wrap",
},
title: {
fontSize: 20,
color: "#000a",
},
});
The final product:
You can do it by just updating your CSS like this in your container stylesheet class :
{
flexDirection: "row",
Display:in-line,
float:left,
},
Related
I am trying to put the elements of a 2 since what is a data .map and I can not put a divider DIV with a flexDirection: "row", I need to make a wrap and they are accommodated but doing it is not working correctly.
<View
style={styles.container_cards}
>
{
data.map((elements, index) => {
return(
<CardTwoRows elements={elements}/>
)
})
}
</View>
const styles = StyleSheet.create({
container_cards: {
width: "100%",
height: "100%",
marginTop: verticalScale(14),
flexWrap: "wrap",
},
})
My CardTwoRows, it basically contains this
<View style={styles.container}>
.........
</View>
const styles = StyleSheet.create({
container: {
width: "49%",
height: verticalScale(220),
borderRadius: scale(6),
marginRight: "2%",
},
})
This is a sample of how it looks, I need to show 2 then a space between the lines and 2 cards again
You can use a flatlist like this.
A working demo as well as the code below
import * as React from 'react';
import { Text, View, StyleSheet, FlatList } from 'react-native';
import Constants from 'expo-constants';
const list = [0, 1, 2, 3, 4, 5, 6, 7];
export default function App() {
return (
<View style={styles.container}>
<FlatList
data={list}
numColumns={2}
keyExtractor={(item)=> item}
renderItem={({ item }) => (
<View style={styles.card}>
<Text style={styles.text}>{item}</Text>
</View>
)}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
card: {
height: 100,
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#212121',
margin: 5,
borderRadius: 10,
},
text: {
fontSize: 40,
fontWeight: '600',
color: 'white',
},
});
I'm having an issue where I'm creating a simple button that isn't visible (but it is there because I'm able to trigger the onPress method) within this view, but I am able to create other components within this View that appear. My code in App.js is below, any help would be greatly appreciated!
import React from 'react';
import {
View,
StyleSheet,
Text,
TouchableOpacity,
} from 'react-native';
export default function App() {
return (
<View style={{
flex: 1,
justifyContent: "center",
alignItems: "center",
}}>
<TouchableOpacity style={styles.button} onPress={() => console.log("Clicked")}>
<Text style={styles.text}>Login</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
button: {
color: "tomato",
borderRadius: 25,
justifyContent: "center",
alignItems: "center",
padding: 15,
height: 50,
width: "100%",
},
text: {
color: "#fff",
fontSize: 18,
fontWeight: "bold",
textTransform: "uppercase",
}
})
You have a white button with white text in front of a white background so it all blends in. Look at this expo snack and the code below to see why (https://snack.expo.dev/#heytony01/thankful-juice-box)
import React from 'react';
import {
View,
StyleSheet,
Text,
TouchableOpacity,
} from 'react-native';
export default function App() {
return (
<View style={{
flex: 1,
justifyContent: "center",
alignItems: "center",
}}>
<TouchableOpacity style={styles.button} onPress={() => console.log("Clicked")}>
<Text style={styles.text}>Login</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
button: {
borderColor:"red", // give border color
borderWidth:2, // give border width
color: "tomato",
borderRadius: 25,
justifyContent: "center",
alignItems: "center",
padding: 15,
height: 50,
width: "100%",
},
text: {
color: "black", // change font to black
fontSize: 18,
fontWeight: "bold",
textTransform: "uppercase",
}
})
I was setting the color of the button instead of the backgroundColor
I want to create a design which I have attached, there are two buttons, when I click on unlimited credit the colour of the button should become red and limited should be white and when I click on limited the limited button should be red and unlimited colour should be white as shown in the image and also when I click on button it should call functions, the function will have some design part and there will be two functions one for unlimited and other for limited based on the button click it should call that function, please let me know how can I execute this.
Final Output:
Full code:
const ButtonContainer = () => {
const [btnOne, setBtnOne] = useState(false);
const [btnTwo, setBtnTwo] = useState(false);
const clickStyle = { backgroundColor: '#F5D9D0', borderColor: '#F3805E' }
return (
<View style={styles.buttonContainer}>
<TouchableOpacity
onPress={() => {
setBtnOne(true);
setBtnTwo(false);
}}
style={[
styles.button,
btnOne ? clickStyle : null,
]}>
<Text>Unlimited Credit</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => {
setBtnOne(false);
setBtnTwo(true);
}}
style={[
styles.button,
btnTwo ? clickStyle : null,
]}>
<Text>Limited Credit</Text>
</TouchableOpacity>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
buttonContainer: {
flexDirection: 'row',
justifyContent: 'space-evenly',
},
button: {
borderColor: 'grey',
borderWidth: 2,
padding: 10,
borderRadius: 20,
},
});
Live working example: Expo Snack
here's a snack I created: https://snack.expo.io/#yoobit0616/two-buttons
import React, { useState } from 'react';
import { Text, View, StyleSheet } from 'react-native';
import { Button } from 'react-native-elements';
import Constants from 'expo-constants';
export default function App() {
const [disabledButton, setDisabledButton] = useState(true);
return (
<View style={styles.container}>
<View style={styles.buttonView}>
<Button
style={styles.button}
onPress={() => setDisabledButton(!disabledButton)}
buttonStyle={
disabledButton
? { backgroundColor: 'white' }
: { backgroundColor: 'red' }
}
titleStyle={{ color: 'black' }}
title="Unlimited Credit"></Button>
<Button
style={styles.button}
onPress={() => setDisabledButton(!disabledButton)}
buttonStyle={
!disabledButton
? { backgroundColor: 'white' }
: { backgroundColor: 'red' }
}
titleStyle={{ color: 'black' }}
title="Limited Credit"></Button>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
buttonView: {
flexDirection: 'row',
justifyContent: 'center',
},
button: {
height: 40,
width: 150,
marginLeft: 20,
borderRadius: 5,
borderWidth: 1,
},
});
Am trying to set my icon in left and text in center (like Image which I have give) in header by using flex but they both are going in center how to solve it.
import React, { Component } from 'react'
import {
AppRegistry,
StyleSheet,
Text,
View,
} from 'react-native'
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.cross}>X</Text>
<Text style={styles.headerText}>Invester Profile</Text>
</View>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
header: {
backgroundColor:'#212121',
height: 159.9,
flexDirection: 'row',
},
headerText: {
color: 'white',
marginTop: 20,
textAlign: 'center',
},
cross: {
color: 'white',
marginTop: 20,
}
})
Just add flex: 1 to headerText. Like this:
headerText: {
color: 'white',
marginTop: 20,
textAlign: 'center',
flex: 1
}
I am currently outlining an app which has a feed which will ultimately have a similar structure to Instagram's (cards containing an image with text underneath). I outlined a basic component and am now attempting to render it in a FlatList. The component should look like this:
However, when I render it in the FlatList it looks like this:
Additionally, the scroll view bounces back to the top when I attempt to scroll. I feel like this issue is resulting from the styling of my listItem but I can't figure out whats wrong/ how to fix it.
Here is the code I used to style the ListItem:
import EStyleSheet from 'react-native-extended-stylesheet';
export default EStyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f5fcff',
alignItems: 'center'
},
listing: {
backgroundColor: 'blue',
height: '60%',
width: '90%',
marginTop: 20,
marginBottom: 20,
flexDirection: 'column',
justifyContent: 'flex-start',
},
listingImage: {
backgroundColor: 'red',
// flexDirection: 'column',
// alignItems: 'flex-start',
flex: 0.8
},
listingInfo: {
backgroundColor: 'green',
flex: 0.2,
flexDirection: 'row',
justifyContent: 'flex-start',
},
hostImg: {
backgroundColor: '#0FFFA4',
flex: 0.3
},
listingText: {
backgroundColor: 'pink',
flex: 0.7,
flexDirection: 'column',
},
listingTitle: {
backgroundColor: '#0FD4FA',
flex: 1,
},
otherInfo: {
backgroundColor: 'orange',
flex: 1
}
});
To define the ListItem:
import PropTypes from 'prop-types';
import React from 'react';
import { View, Text } from 'react-native';
import styles from './styles';
const Listing = ({ children }) => {
return (
<View style={styles.listing}>
<View style={styles.listingImage}>
</View>
<View style={styles.listingInfo}>
<View style={styles.hostImg}>
</View>
<View style={styles.listingText}>
<View style={styles.listingTitle}>
</View>
<View style={styles.otherInfo}>
</View>
</View>
</View>
</View>
);
};
Listing.propTypes = {
children: PropTypes.any,
};
export default Listing;
And finally the code for the Feed view:
import React, { Component } from 'react';
import {StyleSheet, Text, View, FlatList} from 'react-native';
import { Listing } from '../../components/Cards';
import { FeedSeparator } from '../../components/Separators';
type Props = {};
export default class Feed extends Component<Props> {
render() {
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.titleStyle}> Feed </Text>
</View>
<View style={styles.listContainer}>
<FlatList
style = {{ flex: 1 }}
data={[
'a',
'b',
'c',
'd',
'e',
'f',
'g'
]}
renderItem={({ item }) => (
<Listing/>
)}
keyExtractor={item=>item}
ItemSeparatorComponent={FeedSeparator}
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f5fcff',
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
},
header: {
height: 80,
paddingTop: 30,
width: '100%',
backgroundColor: '#DDF1AD',
flexDirection: 'row',
justifyContent: 'center'
},
titleStyle: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
listContainer: {
flex: 1,
backgroundColor: 'blue',
marginTop: 14,
alignSelf: 'stretch'
}
});
Thank you for the help!
If you want to have dynamic height, you need to use Dimensions module of react-native. It will give you access to height and width of the device, where the application is being used.
Import:
import { Dimensions } from 'react-native'
Destructure:
const { height, width } = Dimensions.get('window')
Now you have height and width of a device that uses your application. You can set these values to state of your component, for example.