I am trying to build a flat list in react native but am having trouble keeping the specific list items from not touching. I tried to use the itemSeparatorComponent but the problem still persisted.
This is my FlatList component
<View style={{ position: "absolute", bottom: -35, marginLeft: 40 }}>
<FlatList
horizontal
data={this.state.hitRecords}
renderItem={({ item }) => (
<ArticleBox
title={item.title}
year={item.year}
time={item.time}
author={item.author}
/>
)}
itemSeparatorComponent = {()=> </View>}
/>
</View>
This is my ArticleBox component
const styles = StyleSheet.create({
box: {
backgroundColor: "rgba(0.0,0.0,0.0,0.9)",
height: 150,
width: 300,
alignSelf: "center",
flex: 2,
flexDirection: "column",
borderRadius: 25,
elevation: 2
},
layout: {
textAlign: "center",
color: "#FFF"
},
input: {
color: "#ff8c00"
}
});
class ArticleBox extends Component<Props> {
render() {
return (
<View style={styles.box}>
<Text style={styles.layout}>
Time: <Text styles={styles.input}>{this.props.time}</Text>
</Text>
<Text />
<Text style={styles.layout}>Publication Year: {this.props.year}</Text>
<Text />
<Text style={styles.layout} numberOfLines = {3} >Publication Title: {this.props.title}</Text>
<Text />
<Text style={styles.layout}>Author: {this.props.author}</Text>
</View>
);
}
}
I am trying to get the list to behave like this
item item item item
but the list behaves like this
itemitemitemitem
Add marginLeft: 12 in your 'ArticleBox' Component. like this.
box: {
backgroundColor: "rgba(0.0,0.0,0.0,0.9)",
height: 150,
width: 300,
alignSelf: "center",
flex: 2,
flexDirection: "column",
borderRadius: 25,
elevation: 2,
marginLeft: 12
}
Related
Hi I'm trying to build a chat item component but I'm having problems with the style I can't get it to work the way I want I would like the profile picture to be on the left but the text in the middle and the username on top where the time is on the far right what am I doing wrong?
const ChatItem = () => {
return (
<View>
<View style={styles.card}>
<Image
style={styles.profileImage}
source={{
uri: 'https://reactnative.dev/img/logo-og.png',
}}
/>
<Text style={styles.name}>Username</Text>
<Text style={styles.time}>01:00 AM</Text>
</View>
<Text>Hello World</Text>
</View>
);
};
const styles = StyleSheet.create({
imageWrapper: {},
card: {
flexDirection: 'row',
},
name: {
fontWeight: 'bold',
color: '#000',
},
time: {},
profileImage: {
width: 50,
height: 50,
borderRadius: 90,
},
});
🟢 Problem solved!
You need better understanding about react native styling, you can achive what you want with the following code fragment.
const ChatItem = () => {
return (<View style={{flexDirection: "row", width: '100%', backgroundColor: "lightgrey", padding: 8}}>
<View style={{justifyContent:"center"}}>
<Image style={{ width: 50, height: 50, borderRadius: 25 }} source={{uri:'https://reactnative.dev/img/logo-og.png'}} />
</View>
<View style={{flexDirection: "column", marginLeft: 8, flex: 1}}>
<Text style={{ fontWeight: 700 }}>Username</Text>
<Text style={{ fontWeight: 300 }}>Hello, Good Morning! This is the message sent by the user </Text>
<Text style={{textAlign:"right", color: "grey", fontSize: 12}}>10:20 PM, Today</Text>
</View>
</View>);
};
This will look like,
I am developing iOS apps in React Native Expo.
To display the image icon and placeholder in the Input column, I set it to Image icon and TextInput in the View.
When using KeyboardAvoidingView, the keyboard scrolls to the bottom of TextInput as shown in the attached image (1), but does anyone know if it is possible to scroll to the bottom of the description of the input field as shown in image (2)?
Image(1)
Image(2)
The article is based on the following.
https://reactnative.dev/docs/keyboardavoidingview
Parent function code.
<KeyboardAvoidingView behavior="padding" style={styles.containerStyle}>
<SafeAreaView style={styles.containerStyle}>
<ScrollView style={styles.containerStyle}>
<View style={styles.headContainerStyle}></View>
<View style={styles.mainContainerStyle}></View>
<View style={styles.headMessageContainerStyle}>
<Text style={styles.headMessageTextStyle}>Sign Up</Text>
</View>
{/* Email */}
<MailForm
inputAccessoryViewID={inputAccessoryViewID}
isCorrectMail={isCorrectMail}
setIsCorrectMail={setIsCorrectMail}
/>
{/* Password */}
<PasswordForm
inputAccessoryViewID={inputAccessoryViewID}
isCorrectPassewordSymbol={isCorrectPassewordSymbol}
setIsCorrectPassewordSymbol={setIsCorrectPassewordSymbol}
isCorrectPassewordStringCount={isCorrectPassewordStringCount}
setIsCorrectPassewordStringCount={setIsCorrectPassewordStringCount}
/>
{/* UserId */}
<UserIdForm
inputAccessoryViewID={inputAccessoryViewID}
isCorrectUserIdSymbol={isCorrectUserIdSymbol}
setIsCorrectUserIdSymbol={setIsCorrectUserIdSymbol}
isCorrectUserIdStringCount={isCorrectUserIdStringCount}
setIsCorrectUserIdStringCount={setIsCorrectUserIdStringCount}
isAvailableUserId={isAvailableUserId}
setIsAvailableUserId={setIsAvailableUserId}
/>
{/* Screen Bottom */}
<View style={styles.bottomStyle}>
{isCorrectMail && isCorrectPassewordSymbol && isCorrectPassewordStringCount && isCorrectUserIdSymbol && isCorrectUserIdStringCount && isAvailableUserId ?
(
<TouchableOpacity
style={styles.buttonContainerStyle}>
<Text style={styles.buttonTextStyle}>Sign Up</Text>
</TouchableOpacity>
) : (
<TouchableOpacity
style={[styles.buttonContainerStyle, styles.buttonContainerInvalidStyle]}
onPress={() => navigation.navigate('SignUp')}>
<Text style={styles.buttonTextStyle}>Sign Up</Text>
</TouchableOpacity>
)}
<View style={styles.toLoginStyle}>
<Text style={styles.toLoginTextStyle}>Do you have an account?</Text>
<Text style={[styles.toLoginTextStyle, styles.toLoginTextLinkStyle]}>Login here</Text>
</View>
</View>
</ScrollView>
</SafeAreaView>
</KeyboardAvoidingView>
The code for the <UserId/> component, which we want to support scrolling for this time.
<View>
<View style={styles.searchBoxStyle}>
<View style={styles.searchWrapperStyle}>
<Pressable style={styles.searchContainerStyle} onPress={() => textInputUserId.focus()}>
<Text style={styles.searchTitleStyle}>UserId</Text>
{/* <KeyboardAvoidingView behavior="padding"> */}
<View style={defaultUserIdBorderColor ? isCorrectUserIdSymbol && isCorrectUserIdStringCount ? styles.searchViewStyle : [styles.searchViewStyle, styles.inputIncorrectBorderColorStyle]: styles.searchViewStyle}>
<Image source={require("../../../assets/profile.png")} style={styles.searchIconStyle}/>
{/* <KeyboardAvoidingView behavior="padding"> */}
<TextInput
onChangeText={onChangeUserIdText}
style={styles.searchContentStyle}
value={userIdText}
placeholder="test1234"
inputAccessoryViewID={inputAccessoryViewID}
ref={(input) => textInputUserId = input}
autoCapitalize="none"
maxLength={100}
textContentType="username"
onFocus={() => {
…
}}
onEndEditing={() => {
…
}}
/>
</View>
</Pressable>
</View>
</View>
{/* UserId Description */}
{displayUserIdDescription ? !isCorrectUserIdSymbol || !isCorrectUserIdStringCount || !isAvailableUserId ? (
<View style={styles.descriptionBoxStyle}>
<View style={styles.descriptionWrapperStyle}>
<View style={styles.descriptionContainerStyle}>
{!defaultDisplayUserIcons ? isCorrectUserIdSymbol ? <Image source={require("../../../assets/correct.png")} style={styles.descriptionIconStyle}/>: <Image source={require("../../../assets/incorrect.png")} style={styles.descriptionIconStyle}/>: null}
<Text style={styles.descriptionTextStyle}>Half-width alphanumeric characters only.</Text>
</View>
<View style={styles.descriptionContainerStyle}>
{!defaultDisplayUserIcons ? isCorrectUserIdStringCount ? <Image source={require("../../../assets/correct.png")} style={styles.descriptionIconStyle}/>: <Image source={require("../../../assets/incorrect.png")} style={styles.descriptionIconStyle}/>: null}
<Text style={styles.descriptionTextStyle} >More than 4 words and less than 100 words.</Text>
</View>
<View style={styles.descriptionContainerStyle}>
{!defaultDisplayUserIcons ? isAvailableUserId ? <Image source={require("../../../assets/correct.png")} style={styles.descriptionIconStyle}/>: <Image source={require("../../../assets/incorrect.png")} style={styles.descriptionIconStyle}/>: null}
<Text style={styles.descriptionTextStyle} >Available.</Text>
</View>
</View>
</View>
) : null: null}
</View>
Style sheet code.
import { StyleSheet } from 'react-native';
export const styles = StyleSheet.create({
containerStyle: {
flex: 1,
backgroundColor: "#1B1C56",
},
headContainerStyle: {
width: "100%",
height: "10%",
height: 40,
backgroundColor: "#1B1C56",
},
headMessageContainerStyle: {
backgroundColor: "#feffff",
alignItems: 'center',
},
headMessageTextStyle: {
fontSize: 50,
fontFamily: "AlfaSlabOne_400Regular",
color: "#1B1C56",
marginBottom: 32,
},
mainContainerStyle: {
width: "100%",
height: "15%",
backgroundColor: "#feffff",
borderTopLeftRadius: 50,
alignItems: 'center',
},
searchBoxStyle: {
flex: 1,
backgroundColor: "#feffff",
},
searchWrapperStyle: {
flex: 1,
alignItems: "center",
paddingBottom: 10,
},
searchContainerStyle: {
},
searchTitleStyle: {
fontFamily: "ABeeZee_400Regular_Italic",
color: "#262626",
marginBottom: 5,
},
searchIconStyle: {
width: 24,
height: 24,
marginRight: 10,
marginLeft: 10,
},
searchViewStyle: {
flexDirection: 'row',
alignItems: 'center',
backgroundColor: '#F6F7FB',
borderWidth: 0.5,
height: 60,
borderRadius: 5,
width: 300,
borderColor: "#F6F7FB",
},
searchContentStyle: {
flex: 1
},
inputIncorrectBorderColorStyle:{
borderWidth: 2,
borderColor: "#ED195E",
},
completeBoxStyle: {
width: 60,
alignItems: "center",
padding: 10,
},
completeTextStyle: {
fontSize: 18,
fontWeight: "bold",
color: "hsl(210, 100%, 60%)"
},
passwordIconStyle: {
marginRight: 10
},
descriptionBoxStyle:{
display: "flex",
alignItems: "center",
backgroundColor: "#feffff",
paddingBottom: 10,
},
descriptionWrapperStyle: {
},
descriptionContainerStyle: {
flexDirection: "row",
width: 300,
},
descriptionTextStyle: {
color: "#262626",
fontSize: 12,
overflow: "visible"
},
descriptionIconStyle:{
marginRight: 10,
width: 12,
height: 12,
},
bottomStyle: {
display: "flex",
alignItems: "center",
height: "100%",
backgroundColor: "#feffff",
},
buttonContainerStyle: {
alignItems: "center",
justifyContent: "center",
backgroundColor: "#1B1C56",
width: 300,
height: 60,
borderRadius: 10,
fontSize: 18,
},
buttonContainerInvalidStyle:{
backgroundColor: "#C5C5C7",
},
buttonTextStyle: {
color: "#feffff",
fontFamily: "ABeeZee_400Regular_Italic",
},
toLoginStyle: {
marginTop: 10,
height: "5%",
flexDirection: "row"
},
toLoginTextStyle: {
fontFamily: "ABeeZee_400Regular_Italic",
},
toLoginTextLinkStyle: {
color: "#ED195E",
marginLeft: 10,
},
});
Swapped the positions of KeyboardAvoidingView, SafeAreaView and ScrollView. I have tried to set the keyboardVerticalOffset and the keyboardVerticalOffset, but each had its own problems, and the current code was the closest to the ideal.
I am trying to avoid the keyboard over the form, according to React Native documentation, I should be using KeyboardAwareScrollView component. the instructions are fairly simple, but I cant make it work. Am I missing anything?
return (
<KeyboardAvoidingView
behavior={Platform.OS === "ios" ? "padding" : "height"}
style={{ flex: 1 }}
>
<View style={styles.card}>
<View style={styles.container}>
<Options setIpt={setIpt} />
<View style={styles.input}>
<ShowIpt /> // form input here
</View>
<Button
title="Create"
color="#841584"
accessibilityLabel="Create"
onPress={() =>
navigation.navigate("Result", {
ipt: ipt,
})
}
/>
</View>
</View>
</KeyboardAvoidingView>
);
const styles = StyleSheet.create({
container: {
margin: 10,
},
card: {
backgroundColor: "#F2F2F2",
margin: 10,
marginTop: 20,
borderRadius: 10,
height: "90%",
display: "flex",
},
input: {
backgroundColor: "#FFF",
height: 250,
alignItems: "center",
justifyContent: "center",
borderRadius: 10,
marginTop: 20,
marginBottom: 20,
},
});
Use automaticallyAdjustContentInsets and contentContainerStyle properties and change your style like below.
<KeyboardAvoidingView
automaticallyAdjustContentInsets={false}
behavior={null}
contentContainerStyle={{ flex: 1 }}
style={{
backgroundColor: 'backgroundColor',
flex: 1,
height: '100%',
}}
>
</KeyboardAvoidingView>
I have a flatlist that renders a clickable item (with touchable opacity) and an image background with some text in the middle.
The goal is to render a flatlist that looks like this:
I am trying to add light opacity to the image by adding a view with an overlay and I tried multiple solutions but nothing seems to work.
This is what I have achieved so far:
This is the item in the flatlist:
// console.log("item", item.latest_image.media[0].url);
if (item.empty === true) {
return <View style={[styles.item, styles.itemInvisible]} />;
}
return (
<TouchableOpacity
onPress={() =>
this.props.navigation.push(Screens.Photos, {
team: item,
id: this.props.navigation.getParam("team").id
})
}
>
<ImageBackground
style={styles.backgroundImage}
imageStyle={{ borderRadius: theme.borders_MediumRadius.borderRadius, backgroundColor: 'rgba(255,0,0,.6)' }}
source={{ uri: item.latest_image.media[0].url }}
>
<Text style={styles.itemText}>{item.title.toUpperCase()}</Text>
</ImageBackground>
</TouchableOpacity>
);
};
This is the flatlist:
<SafeAreaView style={{ flex: 1 }}>
<ScrollView
style={{ flex: 1, backgroundColor: "black" }}
scrollEnabled={scrollEnabled}
onContentSizeChange={this.onContentSizeChange}
>
<FlatList
data={formatData(this.state.data, numColumns)}
style={styles.container}
renderItem={this.renderItem}
numColumns={numColumns}
scrollEnabled={scrollEnabled}
/>
<FollowUs />
</ScrollView>
</SafeAreaView>
);
and these are the style props:
container: {
margin: 7.5,
backgroundColor: "black"
},
backgroundImage: {
flex: 1,
height: hp("25"),
backgroundColor: "black",
margin: 7.5,
justifyContent: "center",
alignItems: "center"
},
itemInvisible: {
backgroundColor: "transparent"
},
itemText: {
fontFamily: "RobotoCondensed-Bold",
fontSize: RF(4),
color: "black"
},
overlay: {
backgroundColor: "rgba(255,0,0,0.5)",
alignItems: "center",
justifyContent: "center",
flex: 1,
margin: 7.5,
height: hp("25"), // approximate a square
borderRadius: theme.borders_MediumRadius.borderRadius
}
});
If anybody could help me it would be really appreciated!
Thanks
Here's an example of how you can achieve this
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<TouchableOpacity
style={{
backgroundColor: '#FFF',
height: 200,
width: 200,
position: 'relative'
}}>
<ImageBackground
source={{ uri: 'https://placehold.it/200x200' }}
style={{
height: 200,
width: 200,
opacity: 0.6,
position: 'absolute',
}}
/>
<View
style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Hello World!</Text>
</View>
</TouchableOpacity>
</View>
);
}
}
Here's an example Snack - https://snack.expo.io/#hannigan/lonely-bubblegum
You should also set the activeOpacity on the TouchableOpacity to be the same value as what you will set on the ImageBackground (0.6)
Solved it by adding a view around the text and giving it these props:
<View
style={{
flex: 1,
alignItems: "center",
justifyContent: "center",
backgroundColor: "rgba(255,255,255,0.75)",
borderRadius: theme.borders_MediumRadius.borderRadius
}}
>
<Text style={styles.itemText}>{item.title.toUpperCase()}</Text>
</View>
I made a custom bottom Navbar for my React-Native App which I want to stick at the bottom of the screen.
This is what it looks like
<View style={styles.NavBarBottom}>
<TouchableOpacity
onPress={() => this.activeTab("home")}>
<Text> <Icon name="bitcoin" size={30} color={ this.state.activeTab == "home" ? "#fbc02d" : "white"} /></Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => this.activeTab("News")}>
<Text> <Icons name="news" size={30} color={this.state.activeTab == "News" ? "#fbc02d" : "white"} /> </Text>
</TouchableOpacity>
</View>
where Styles are this
const styles = StyleSheet.create({
NavBarBottom : {
paddingTop: 8,
borderTopWidth: 2,
backgroundColor: "#005cb2",
display: "flex",
flexDirection: "row",
justifyContent: "space-around",
padding: 5,
}
})
Now this successfully works and show in case of Home Screen but on another screen when I import it, It appears only when the content is loading and afterwards it doesn't appear (attaching the GIF for the same in end). Also on the page where I am just passing the props, it doesn't appear
The code for other page would be
<View>
<Header
navigation = {this.props.navigation}
enable = "true" />
<ScrollView>
<View style={listOfCurrencies}>
{ CurrencyDisplay.map(data =>
<TouchableOpacity>
<View style={IndvidualCurrencyMain}>
<Text style={dataLong}> {data["long"]}</Text>
<Text style={dataShort}>{data["short"]}</Text>
</View>
</TouchableOpacity>)}
</View>
</ScrollView>
<BottomNavigation />
</View>
With Styles like this
const styles = StyleSheet.create({
listOfCurrencies: {
display: "flex",
flexDirection: "column",
marginBottom: 60
},
IndvidualCurrencyMain: {
backgroundColor: "white",
display: "flex",
flexDirection: "row",
height: 80,
alignItems: "center",
height: 50,
borderRadius: 10,
marginLeft: 3,
marginRight: 3,
marginTop: 5,
marginBottom: 5
},
dataLong:{
marginLeft: 3,
width: 150
},
dataShort: {
marginLeft: "48%",
marginRight: 5
},
index: {
marginLeft: 2,
width: 20
}
})
Any idea why it won't be appearing?
You can try this just by adding the main view flex:1 style