React-native: position elements in the center and flex-end - react-native

Is it possible to have the following layout (without resorting to absolute positioning)?
|...........centreElement.......RightElement|
So as if the justifyContent was 'space-between' but without the first element. I also thought to 'justifyContent: 'centre'' and then try 'justifySelf: 'flex-end' on the RightElement but this property does not exist.
<View styles={styles.container}>
<Text>centreElement</Text>
<Text>RightElement</Text>
</View>
container: {
flexDirection: 'row',
justifyContent: 'center'
}

There is mistake in your code please update code from <View styles={styles.container}> to <View style={styles.container}>
Please try the following code:
<View style={styles.container}>
<View style={{ flex: 1 }} />
<View style={{ flex: 1 }}>
<Text>centreElement</Text>
</View>
<View style={{ flex: 1 }}>
<Text>RightElement</Text>
</View>
</View>

An ideal solution without using absolute. The CentreElement text might not comes at the center, but without absolute, you cannot align it either, since you are not giving the content to take up the 100% width which is needed to rightly align the content to center. Following steps can give you the satisfying results:
Just give out flexDirection: 'row' to the container
Give both the text CentreElement and RightElement 50% width
Align both of them to right, and done!!
Code:
export default function App() {
return (
<View style={styles.container}>
{/* Txt container*/}
<View style={styles.textContainer}>
<Text style={styles.text}>
CentreElement
</Text>
<Text style={styles.text}>
RightElement
</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
textContainer: {
width: '100%',
flexDirection: 'row'
},
text: {
textAlign: 'right',
width: '50%',
fontSize: 16,
fontWeight: 'bold'
}
});
Result:

Related

Keyboard changing flex layout in react native with textInput

Layout overlapping on text input in react native as you can see in the images.
I have experimented with keyboardAvoidingView and keyboardAwareScrollView but I just cant fix it or I am implementing them incorrectly.
How to avoid keyboard from changing your flex layout? What's the industry standard? keyboardAvoidingView? I find that inconsistent or have not understood it conceptually. My approach usually is wrapping the entire code with either keyboardAwareScrollView or keyboardAvoidingView. Do I only do it over the textInput component?
const InterviewAlignment = ({navigation}) => {
return (
<View style={styles.container}>
<View style={styles.header}>
</View>
<View style={styles.main}>
<View
style={{
flex: 1,
justifyContent: 'space-between',
}}>
<View
style={{
flex: 1,
backgroundColor: 'red',
}}>
</View>
<View style={styles.modeContainer}>
</View>
<View style={{flex: 1}}>
<Text style={styles.text}>Link Address</Text>
<TextInput
style={styles.linkInput}
/>
</View>
</View>
<View style={{flex: 1, backgroundColor: 'red'}}>
</View>
<View style={{flex: 1.5, backgroundColor: 'black'}}>
</View>
</View>
</View>
);
};
//RFValue is just a responsive wrapper
const styles = StyleSheet.create({
container: {
flex: 1,
},
header: {
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: RFValue(16),
minHeight: RFValue(56),
backgroundColor: '#B1C59B',
},
text: {
fontSize: RFValue(15),
color: 'black',
},
main: {
flex: 1,
padding: 15,
},
modeContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'flex-start',
flex: 1,
backgroundColor: 'yellow',
},
linkInput: {
marginHorizontal: RFValue(40),
padding: 0,
paddingTop: 2,
borderBottomWidth: RFValue(1),
borderBottomColor: '#C4C4C4',
},
});
You can set height instead of flex in styling

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.

React-native: Layout a view next to a centered one

I want to achieve the following:
I want to display a label next to a centered value.
As far as I can see that's not possible with flexbox, right?
How could I achieve that?
Additionally I'd like to have the base-aligned, but currently this option is not working on Android. Therefore I'm using hardcoded paddings for the smaller text boxes. Any other Idea for that?
Another suggestion, if you don't want to use empty Views, is to position Value to the center, and then have the unit positioned absolute (so it goes off the flex) and to the bottom so you ensure it's base-aligned to Value.
<View style={styles.container}>
<Text style={styles.value}>
Value
<Text style={styles.unit}>unit</Text>
</Text>
</View>
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: 'lightgrey',
},
value: {
backgroundColor: '#eee',
textAlign: 'center',
fontSize: 20,
position: 'relative',
},
unit: {
fontSize: 12,
position: 'absolute',
bottom: 0,
},
});
You can use use 3 flex boxes placed horizontally to get a similar effect. Leave the left box blank.
(Ive added background colors for reference)
<View style={styles.container}>
<View
style={{
flexDirection: 'row',
}}>
<View style={{ flex: 1}} /> {/* blank view */}
<View
style={{
flex: 1,
justifyContent: 'flex-end',
alignItems: 'center',
}}>
<Text style={{ fontSize: 30 }}>Value</Text>
</View>
<View
style={{
flex: 1,
justifyContent: 'flex-end',
alignItems: 'fllex-start',
}}>
<Text style={{ fontSize: 12 }}>Unit</Text>
</View>
</View>
</View>

How to vertically stretch two Views that are in a column formation - React Native Flex?

I have this view, where there are two Views in a column:
<View style={styles.container}>
<View style={styles.content}>
<View style={{backgroundColor: 'orange'}}>
<Text>Test</Text>
</View>
<View style={{backgroundColor: 'yellow'}}>
<Text>Test2</Text>
</View>
</View>
</View>
This is my Styles:
container: {
flexDirection: 'row',
backgroundColor: 'white',
justifyContent:'center',
alignItems:'center',
height: 80,
margin: 8,
},
content: {
flex:1,
alignItems: 'stretch',
flexDirection: 'column',
},
Here is a ScreenShot of what it looks with the above code.
How can I get the orange and yellow View to expand vertically equally, without manually defining the height for each of them?
The Text inside should be centered, but left-aligned.
Add flex: 1 to each cell to make them expand and add justifyContent: 'center' to make the text vertically centered. Like this:
<View style={styles.container}>
<View style={styles.content}>
<View style={{ backgroundColor: 'orange', flex: 1, justifyContent: 'center' }}>
<Text>Test</Text>
</View>
<View style={{ backgroundColor: 'yellow', flex: 1, justifyContent: 'center' }}>
<Text>Test2</Text>
</View>
</View>
</View>

KeyboarddAvoidingView not working with keyboard on bottom

I have a component that renders the following:
return (
<KeyboardAvoidingView behavior="position">
<View style={styles.container}>
<View style={styles.messagesContainer}>
</View>
<SafeAreaView>
<View style={styles.inputView}>
<TextInput style={styles.input} />
<Button title="submit" />
</View>
</SafeAreaView>
</View>
</KeyboardAvoidingView>
)
With stylesheet:
const styles = StyleSheet.create({
container:{
height: "100%"
},
messagesContainer: {
backgroundColor: "red",
flex: 1
},
inputView: {
flexDirection: "row",
alignItems: "center",
padding: 5,
height: 24
},
input: {
height: 20,
backgroundColor: "white",
flex: 1
}
});
This view is rendered as a screen in a react-navigation StackNavigation.
I'd like the view to move up to make space for the keyboard, but it doesn't seem to move up far enough.
I know it's moving it up, because if I add some padding to the bottom of my container:
I've also wondered whether the SafeArea component is messing up the calculations of how far up the view should be moved, but it doesn't seem so.
I could tweak that bottom padding until the text input shows up right above the keyboard, but that seems like a flaky solution that'd require different values for different devices, orientations, keyboard layouts, etc.
Is there a more robust solution?
Need some modification in styling try this
return (
<KeyboardAvoidingView behavior="position" style={styles.containerWraper}>
<View style={styles.container}>
<View style={styles.messagesContainer}>
</View>
<SafeAreaView>
<View style={styles.inputView}>
<TextInput style={styles.input} />
<Button title="submit" />
</View>
</SafeAreaView>
</View>
</KeyboardAvoidingView>
)
Styles
const styles = StyleSheet.create({
container:{
flex: 1,
justifyContent: 'center'
},
containerWraper:{
flex: 1,
justifyContent: 'center'
},
messagesContainer: {
backgroundColor: "red",
flex: 1
},
inputView: {
flexDirection: "row",
alignItems: "center",
padding: 5,
height: 24
},
input: {
height: 20,
backgroundColor: "white",
flex: 1
}
});
Try NativeBase instead, that has better approach