How to set text and an icon above the image with separate view in react-native - 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>
);
}
}

Related

react native image not show on android emulator

below is my code
<View style={{ flex: 1, backgroundColor: "#f1f5f7", alignItems: "center" }}>
<ScrollView>
<ImageBackground
source={{
uri:
"https://mosfeed.id/assets/collections/ads/small/photo_2021-03-10%2016.38.26.jpeg",
}}
style={{
width: screenWidth,
height: 190,
alignItems: "center",
justifyContent: "center",
paddingHorizontal: 50,
}}
></ImageBackground>
<ItemData title="Upcoming Events" data={DATA1} />
<ItemData title="Premium" data={DATA2} />
</ScrollView>
<MaterialSnackbar ref={snackbarRef} />
</View>
the problem is that the image not showing on android emulator only and only from my server. but when i change the link from other server link the image show.
is there any setting so the image can be showed on my server.
thanks
i achieved this by:
import { ImageBackground } from 'react-native';
<ImageBackground style={ styles.imgBackground }
resizeMode='cover'
source={require('./Your/Path.png')}>
//place your now nested component JSX code here
</ImageBackground>
And then the Styles:
imgBackground: {
width: '100%',
height: '100%',
flex: 1
},

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 Android: How to display image and text in one row?

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)

React Native cropped border

I am very new to React Native and I am trying to create a view as shown in the below image.
Is it possible to create a semi circular cropped border view as highlighted in red in the attached image?
I wrote the example code, output not exactly same but it would be helpful
Output of the code :
import React from 'react';
import {View, Text} from 'react-native';
import {height, width} from 'react-native-dimension';
import moment from 'moment';
const Test5 = () => {
return (
<View style={{flex: 1, backgroundColor: 'white'}}>
<View
style={{
alignSelf: 'center',
height: height(80),
width: width(90),
marginTop: width(20),
}}>
<View
style={{
flex: 1,
backgroundColor: 'grey',
}}>
<View
style={{
alignSelf: 'center',
height: height(27),
width: width(80),
borderRadius: width(4),
marginTop: width(20),
backgroundColor: 'white',
}}>
<View
style={{justifyContent: 'space-between', flexDirection:
'row'}}>
<View>
<Text style={{fontWeight: 'bold', padding: 10}}>CODE200</Text>
</View>
<View
style={{
alignSelf: 'flex-end',
marginTop: height(7),
padding: 10,
}}>
<Text style={{textAlign: 'right'}}>Valid Till</Text>
<Text style={{textAlign: 'right'}}>
{moment().format('dddd, MMMM Do YYYY')}
</Text>
</View>
</View>
<View
style={{
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row',
}}>
<View
style={{
height: height(4),
width: width(8),
borderRadius: width(10),
backgroundColor: 'grey',
}}
/>
<Text style={{color: 'grey'}}>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - -
</Text>
<View
style={{
height: height(4),
width: width(8),
borderRadius: width(10),
backgroundColor: 'grey',
}}
/>
</View>
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
padding: 10,
}}>
<View>
<Text style={{textAlign: 'left'}}>APPLICABLE ON</Text>
<Text style={{textAlign: 'left'}}>Today</Text>
</View>
<View style={{alignSelf: 'flex-end'}}>
<Text style={{fontWeight: 'bold', textAlign: 'right'}}>Rs: 200/-</Text>
</View>
</View>
</View>
</View>
</View>
</View>
);
};
export default Test5;
I think this would require some css wizardery... Maybe you can can get the blank ticket as an asset and have it be the background Image for the view?
https://facebook.github.io/react-native/docs/images.html#background-image-via-nesting
<ImageBackground
imageStyle={{ resizeMode: 'stretch'}}
style={{flex: 1,flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
alignContent: 'center',
height: textEntryHeight, }}
source={
highlightColor === 'magenta' || this.state.keyboardActive ?
textEntryHighlighted : textEntryNonHighlighted}
>
{textEntryHeight > 55 ?
<TextInput style={{
marginLeft: textEntryHeight > 55 ? 48 : 23,
color: 'white',
fontSize: bodyFontSize,
alignSelf: 'center',
width: '100%',
}}
returnKeyType='done'
onSubmitEditing={Keyboard.dismiss}
ref='textInput'
autoFocus={this.props.openKeyboard}
value={body}
onChangeText={(text) => updateHomeTextInputContents(text)}
underlineColorAndroid={'transparent'}
selectionColor={'white'}
keyboardAppearance={'dark'}
onFocus={() => this.setState({keyboardActive: true})}
onBlur={() => this.setState({keyboardActive: false})}
/> :
<Text style={{marginLeft: textEntryHeight > 55 ? 48 : 23,
color: 'white',
fontSize: bodyFontSize,
alignSelf: 'center'}}>
{body}
</Text>
}
<TouchableOpacity style={{position: 'absolute', right: 0}}
onPress={iconOnPress}>
<Image style={{height: 70, width: 70, right:0}} source=
{icon}/>
</TouchableOpacity>
</ImageBackground>
There is no out-of-the-box solution in React Native for this, but I think the simplest way to go is to create a component which you use as separator, only for this part:
For this you only need a parent View with a grey background, 2 semi circles, and a View with a white background which wraps a View with a dashed border style
I think you want something like this.--------------
Used this component for dash ---'react-native-dash'
/**
* Sample React Native App
* https://github.com/facebook/react-native
* #flow
*/
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
TouchableOpacity,
Dimensions,
Image,
FlatList,
AsyncStorage,
TextInput,
ActivityIndicator,
ScrollView,
ImageBackground
} from 'react-native';
import { ListItem, Left, Body, Right, Title } from "native-base";
import Dash from 'react-native-dash';
const window = Dimensions.get('window');
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' +
'Cmd+D or shake for dev menu',
android: 'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
var localizedString;
type Props = {};
export default class App extends Component<Props> {
constructor(Props){
super(Props);
}
render() {
return (
<View style={{flex:1, alignItems:'center', justifyContent:'center', backgroundColor:'grey'}}>
<View style={{width:'80%', backgroundColor:'white', height:'100%', justifyContent:'space-between', flexDirection:'row', alignItems:'center', position:'absolute'}}>
</View>
<View style={{width:'95%', height:'100%', flexDirection:'row', alignItems:'center'}}>
<View style={{height:50, width:50, backgroundColor:'grey', borderRadius:150}}>
</View>
<Dash style={{width:'75%', height:5}}/>
<View style={{height:50, width:50, backgroundColor:'grey', borderRadius:150,}}>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
});

react native scrollView Height always stays static and does not change

I build react native app and I use with scrollView for header with list of text horizontal.
The issue is that the height of the scroll view takes half size of the screen. Even after declared it as a style, it still stays as it is.
screen with the scrollView
<View style={Style.container} >
{this.props.ExtendedNavigationStore.HeaderTitle ? <BackHeader header={this.props.ExtendedNavigationStore.HeaderTitle} onPressBack={this.goBack} /> : <Header openDrawer={this.openDrawer} />}
<ScrollView contentContainerStyle={{flexGrow:1}} style={Style.scrollableView} horizontal showsHorizontalScrollIndicator={false}>
{this.renderScrollableHeader()}
</ScrollView>
<Routes /> /* stack with dashboard screen */
</View>
</Drawer>
)
}
styles
import {StyleSheet} from 'react-native'
import {calcSize} from '../../utils'
const Styles = StyleSheet.create({
container : {
flex:1,
backgroundColor:"#e9e7e8"
},
scrollableView:{
height: calcSize(40),
backgroundColor: '#000',
},
textCategory:{
fontSize: calcSize(25),
color:'#fff'
},
scrollableButton:{
flex:1,
margin:calcSize(30)
}
})
export default Styles
As you can see the black size is the scroll View,
I want it to be small.
In routes stack into dashboard screen, the style:
const Style = StyleSheet.create({
container: {
backgroundColor: '#9BC53D',
flex: 1,
justifyContent: 'space-around',
alignItems: 'center'
},
text: {
fontSize: 35,
color: 'white',
margin: 10,
backgroundColor: 'transparent'
},
button: {
width: 100,
height: 75,
margin: 20,
borderWidth: 2,
borderColor: "#ecebeb",
justifyContent: "center",
alignItems: "center",
borderRadius: 40
}
})
There is an existing limitation with ScrollView where height cannot be provided directly.
Wrap the ScrollView in another View and give height to that View.
Like,
render() {
return (
<View style={styles.container}>
<View style={{height: 80}} >
<ScrollView
horizontal
style={{ backgroundColor: 'blue'}}
>
<Text style={{padding: 24}} >title1</Text>
<Text style={{padding: 24}} >title2</Text>
<Text style={{padding: 24}} >title3</Text>
<Text style={{padding: 24}} >title4</Text>
<Text style={{padding: 24}} >title5</Text>
</ScrollView>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
},
});
snack sample: https://snack.expo.io/HkVDBhJoz
EXTRAS: Unlike height providing width to a ScrollView will work correctly
Use flexGrow:0 inside ScrollView style
<ScrollView style={{ flexGrow:0 }}>
That worked for me:
<ScrollView contentContainerStyle={{ flexGrow: 1 }}>
<View style={{ flexGrow: 1 }}>
...
</View>
</ScrollView>
Considering the issue that you are using fixed height for the header, and flex for the Routes maybe, the orientation for different devices would not scale well and would look weird.
Therefore you may consider switching it to the flex
Here is the example by adding flexGrow to the styles of the ScrollView since it accepts view props
<View style={{ flex: 1 }}>
<ScrollView style={{ flexGrow: 0.05, backgroundColor: 'red', paddingTop: 50 }} horizontal>
<View style={{ width: 100 }}><Text>Label</Text></View>
<View style={{ width: 100 }}><Text>Label</Text></View>
<View style={{ width: 100 }}><Text>Label</Text></View>
<View style={{ width: 100 }}><Text>Label</Text></View>
<View style={{ width: 100 }}><Text>Label</Text></View>
</ScrollView>
<View style={{ flex: 0.95, backgroundColor: 'green' }} />
</View>
and here's the link to the snack expo
Use maxHeight inside scrollview style
<ScrollView style={{ maxHeight: 40 }}>•••</ScrollView>
Setting minHeight: int, maxHeight: int to ScrollView should work where int is height in pixels.
example below
<ScrollView style={{ minHeight: 280, maxHeight: 260}}>