creating partially transparent background layout in react native - reactjs

I am using react native 0.49 and here is what I want to achieve:
so far this is what I did:
render() {
return (
<View style={{flex: 1}}>
<View style={{flexDirection: 'row', justifyContent: 'center', alignItems: 'center'}}>
<SearchHeader /> // search input component
<View style={{padding: 5}}>
<TouchableOpacity style={{padding: 5}} onPress={this.btnPressed}> // icon more
<Image source={require('../../assets/iconMore/iconMore.png')}/>
</TouchableOpacity>
</View>
</View>
<TabBarMain navigation={this.props.navigation}/> // tab navigator
{
this.state.showView
?
<View style={popoverStyle}>
<Text>View is showing</Text>
</View>
:
null
}
</View>
);
}
btnPressed = () => {
this.setState({
showView: !this.state.showView
});
}
I am trying to make that transparent background behind the view but Im failing to do so. Also, that triangle arrow pointer right underneath the icon (icon more). Im not sure if my code/component structure is correct, also the dark layout supposed to dismiss the view when pressed (pressing outside the view).
UPDATE
I added a view according to #sfratini answer.
showShadowLayer = () => {
if (this.state.showView){
return {
position: 'absolute',
top: 0, bottom: 0, left: 0, right: 0,
backgroundColor: "rgb(0,0,0)",
opacity: 0.5,
zIndex: 1000
}
} else {
return {
flex: 1
}
}
}
render() {
return (
<View style={{flex: 1}}>
<StatusBarView style={{flexDirection: 'row', justifyContent: 'center', alignItems: 'center'}}>
<SearchHeader />
<View style={{padding: 5}}>
<TouchableOpacity style={{padding: 5}} onPress={this.btnPressed}>
<Image
source={require('../../assets/iconMore/iconMore.png')}
/>
</TouchableOpacity>
</View>
</StatusBarView>
<TabBarMain navigation={this.props.navigation}/>
<View style={this.showShadowLayer()}/> // <-- added the partial transparent view here
{
this.state.showView
?
<View style={popoverStyle}>
<Text>View is showing</Text>
</View>
:
null
}
</View>
);
}
const popoverStyle = { // style for the custom popover
borderStyle: 'solid',
position: 'absolute',
backgroundColor: 'white',
borderColor: 'black',
elevation: 5,
height: SCREEN_HEIGHT,
width: SCREEN_WIDTH - 40,
alignSelf: 'flex-end',
marginTop: 60,
//for ios
shadowColor: 'black',
shadowOpacity: 0.3,
shadowOffset: { width: 3, height: 3 },
shadowRadius: 4,
}
But now I cannot interact with this view. How to dismiss it when pressed outside the view!

Just add a "modal view" behind your drawer. Something like this should do:
<View style={{
position: 'absolute',
top: 0, bottom: 0, left: 0, right: 0,
backgroundColor: rgba(255,255,255,0.5)
}}/>
The 4th value is the opacity
Edit:
Add it after here:
<View style={this.showShadowLayer()}/> // add here
{
this.state.showView
?
<View style={popoverStyle}>
<Text>View is showing</Text>
</View>
:
null
}
RN handles the zIndex by checking in which order you add the views in the render.
As you saw after a couple of testing, render can only have one parent so I am wrapping everything in another view.

Related

How to get 2 react native <view> in the same line?

I am trying to get an image viewer to work with my gallery, The library I use https://github.com/ascoders/react-native-image-viewer gives option to render a react element in the header. I am using this following element in the place of header
<View styles={modalStyles.header}>
<View onPress={props.closeModal} style={modalStyles.leftHeader}>
<Icon name="close" size={20} color="#fff"></Icon>
</View>
<View onPress={() => props.startDownload(props.getShowingImage())} style={modalStyles.rightHeader}>
<Icon name="download" size={30} color="#fff"></Icon>
</View>
</View>
and here is my styles
const modalStyles = StyleSheet.create({
header: {
flex: 1,
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'space-between',
backgroundColor: "red",
},
leftHeader: {
marginLeft: 20,
padding: 10,
alignSelf: 'flex-start',
backgroundColor: "red",
},
rightHeader: {
marginRight: 20,
padding: 10,
alignSelf: 'flex-end',
backgroundColor: "red",
},
})
and here is how the modal looks like
This is how i get content inline one at left and one at right. Also you have typo in your code .. rename style with styles
<View style={[{flexDirection:'row', alignItems:'center'}]}>
<View style={[{flex:1,flexDirection:'row'}]}>
//... left content
</View>
<View style={[{justifyContent:'space-evenly', marginVertical:10}]}>
// .... right content
</View>
</View>

Using flexbox with 2 components but both are not adjacent

Image here.
App
Code
I just want Advice component close to Weather component.
How can I get it ?
App.js
<View style={styles.columnLayout}>
<Weather currentCity="Phuket"/>
<Advice />
</View>
const styles = StyleSheet.create({
columnLayout : {
flex : 1,
flexDirection: 'column',
}
})
Weather and Advice component are not using flex.
WeatherData.js (export to Weather.js)
<View style={styles.viewStyle}>
<View style={styles.dataStyle}>
<Text style={styles.textStyle}>Temperature</Text>
<Text style={styles.textStyle}>{this.props.temp} °C</Text>
</View>
<View style={styles.dataStyle}>
<Image style={{ width: 80, height: 80 }} source={{ uri: `http://openweathermap.org/img/w/${this.props.icon}.png` }}></Image>
<Text style={styles.textStyle}>{this.props.description}</Text>
</View>
<View style={styles.dataStyle}>
<Text style={styles.textStyle}>Humidity</Text>
<Text style={styles.textStyle}>{this.props.humidity} %</Text>
</View>
</View>
const styles = StyleSheet.create({
dataStyle: {
backgroundColor: '#00b377',
flex: 1,
alignItems: "center",
justifyContent: "center"
},
viewStyle: {
height: 150,
flexDirection: "row",
justifyContent: "center"
},
textStyle: {
color: 'white',
textShadowColor: 'rgba(0, 0, 0, 0.75)',
textShadowOffset: {width: -1, height: 1},
textShadowRadius: 10
}
})
another one Component using the same style.
I already add code.
In react-native documents flex : 1 and flexDirection : column all View are adjacent
but mine is not.

How to force react native content to ignore keyboard?

I have tried using both KeyboardAvoidingView and ScrollView to prevent my content from being squished (pushed up) when the keyboard is present. I have tried using padding, height, and position for my behavior but nothing is working. Can someone please tell me how I can force my content to ignore the keyboard and not get pushed up??
return (
<View style={{height: '100%', backgroundColor: '#D6D6D6', position: 'relative'}}>
<View style={styles.wrapper}>
<View style={{height:'100%', borderRadius: 7}}>
<View style={styles.container}>
<ScrollView style={{borderRadius: 7}}
horizontal
showsHorizontalScrollIndicator={false}
scrollEventThrottle={10}
pagingEnabled
onScroll={
Animated.event(
[{nativeEvent: {contentOffset: {x: this.animVal}}}]
)
}
>
{imageArray}
</ScrollView>
<View style={styles.listViewContainer}>
<TouchableOpacity style={styles.listView} onPress={() => Actions.pop()}>
<View style={{flex: 1, flexBasis: 22}}>{listIcon}</View>
<View style={{flex: 2, flexBasis: 57}}><Text style={{color: '#fff'}}>List View</Text></View>
</TouchableOpacity>
</View>
<View style={styles.circleContainer}>
{circleArray}
</View>
</View>
<View style={styles.productsSection}>
<Text style={styles.title}>{prodDesc}</Text>
<Text style={styles.desc}>{prodBrand}</Text>
<Text style={styles.desc}>Item: {prodId || ''}</Text>
<Text style={[styles.desc, {marginBottom: 15}]}>Category: {prodCat}</Text>
<Table borderStyle={{borderWidth: 0}}>
<Rows data={rows}/>
</Table>
</View>
<View style={styles.bodyFooter}>
<QuantityCounter style={{width: '100%', display: 'block', marginRight: 20}} data={{productId: prodId}} />
</View>
</View>
</View>
<View style={styles.footer}>
<View style={styles.cartContainer}>
{cartIcon}
<Text style={{color: '#3A3A3A', fontSize: 14}}>18 items</Text>
</View>
<TouchableOpacity style={styles.viewCartButtonContainer} onPress={() => this.cartRedirect() }>
<Text style={{color: '#fff', fontSize: 15, marginTop: '5%'}}>View Cart</Text>
</TouchableOpacity>
</View>
<Header/>
</View >
);
here are my main styles for this:
var styles = StyleSheet.create({
wrapper: {
backgroundColor: '#E6E6E6',
marginVertical: 15,
marginHorizontal: 10,
borderRadius: 7,
elevation: 3,
maxHeight: '80%',
flexShrink: 1,
zIndex: 0,
marginTop: 75
},
container: {
flex: 1.7,
justifyContent: 'space-between',
alignItems: 'center',
height: '50%',
borderRadius: 7
},
footer: {
justifyContent:'space-between',
alignItems: 'center',
height: '10%',
backgroundColor: '#E6E6E6',
paddingVertical: 15,
paddingHorizontal: 17,
flexDirection: 'row',
borderStyle: 'solid',
borderTopColor: '#8E8E93',
borderTopWidth: 1
},
cartContainer: {
flexDirection: 'row',
width: '35%'
},
viewCartButtonContainer: {
backgroundColor: '#356FAF',
height: '90%',
width: '45%',
padding: 20,
alignItems: 'center',
justifyContent: 'center',
borderRadius: 3
},
bodyFooter: {
backgroundColor: '#F6F6F6',
justifyContent: 'center',
flex: 0.45,
borderTopColor: '#D6D6D6',
borderTopWidth: 1,
borderStyle: 'solid',
borderBottomRightRadius: 7,
borderBottomLeftRadius: 7
},
circleContainer: {
position: 'absolute',
zIndex: 2,
bottom: 10,
left: 10,
flexDirection: 'row',
},
listViewContainer: {
position: 'absolute',
zIndex: 10,
top: 0,
right: 0,
justifyContent: 'center'
},
listView: {
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
borderTopRightRadius: 3,
backgroundColor: '#000',
paddingVertical: 5,
paddingHorizontal: 10
},
What it looks like without the keyboard:
What it looks like with the keyboard:
Handling View behavior when toggling a keyboard can be a tricky thing in React Native. There are multiple possible solutions to questions like this, but in this case the solution was this:
Instead of using style={{height:'100%'}} on your components that get pushed up, try using Dimensions:
import {Dimensions} from 'react-native';
const { height } = Dimensions.get('window');
and specify style={{ height }} in the right components.
Another thing that might be worth a try if someone else stumbles on this question:
React Native for Android has some default settings defined in the Android manifest. If you are not using Expo (or CRNA), you can change the behavior of the keyboard in AndroidManifest.xml by changing the windowSoftInputMode rule.
Try changing android:windowSoftInputMode="adjustResize" to android:windowSoftInputMode="adjustPan" or to android:windowSoftInputMode="adjustNothing". You can try to play around with some other options (See here) if this doesn't give you the desired effect.
You should use try behavior as "none" for android and if you don't want to getting small, you can set android:windowSoftInputMode="adjustPan" in manifest file.
and if still face any error checkout react-native-keyboard-aware-scrollview
here on npm.
I went through a similar problem and solved it by changing
android:windowSoftInputMode="adjustPan”
In android manifest.
Also clean and rebuild. This might work
remove the position absolute it will works just fine trust me
for some cases if you want keep defualt manifest
you can move your elements inside a Scrollview it may help.
issue solved for me in this way

How to horizontally center a component while there are other components in the same row?

I'm trying to mimic a stack navigation header like below:
where the title is perfectly centered while there's a return button in the same row on the left.
I have tried to use justifyContent: 'space-between' with an empty View on the right side but the title will be off center to the right a bit. How should I approach this?
my renderHeader function:
_renderHeader = () => {
return (
<View style={styles.headerAbsolute}>
<View style={styles.bar}>
<ButtonBack
text={"Resumes"}
onPress={() => this._onNavBackHandler()}
/>
{true ? (
<Text style={styles.headingFixed}>{this.state.title}</Text>
) : ( null )}
<View/>
</View>
</View>
)
}
These are the styles:
bar: {
height: 55,
flexDirection: "row",
alignItems: 'flex-end',
justifyContent: 'space-between',
paddingHorizontal: 10,
paddingBottom: 5,
},
headingFixed: {
fontSize: TITLE_TERTIARY,
fontWeight: 'bold',
color: COLOR_DARK_SECONDARY,
backgroundColor: 'transparent',
},
I will make use of Flex here.
Please note that i used borderWidth in the below example only for reference to show how it looks.
I will have flexDirection as row and i will give a flex: 1 to all the views inside
App.js
import React, { Component } from 'react';
import {
View,
Text
}
from 'react-native';
class App extends Component{
render(){
return(
<View>
<View style={styles.navBar}>
<View style={styles.itemStyle}>
<Text style={{fontSize: 18, color: 'blue'}}>Resumes</Text>
</View>
<View style={styles.itemStyle}>
<Text style={{fontSize: 20, color: 'blue'}}>Settings</Text>
</View>
<View style={styles.itemStyle}></View>
</View>
</View>
)
}
}
const styles = {
navBar: {
marginTop: 42,
height: 36,
flexDirection: 'row',
alignItems: 'center'
},
itemStyle: {
flex: 1,
height: 36,
alignItems: 'center',
justifyContent: 'center',
borderWidth: 1
}
}
export default App;
You can play around in the inside views. Move the buttonBack inside one of the child views.
I can think of two ways to accomplish this, but neither seems quite ideal.
The first way is using a transparent element with fixed width and justifyContent: 'space-between', as you indicated:
<View style={{flexDirection: 'row', justifyContent: 'space-between'}}>
<Text style={{width: 80}}>left</Text>
<Text>center</Text>
<Text style={{width: 80, color: 'transparent'}}>right</Text>
</View>
Setting a fixed width to the elements on each side will cause the middle element to center horizontally.
An alternative approach is using absolute styling. Using percentages with left and right requires React Native >= 0.42.
<View>
<Text>Left</Text>
<Text style={{position: 'absolute', width: 50, left: '50%', marginLeft: -25}}>Center</Text>
</View>

Center a text in the screen with react native

I am trying to center a text vertically and horizontally on the screen. This is my code
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.header}>
<Text> Header </Text>
</View>
<Text style={styles.text}> some text in the middle center of the screen </Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
backgroundColor:'white',
alignItems:'center',
justifyContent:'center'
},
header: {
backgroundColor: 'green',
alignSelf: 'stretch',
alignItems: 'center',
heigth: 80 // this dose not change the header height
},
text:{
//flex: 1,
justifyContent:'center',
}
});
If I add the flex:1 to text, the header will also be centered which is not expected. I don't know if it's related but the I am also not able to modify the header view height. How can I solve this? The problems can be reproduced on this snack.
<div data-snack-id="S1urACbJM" data-snack-platform="ios" data-snack-preview="true" data-snack-theme="light" style="overflow:hidden;background:#fafafa;border:1px solid rgba(0,0,0,.16);border-radius:4px;height:505px;width:100%"></div>
<script async src="https://snack.expo.io/embed.js"></script>
This is one way to center text in the screen:
<View style={{
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'blue'
}}>
<Text style={{backgroundColor: 'red'}}>
Your Text
</Text>
</View>
And you can also try with position:'absolute':
<View style={{
backgroundColor: 'blue',
position: 'absolute',
top: 0, left: 0,
right: 0, bottom: 0,
justifyContent: 'center',
alignItems: 'center'}}>
<Text style={{backgroundColor: 'red'}}> Your Text </Text>
</View>
I suggest setting the header as position:'absolute' , and use flex:1 and justify-content:'center' on the container.
Check the updated style
const styles = StyleSheet.create({
container: {
backgroundColor:'white',
alignItems:'center',
justifyContent:'center',
flex:1,
paddingTop:20 //this amount should be equal to the header height so that any items displayed inside the container will start after the absolute positioned header
},
header: {
backgroundColor: 'green',
alignSelf: 'center',
justifyContent:"flex-start",
alignItems: 'center',
position:"absolute",
top:0
}
});
Regarding the height you just have a typo in the word height

Resources