Can't align 2 views vertically and horizontally in React-Native - react-native

I have 2 Views One view on the left that have 2 items aligned next to each other, and the other is on the right like this, and on IOS it's worse
The problem as you can see here the name is not align to the arrow, I can't figure what the reason is. Here is my code:
<View style={styles.header}>
<View style={styles.leftHeader}>
<TouchableOpacity onPress={() => navigation.goBack()}>
<ArrowLeft />
</TouchableOpacity>
<View>
<Text style={styles.username}>{user && user.username}</Text>
</View>
<TouchableOpacity
onPress={() => setMoreModalVisible(true)}
style={styles.twoDots}>
<TwoDots />
</TouchableOpacity>
</View>
and here is the styling:
header: {
paddingHorizontal: wp(3),
flexDirection: 'row',
marginTop: hp(2.5),
marginBottom: hp(1),
justifyContent: 'space-between',
alignItems: 'center',
},
leftHeader: {
flexDirection: 'row',
justifyContent: 'flex-start',
textAlignVertical: 'center',
},
username: {
fontFamily: semiBoldFont,
fontSize: 15,
paddingLeft: wp(3),
},

Check the style of <ArrowLeft /> component. There might be some padding or margin applied there.
Or provide us with a snack for you code so we can be able to help you.

You can't change the height of the header because the headerStyle prop doesn't have a height prop.
You can follow the docs reference on this link:
https://reactnavigation.org/docs/headers#adjusting-header-styles
Another way to modify the header style you can set the headerShown prop to false and build your own header in the component where you can define the view height of your custom header, but be carefully because if you turn of the header in react-navigation then you have to use the safeareaview from react-native-safe-area-context because your screen will be slip up. On Android you don't take care about this.
A little code snipett for the example:
<Stack.Screen
name="example"
component={Component}
options={{headerShown: false}}
/>
function Component() {
const headerHeight = Platform.OS === 'ios' ? 44 : 20
return (
<SafeAreaView edges={["top"]}>
<View style={{height: headerHeight}}>
// your custom header content
</View>
</SafeAreaView>
)
}

Related

React Native need <Text> to appear next to the pressable checkbox not inside

I need that the Text appears next to the pressable checkbox not inside it... How can i do this using styles and not changing the code structure?
return (
<Pressable
style={[{ opacity: disabled ? 0.5 : 1 },
styles.checkboxBase, checked && styles.checkboxChecked]}
onPress={onPress}>
<View style={[styles.checkboxContainer, style]}>
{checked && <Icon size={16} name="bullet"
color="gray" />}
</View>
<Text style={styles.text}>{label}</Text>
</Pressable>
);
}
const styles = StyleSheet.create({
checkboxBase: {
width: 24,
height: 24,
},
checkboxChecked: {
justifyContent: "center",
alignItems: "center",
},
checkboxContainer: {
flexDirection: "row",
color: "red",
},
text: {},
});
Pressable is like a view with TouhcableOpacity. You have to put text and Pressable inside a partent probability in a view like this.
<View style={{flex-direction:'row'}}>
<Pressable>
<Icon />
</Pressable>
<Text></Text>
</View>

How do i prevent the keyboard from pushing my view up in react native?

I am building a registration form in React Native. I have this view.
When keyboard is showing it is pushing my form and image over my title view like this.
What should i do so that it my view doesnot lose its shape. I already tried scrollview and keyboard avoiding view but they are not helping.
Here is my code.
<View style={styles.container}>
<View style={styles.headingContainer}>
<Text style={styles.headingText}>Sign Up</Text>
</View>
<View style={styles.form}>
<View style={{ marginTop: 5, width: '100%', padding: 10 }}>
<Input
name="name"
placeholder="Name"
label="Name"
/>
<View style={styles.buttons}>
<Button onPress={handleSubmit}>
{isLogin ? 'Log In' : 'Sign Up'}
</Button>
</View>
</View>
</View>
<View style={styles.logoCon}>
<Image style={styles.logo} source={Logo} />
</View>
</View>
);
}
export default AuthForm;
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
justifyContent: 'flex-start',
alignItems: 'flex-start',
},
headingContainer: {
justifyContent: 'center',
alignItems: 'center',
width: '100%',
height: '40%',
backgroundColor: Colors.primary800,
marginBottom: '-22%',
},
form: {
backgroundColor: Colors.primary100,
justifyContent: 'center',
alignItems: 'center',
height: 'auto',
width: '100%',
borderTopLeftRadius: 80,
},
logoCon: {
alignSelf: 'center',
height: 100,
width: 100,
marginTop: 'auto',
marginBottom: -15,
},
});
Update: I have solve the above issue, but now I am facing another issue i.e. I have to hide keyboard everytime i try to input text in any of the lower inputs. Also it is pushing my top view up inside the screen. NO scrollview is working. I have tried KeyboardAvoidingView, ScrollView, Animation, KeyboardAwareScrollView so far but got no luck.
PS: I know UIs are different. Its because i have completed the UI and this is the last thing i need to work on.
I have managed to overcome this issue using this npm package: react-native-keyboard-aware-scroll-view
npm install --save react-native-keyboard-aware-scroll-view
Then in your component, the basic usage is like below
return (
<Fragment>
<SafeAreaView style={{flex: 1}}>
<KeyboardAwareScrollView keyboardShouldPersistTaps='always'>
<View style={{flex: 1}}>
//The content
</View>
</KeyboardAwareScrollView>
</SafeAreaView>
</Fragment>
);
If you have a modal, I'd suggest you to use it like that
<Modal
animationType="slide"
transparent={true}
visible={this.state.search_modal_visible}
>
<KeyboardAwareScrollView contentContainerStyle={{flexGrow: 1}}>
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
//The content
</View>
</KeyboardAwareScrollView>
</Modal>
In your AndroidManifest.xml, please be sure the line below exists as an attribute for activity tag
<activity
android:windowSoftInputMode="adjustResize"
You can hide keyboard whenever you want like that in your code
Keyboard.dismiss();
The problem here is that you have in your AndroidManifest.xml:
windowSoftInputMode="adjustResize";
Change it to:
windowSoftInputMode="adjustPan"
Note: after this change you'll need run ./gradlew clean in android folder and react-native run-android in your project directory
Try to use KeyboardAvoidingView of react native
I hade the same issue, I used this to just hide the views I don't want to see whenever the keyboard is opened: https://github.com/TIKramer/react-native-hide-onkeyboard
If you found another solution please let me know.

text in flatlist going off screen react native

I tried basically every answer from this question and nothing seems to work: React native text going off my screen, refusing to wrap. What to do?
I am facing the same issue where my text goes off-screen and seems to be operating at a width of about 130% or so.
Here is the screen I render the list:
<View style={styles.container}>
<FlatList
data={messagePreviewData}
renderItem={({ item }) =>
<ChatPreview
readMessage={item.readMessage}
imgUri={item.imgUri}
username={item.username}
time={item.time}
message={item.message}
/>
}
keyExtractor={(item, index) => item.username + index}
/>
</View>
const styles = StyleSheet.create({
container: {
backgroundColor: colors.neutralOne,
flex: 1,
}
});
And here is the ChatPreview component, where the message text goes off-screen:
const ChatPreview = (props: ChatPreviewProps) => {
const { readMessage, imgUri, username, time, message } = props;
return (
<View style={styles.container}>
<View style={styles.leftContainer}>
<View style={styles.badgeAvatarContainer}>
<Avatar
rounded
source={{ uri: imgUri }}
size={scaledSize(50)}
/>
</View>
<View style={styles.usernameMessageContainer}>
<Text style={styles.username}>{username}</Text>
<Text style={styles.messagePreview} numberOfLines={2} ellipsizeMode='tail'>
{message} // this is what's going off screen
</Text>
</View>
</View>
<Text style={styles.time}>{time}</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
justifyContent: 'space-between',
padding: 10,
},
leftContainer: {
flexDirection: 'row'
},
badgeAvatarContainer: {
flexDirection: 'row',
alignItems: 'center',
marginRight: 10
},
usernameMessageContainer: {
// I have tried putting many things here and nothing seems to work
},
username: {
color: colors.neutralEight,
fontFamily: fonts.bodyTwoBold.fontFamily,
fontSize: fonts.bodyTwoBold.fontSize,
lineHeight: fonts.bodyTwoBold.lineHeight,
},
messagePreview: {
color: colors.neutralFour,
fontFamily: fonts.captionOne.fontFamily,
fontSize: fonts.captionOne.fontSize,
lineHeight: fonts.captionOne.lineHeight,
},
time: {
color: colors.neutralFour,
fontFamily: fonts.captionTwo.fontFamily,
fontSize: fonts.captionTwo.fontSize,
lineHeight: fonts.captionTwo.lineHeight
}
});
It might be worth setting flex:1 from parent down to the child where the content is going off screen. I've had issues where I've forgotten to do it and it's been messed up.
Essentially you could add flex:1 to every style in ChatPreview to try it out, It won't look super nice probably but you should see if the issue is fixed I believe and tweak it from there.
coincidently i was doing a similar thing yesterday. So as you are using flex-direction row , thus , the text (which tries to get all the width given to it, by default 100%) goes out of screen as now 100% is what you said 130%. Now to restrict it, we just need to restrict its width. So set width on the text, maybe only 60% or 70% and it will work fine.

React Native - Stacking app header content

I'm trying to stack content in the Navigation top bar that is being set using the setOptions method. However, I can't seem to stack vertically two pieces of content. Instead, I can only show a single piece of content. Keep in mind this header bar has no true navigation linking and is purely a "title" bar with text and imagery. It is also within the same component I use to actually create my navigation with createBottomTabNavigator().
What I would like is, pseudo-visually:
<Text><Image><Text>
<Text>
Here is my code:
navigation.setOptions({
headerTitle: (
<Text
style={{
flex: 1,
flexDirection: 'row',
flexWrap: 'wrap',
}}
>
<Text
style={{
fontSize: 16,
lineHeight: 16,
}}
>
Left Text{' '}
</Text>
<Image
source={require('../assets/images/icon-large.png')}
style={{ resizeMode: 'contain', width: 25, height: 25 }}
/>
<Text
style={{
fontSize: 16,
lineHeight: 16,
}}
>
{' '}
Right Text
</Text>
</Text>
),
});
which gives me, pseudo-visually:
<Text><Image><Text>
Now, i've tried various layout's of <View> and <Text> and just cant seem to get the stacking visual im looking for. I've tried wrapping it all with a <View> and then a last <Text> but I believe the headerTitle property needs a <Text> or type string assigned to it.
Any suggestions how I can get another <Text> underneath (and centered) what I have already?
With a combination of this and moving the navigation settings to the Stack.Screen I was able to get what I wanted. My problem seemed to be that I was trying to set the header options within BottomTabNavigator. Instead, if I passed in a custom "Header" component, it rendered the way I wanted it. Like so:
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Scroll Rack"
options={{ headerTitle: (props) => <Header /> }}
component={BottomTabNavigator}
/>
</Stack.Navigator>
</NavigationContainer>
You need to have a View which behaves as a row inside a View which behaves as a column. In the example below, the first View as flexDirection: 'column' as default.
<View style={{backgroundColor: "#ff0000"}}>
<View style={{flexDirection: 'row', justifyContent: "space-between", backgroundColor: "#00ff00"}}>
<Text>Left</Text>
<Image
source={{uri: "https://miro.medium.com/max/712/1*EnF9uIN_u2_X7ey24lB7Tg.png"}}
style={{ resizeMode: 'contain', width: 25, height: 25 }}
/>
<Text>Right</Text>
</View>
<Text style={{textAlign: "center"}}>Bottom</Text>
</View>
Here's a snack of it

Style the container of React Native's FlatList items separate from the header / footer?

I have a FlatList using the ListHeaderComponent and ListFooterComponent.
Is there a way to style a container of the items (which come from the data prop), but not include the header and footer with in?
https://snack.expo.io/#jamesweblondon/bold-pretzel
import React from "react";
import { View, Text, FlatList } from "react-native";
const exampleData = [...Array(20)].map((d, index) => ({
key: `item-${index}`,
label: index,
backgroundColor: `rgb(${Math.floor(Math.random() * 255)}, ${
index * 5
}, ${132})`,
}));
const Example = () => {
const renderItem = ({ item }) => {
return (
<View
style={{
flexDirection: "row",
width: "100%",
backgroundColor: item.backgroundColor,
}}
>
<Text
style={{
fontWeight: "bold",
color: "white",
fontSize: 32,
height: 100,
}}
>
{item.label}
</Text>
</View>
);
};
return (
<View style={{ flex: 1 }}>
<FlatList
data={exampleData}
renderItem={renderItem}
keyExtractor={(item) => item.key}
ListHeaderComponent={
<View
style={{
backgroundColor: "grey",
height: 200,
justifyContent: "center",
alignItems: "center",
}}
>
<Text>Before list</Text>
</View>
}
ListFooterComponent={
<View
style={{
backgroundColor: "grey",
height: 200,
justifyContent: "center",
alignItems: "center",
}}
>
<Text>After list</Text>
</View>
}
/>
<View
style={{
backgroundColor: "gold",
height: 200,
justifyContent: "center",
alignItems: "center",
}}
>
<Text>Footer</Text>
</View>
</View>
);
};
export default Example;
Currently it looks like this:
Id like an element allowing me to wrap data so I can add padding, border, etc:
You can use columnWrapperStyle prop instead of contentContainerStyle prop of FlatList. This will help you to make styling of the wrapper of the components generated from the data.
For demo just add border property and you will see this will only apply styles to the container of items and not to ListHeaderComponent and ListFooterComponent
Example
<FlatList
....
columnWrapperStyle={{borderWidth: 1, borderColor: 'red'}}
/>
Note: Please make sure as the name of the prop suggests, the style will be applied to each colum. Also if this prop does not work for you then consider using numColumns prop of FlatList first then apply style with columnWrapperStyle
Check out the contentContainerStyle prop in FlatList, it should help you do exactly what you are looking for.
I have found a workaround that works for me, but I'm not sure if it can cause performance issues, so beware.
The idea is to have two FlatLists, where the one containing your actual list is the FooterListComponent of the other one.
N.B. notice how it's not a FlatList wrapped in a ScrollView, which would trigger the Virtualizedlists should never be nested inside plain scrollviews error.
Here's the code
const MyComponent = () => {
return (
<FlatList
data={[]}
renderItem={() => undefined}
ListHeaderComponent={ListHeader} // whatever you want above your list
ListFooterComponent={() => {
return (
<View style={yourContainerStyle}>
<FlatList
data={data}
renderItem={renderItem}
/>
</View>
);
}}
/>)
}