React Native Android: How to display image and text in one row? - react-native

I have tried to create react native app for android where child image and name will display in one line. You can check it on rawgit.
import React, { Component } from 'react'
import { AppRegistry, Text, View, Image } from 'react-native'
export default class App extends Component {
render() {
return (
<View>
<Text style={{ textAlign: 'left', fontSize: 15, backgroundColor:'grey'}}>
<Image source={{uri: 'https://leaderbord-552b1.firebaseapp.com/AdminLTE%202%20_%20User%20Profile_files/mount-carmel-logo.png'}} style={{width: 70, height: 70, borderRadius:70}} />
<Text style={{ backgroundColor:'lightblue', }}>Vindhyachal</Text>
</Text>
</View>
)
}
}
AppRegistry.registerComponent('App', () => App)
I am getting below output which is wrong, text is displaying at bottom instead of same line at middle.
Note:- I can't change the component structure because I am using it in react-native-material-ui-demo-app Drawer as below.
<Drawer.Section
divider
items={[
{ value: <Text style={{ textAlign: 'left', fontSize: 15, backgroundColor:'grey'}}>
<Image source={{uri: child.image'}} style={{width: 70, height: 70, borderRadius:70}} />
<Text style={{ backgroundColor:'lightblue', }}>{child.name}</Text>
</Text> },
{ value: <Text style={{ textAlign: 'left', fontSize: 15, backgroundColor:'grey'}}>
<Image source={{uri: child.image'}} style={{width: 70, height: 70, borderRadius:70}} />
<Text style={{ backgroundColor:'lightblue', }}>{child.name}</Text>
</Text> }
]}
/>
Below is screenshot of my app where I am facing this issue.
I'll be grateful if someone can help me out with this issue. Thanks in advance.

It can be achieved by two ways as follows:
1] With inner Text block removed (Just need to add alignSelf: "center" to your Text) as follows:
<View style={{flexDirection: 'row', textAlign: 'left', fontSize: 15, backgroundColor:'grey'}}>
<Image source={{uri: 'https://leaderbord-552b1.firebaseapp.com/AdminLTE%202%20_%20User%20Profile_files/mount-carmel-logo.png'}} style={{width: 70, height: 70, borderRadius:70}} />
<Text style={{ backgroundColor:'lightblue', alignSelf: "center" }}>Vindhyachal</Text>
</View>
2] Without Text removed using your existing Drawer.Section code (Need to provide float:"left" style and positioning your Text as absolute with top value) as follows:
<Text style={{ textAlign: 'left', fontSize: 15, backgroundColor:'grey', alignItems:"center", float:"left", position:"absolute"}}>
<Image source={{uri: 'https://leaderbord-552b1.firebaseapp.com/AdminLTE%202%20_%20User%20Profile_files/mount-carmel-logo.png'}} style={{width: 70, height: 70, borderRadius:70, float:"left"}} />
<Text style={{ backgroundColor:'lightblue', float:"left", position:"absolute", top:"45%"}}>{child.name}</Text>
</Text>

Here's an example I've used to handle what you need:
import React, { Component } from 'react';
import {
Text, View, Image
} from 'react-native';
import Dimensions from 'Dimensions';
const DeviceWidth = Dimensions.get('window').width;
const DeviceHeight = Dimensions.get('window').height;
class MyClass extends React.Component {
render() {
return (
<View style={{flexDirection:'row', width:DeviceWidth, height:DeviceWidth*0.5, alignItems:'center'}}>
<Image source={{uri : 'someUrl'}}
style={{width:DeviceWidth*0.5, height:DeviceWidth*0.5, resizeMode:'contain'}}/>
<Text>Next to Image</Text>
</View>
);
}
}
export default MyClass;

Here goes the updated code where moving the flex styling to the View tag. Sharing a URL where you can play with flex.
export default class App extends Component {
render() {
return (
<View style={{flexDirection:'row',alignItems:'center',justifyContent:'center'}}>
<Text style={{ textAlign: 'left', fontSize: 15, backgroundColor:'grey'}}>
<Image source={{uri: 'https://leaderbord-552b1.firebaseapp.com/AdminLTE%202%20_%20User%20Profile_files/mount-carmel-logo.png'}} style={{width: 70, height: 70, borderRadius:70}} />
<Text style={{ backgroundColor:'lightblue', }}>Vindhyachal</Text>
</Text>
</View>
)
}
}
AppRegistry.registerComponent('App', () => App)

Related

Problem with placing a pressable text in react native

I am working on a react native application and I was dealing with Modal and Pressable components, in fact the problem I got is not an error or a bug but I was trying to place a pressable text at the very right down side of the screen but I am struggling with styling a little bit so if anyone can give me a hand in this I would be so grateful.
this the code I wrote for the pop up form component:
import React from 'react';
import {View, Text, Modal, StyleSheet, Pressable, Alert} from 'react-native';
const PopUpModal = ({visible, onPress})=>{
return(
<View style={styles.container}>
<Modal
animationType="slide"
transparent={true}
visible={visible}
onRequestClose={() => {
Alert.alert("Modal has been closed.");
}}
>
<View style={styles.container}>
<View style={styles.modalView}>
<Text style={styles.modalText}>Hello World!</Text>
<Pressable
style={[styles.button, styles.buttonClose]}
onPress={onPress}
>
<Text style={styles.textStyle}>Cancel</Text>
</Pressable>
</View>
</View>
</Modal>
</View>
)
}
const styles = StyleSheet.create({
container:{
width: "100%",
height : "100%"
},
modalView: {
margin: 20,
backgroundColor: "white",
borderRadius: 5,
padding: 35,
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 2
},
shadowOpacity: 0.25,
shadowRadius: 4,
elevation: 5,
},
button: {
display: "flex",
borderRadius: 2,
padding: 10,
flexDirection:"row-reverse",
width: 65,
backgroundColor: "#051316",
},
buttonClose: {
},
textStyle: {
color: "white",
fontWeight: "bold",
textAlign: "center",
alignItems: "flex-end"
},
modalText: {
marginBottom: 15,
flexDirection: "row-reverse"
}
})
export default PopUpModal
and this is what it looks like as an output of the above code example
output pic
Put your Pressable inside a View with the style property flexDirection:"row-reverse"
<View style={{flexDirection:"row-reverse"}}>
<Pressable
style={[styles.button, styles.buttonClose]}
onPress={onPress}
>
<Text style={styles.textStyle}>Cancel</Text>
</Pressable>
</View>

React Native app with same code but different result for vertical margins in iOS and Android

I tested a simple render() on android (Galaxy S7) and iOS (iPhone S8+), and I get results that I don't understand.
The S7's height in dp's (Density-independent Pixels) is 640, while the iPhone 8 Plus height is 736 dp's, so I expected the distance between 'email' and 'password' to be somewhat smaller on the iPhone 8+, but not that tiny...
The 2nd issue is negative margins, that seem to behave differently on the two platforms. Is that what one should expect?
(And, yes, I know that I can set different margins on the two platforms, but I want to understand why the results are so different from my expectations...)
This is my code:
import React, { Component } from 'react';
import { View, TextInput } from 'react-native';
export class Login extends Component {
render() {
return (
<View style={{ marginLeft: 100 }}>
<View style={{ marginTop: 25 }}>
<TextInput
style={{ color: 'black', width: 260 }}
value='email'
/>
<View style={{ marginTop: -10,
borderBottomWidth: 1,
width: 200,
borderBottomColor: 'black' }} />
</View>
<View style={{ marginTop: 5 }}>
<TextInput
style={{ color: 'black', width: 260 }}
value='password'
/>
<View style={{ marginTop: -10,
borderBottomWidth: 1,
width: 200,
borderBottomColor: 'black' }} />
</View>
</View>
);
}
}
And this is how this code is displayed on an android Galaxy S7 emulator (left) and iPhone 8+ emulator.
I know this isn't your question, and i saw your profile and you're a react god yourself, and i respect that a lot haha
But my question is, why is the code like this, instead of being something like:
import React, { Component } from 'react';
import { View, TextInput } from 'react-native';
export class Login extends Component {
render() {
return (
<View style={{ alignItems: 'center' }}>
<TextInput
style={{color: 'black', width: 260, borderBottomWidth : 4, marginTop: 25}}
value='email'
/>
<TextInput
style={{color: 'black', width: 260, borderBottomWidth : 4, marginTop: 5}}
value='password'
/>
</View>
);
}
}
Do this code works the same way?
[EDIT]
Also, maybe it's because some problems with the IOS top and bottom bar (On newer iphones, wich is not the case). So maybe the "Top" of the Android isn't the same "Top" as the IOS because Android apps doesn't overlaps the top bar like IOS do.
So you can make a conditional check to change the IOS MarginTop value, like this:
import React, { Component } from 'react';
import { View, TextInput, Platorm } from 'react-native';
export class Login extends Component {
render() {
return (
<View style={{ marginLeft: 100 }}>
<View style={{ marginTop: Platform.OS == 'android' ? 25 : 35 }}>
<TextInput
style={{ color: 'black', width: 260 }}
value='email'
/>
<View style={{ marginTop: Platform.OS == 'android' ? -10 : -5,
borderBottomWidth: 1,
width: 200,
borderBottomColor: 'black' }} />
</View>
<View style={{ marginTop: Platform.OS == 'android' ? 5 : 15}}>
<TextInput
style={{ color: 'black', width: 260 }}
value='password'
/>
<View style={{ marginTop: Platform.OS == 'android' ? -10 : -5,
borderBottomWidth: 1,
width: 200,
borderBottomColor: 'black' }} />
</View>
</View>
);
}
}
I think this should work as fine.
The other answer and comment were good, but they didn't answer my question... :)
I asked why there is such a difference between the two platforms and not how it can be modified with platform specific values.
I found the answer here:
react-native TextInput displays wrong when changing height on Android
quote: Android adds some default padding on top and bottom, you can reset them by adding paddingVertical: 0 to your element' style.
When specifying paddingVertical: 0, we get the expected results:
This is the updated code:
import React, { Component } from 'react';
import { View, TextInput } from 'react-native';
export class Login extends Component {
render() {
return (
<View style={{ marginLeft: 100 }}>
<View style={{ marginTop: 25 }}>
<TextInput
style={{ color: 'black', width: 260, paddingVertical: 0 }}
value='email'
/>
<View style={{ marginTop: 0,
borderBottomWidth: 1,
width: 200,
borderBottomColor: 'black' }} />
</View>
<View style={{ marginTop: 15 }}>
<TextInput
style={{ color: 'black', width: 260, paddingVertical: 0 }}
value='password'
/>
<View style={{ marginTop: 0,
borderBottomWidth: 1,
width: 200,
borderBottomColor: 'black' }} />
</View>
</View>
);
}
}

React Native error: Super expression must either be null or a function

I get the error when i trie to run this app on my Android Emulator. I changed "Component" to "React.Component" but then i get other Problems.
My versions of React, Node and JS are the newest.
The Problem is that im new and have this Code from a Viedo and he didnt got thet Mistake.
Without React.Component:
With React.Component:
import React from 'react';
import {
StyleSheet,
Text,
View,
TextInput,
TouchableOpacity,
StatusBar,
Component,
} from 'react-native';
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<StatusBar backgroundColor="#1e90ff" barStyle="light-content" />,
<Text style={styles.login}>Login</Text>,
<TextInput style={styles.input} placeholder="Username" />,
<TextInput
style={styles.input}
placeholder="Password"
secureTextEntry
/>
<View style={styles.btnContainer}>
<TouchableOpacity style={styles.userBtn}>
<text style={styles.btnText}>Login</text>
</TouchableOpacity>
<TouchableOpacity style={styles.userBtn}>
<text style={styles.btnText}>Text</text>
</TouchableOpacity>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
backgroundColor: '#1e90ff',
},
login: {
textAlign: 'center',
fontSize: 35,
margin: 10,
color: '#fff',
},
input: {
width: '80%',
backgroundColor: '#fff',
padding: 10,
marginBottom: 10,
height: 30,
},
btnContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
width: '90%',
},
userBtn: {
backgroundColor: '#fff',
padding: 15,
width: '40%',
margin: 10,
},
btnText: {
fontSize: 25,
textAlign: 'center',
color: '#1e90ff',
},
});
Can someone help me?
1. TypeError: Super expression must either be null or a function
To fix this
import React, { Component } from 'react';
Then you can use
export default class App extends Component
2. Invariant Violation: Text strings must be rendered within a component
To fix this remove , & change
<text style={styles.btnText}>Login</text>
to
<Text style={styles.btnText}>Login</Text>
EDIT
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<StatusBar backgroundColor="#1e90ff" barStyle="light-content" />
<Text style={styles.login}>Login</Text>
<TextInput style={styles.input} placeholder="Username" />
<TextInput
style={styles.input}
placeholder="Password"
secureTextEntry
/>
<View style={styles.btnContainer}>
<TouchableOpacity style={styles.userBtn}>
<Text style={styles.btnText}>Login</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.userBtn}>
<Text style={styles.btnText}>Text</Text>
</TouchableOpacity>
</View>
</View>
);
}
}
Hope this helps you. Feel free for doubts.

Parameter Routes in react-native

Formy post screen I want to take the title and body of each one
import React, { Component } from 'react';
import { StyleSheet, TouchableOpacity, Image, Text, View, Dimensions, TouchableWithoutFeedback } from 'react-native';
import { withNavigation } from 'react-navigation';
class Card extends Component {
render() {
return (
<TouchableWithoutFeedback onPress={() => this.props.navigation.navigate('Post')}>
<View style={{
flexDirection: 'row',
backgroundColor: 'white',
borderBottomWidth: 1,
borderRightWidth: 1,
borderLeftWidth: 1,
borderColor: 'rgba(200,200,200,0.3)',
borderRadius: 6,
width: '86%',
maxHeight: 120,
minHeight: 120,
alignSelf: 'center',
marginBottom: 5
}}>
<Image style={{
width: 100,
maxHeight: 100,
minHeight: 100,
borderRadius: 20,
}}
source={{ uri: 'https://im.ziffdavisinternational.com/ign_br/screenshot/default/screenshot-2_5s54.png',
}}
/>
<View style={{
width: '66%',
padding: 5}}
>
<Text style={{
fontSize: 14,
fontWeight: 'bold',
fontFamily: 'verdana',
}}
numberOfLines={2}
>
{this.props.item.title}
</Text>
<Text style={{
fontSize: 12,
marginTop: 5
}} numberOfLines={3}
>
{this.props.item.body}
</Text>
</View>
</View>
</TouchableWithoutFeedback>
);
}
}
export default withNavigation(Card);
The question is very unclear about what you want to accomplish with a very little piece of information.
If the question is about passing parameters from a screen to another (in this case, title and body), using react navigation, then you just have to change your navigate function in the TouchableWithoutFeedback to:
this.props.navigation.navigate('Post',{ "title":this.props.item.title, "body": this.props.item.body })
Then, when you want to use them in the destination screen (in this case, Post), you can access them using:
this.props.navigation.state.params.title //or this.props.navigation.state.params.body
or do
var title=this.props.navigation.getParam("title")

How to set text and an icon above the image with separate view in react-native

I need to show a text and icon above the image. But here the image view and text and loading icon need not to be linked. Both need to have a separate view.
I have set two views (one view for image and another one for and add the marginTop:-300 values in my view. It is a wrong behavior.
<View style={{ aspectRatio: 4 / 5, alignSelf: 'center', width: '100%', paddingLeft: '5%', paddingRight: '5%', opacity: 0.4,}}>
<FastImage resizeMode={FastImage.resizeMode.cover} style={{width: '100%',height: '100%',marginTop:18}} source={["imageUrl"]}/>
</View>
<View style={{flex:1, alignSelf: 'center', alignItems: 'center', justifyContent: 'center', marginTop:-50}}>
<ActivityIndicator size={"large"} color={"#ffffff"} style ={{marginTop:-700,}}/>
<Text style={{ fontSize: 15, fontFamily: "OpenSans-Semibold",marginTop:-300,color:'white'}}>Image captured</Text>
</View>
https://i.stack.imgur.com/MF5kM.png
You can use Position
Example This is an example of an image link I've created.
import React, { Component } from 'react';
import { View, Image,ActivityIndicator, Text } from 'react-native';
export default class App extends Component {
render() {
return (
<View style={{flex:1,justifyContent:"center",alignItems:"center"}}>
<View style={{opacity:0.5}}>
<Image
style={{width: 50, height: 50 }}
source={require('#expo/snack-static/react-native-logo.png')}
/>
<Image
style={{width: 50, height: 50, }}
source={{uri: 'https://facebook.github.io/react-native/img/tiny_logo.png'}}
/>
<Image
style={{width: 66, height: 58}}
source={{uri: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAAAzCAYAAAA6oTAqAAAAEXRFWHRTb2Z0d2FyZQBwbmdjcnVzaEB1SfMAAABQSURBVGje7dSxCQBACARB+2/ab8BEeQNhFi6WSYzYLYudDQYGBgYGBgYGBgYGBgYGBgZmcvDqYGBgmhivGQYGBgYGBgYGBgYGBgYGBgbmQw+P/eMrC5UTVAAAAABJRU5ErkJggg=='}}
/>
</View>
<ActivityIndicator size={"large"} color={"red"} style={{position:"absolute"}}/>
<Text style={{position:"absolute",color:"red",bottom:"45%"}} > Loading </Text>
</View>
);
}
}