What's my mistake when using ternany operator with isTextInputFocused property in react-native? - react-native

I have a textInput. When i click on the textInput, i want "Search Icon" to be passive, but when i dont click on the textInput, i want "Search Icon" to be active,
Its strangely not doing what I want. As I look at my code, it looks correct, but i must have something wrong because it doesnt work as i wanted.(not giving an error tho)
App.js
import React from 'react';
import { StyleSheet, Text, View, TextInput, isTextInputFocused } from 'react-native';
import Search from "./src/icons/search.svg";
import {Loupe} from "./src/icons/loupe.png";
const App = () => {
return (
<View style={styles.container}>
<View style={styles.firstPart}>
<Text style={styles.date}>Saturday, Feb 21</Text>
<View style={styles.package}>
<Text style={styles.packageText}>Your Package</Text>
<View style={styles.plusIcon}>
<Text style={styles.textPlus}>+</Text>
</View>
</View>
{isTextInputFocused ? (<Search height={25} width={20} fill={"#00FF00"} style={{ position: "absolute", top: 90, left: 30, zIndex: 0 }} />):
(<Search height={25} width={20} fill={"#A9A9A9"} style={{ position: "absolute", top: 90, left: 25, zIndex: 20 }}/>)}
<TextInput style={styles.input} placeholder="Search" placeholderTextColor="gray"></TextInput>
</View>
{/* { !isTextInputFocused && (<View>
<Search height={30} width={30} fill={"#00FF00"} />
</View>)} */}
</View>
);
};
const styles = StyleSheet.create({
container:{
margin:10,
},
firstPart:{
backgroundColor: "mediumblue",
borderTopRightRadius:40,
borderTopLeftRadius:40,
padding:20,
},
date:{
fontSize:15,
color:"white",
},
package:{
flexDirection: "row",
justifyContent: "space-between",
},
packageText: {
fontSize:25,
color:"white",
flexDirection: "column",
},
plusIcon: {
height:25,
width:25,
borderRadius:25,
backgroundColor:"blue",
marginTop:5,
},
textPlus: {
fontSize:25,
fontWeight:"bold",
color:"white",
marginLeft:5,
},
input: {
height:40,
borderWidth:.5,
borderRadius:10,
marginVertical:10,
backgroundColor:"darkslateblue",
padding:5,
}
});
export default App;

You can use the TextInput component's onFocus, onBlur props to focus information on an input. Sample code is available below.
const [text, setText] = useState("")
const [inputFocus, setFocus] = useState(false)
return (
<View style={styles.container}>
<Text>
{inputFocus ? "Focused" : "Not Focused"}
</Text>
<TextInput
value={text}
onChangeText={setText}
onFocus={() => setFocus(true)}
onBlur={() => setFocus(false)}
style={{
width: "100%",
height: 44,
backgroundColor: "gray"
}}
/>
</View>
)

Related

KeyboardAvoidingView doesn't work properly on my app

I searched a lot about this issue and I don't know what to do now so I decided to ask here and any help would be appreciated. in my register screen I have some input that user must fill and for the last one I need to avoid keyboard overlay. so I used KeyboardAvoidingView component to solve this but as you can see in the screenshot the device keyboard still overlay my input (birth date).
here is my code for this screen :
import React, { useState } from 'react';
import { View, Text, KeyboardAvoidingView, Image, Dimensions, PixelRatio, StyleSheet } from 'react-native';
import { widthPercentageToDP as wp, heightPercentageToDP as hp } from 'react-native-responsive-screen';
import { COLORS } from '../Constants/COLORS';
import PrimaryButton from '../Components/PrimaryButton';
import PrimaryInput from '../Components/PrimaryInput';
import CheckBox from '../Components/CheckBox';
const Register = (props) => {
const [lisenceTerm, setLisenceTerm] = useState(true);
const [privacyPolicy, setPrivacyPolicy] = useState(true);
return (
<View style={styles.screen}>
<View style={styles.imageContainer}>
<Image style={styles.image} source={require('../assets/Img/office_5.jpg')} />
</View>
<View style={styles.loginContainer}>
<View style={styles.headerContainer}>
<Text style={styles.headerText}>Join us</Text>
</View>
<KeyboardAvoidingView style={styles.inputContainer}>
<PrimaryInput placeholder='Name Surname' />
<PrimaryInput placeholder='Your Email' />
<PrimaryInput placeholder='Phone Number (05***)' />
<PrimaryInput placeholder='Birth Date' />
</KeyboardAvoidingView>
<View style={styles.checkBoxContainer}>
<CheckBox text='Kvkk sözleşmesini okudum, kabul ediyorum' active={lisenceTerm} onPress={() => setLisenceTerm(!lisenceTerm)} />
<CheckBox text='Kullanıcı sözleşmesini okudum, kabul ediyorum' active={privacyPolicy} onPress={() => setPrivacyPolicy(!privacyPolicy)} />
</View>
<View style={styles.buttonsContainer}>
<PrimaryButton
buttonStyle={styles.button}
textStyle={styles.buttonText}
title='Register' />
</View>
</View>
</View>
)
}
const styles = StyleSheet.create({
screen: {
flex: 1,
},
imageContainer: {
width: '100%',
height: '34%'
},
image: {
width: '100%',
height: '100%'
},
loginContainer: {
backgroundColor: 'white',
width: '100%',
height: '71%',
position: 'absolute',
zIndex: 1,
bottom: 0,
borderTopLeftRadius: 40,
borderTopRightRadius: 40,
alignItems: 'center'
},
buttonsContainer: {
width: '100%',
justifyContent: 'center',
alignItems: 'center'
},
button: {
justifyContent: 'center',
alignItems: 'center',
width: '75%',
paddingVertical: '3%',
marginTop: hp(1.2),
borderRadius: hp(3.5),
backgroundColor: COLORS.royalBlue
},
buttonText: {
fontSize: hp(3.2),
fontFamily: 'BalooPaaji2ExtraBold',
color: 'white'
},
arrow: {
right: 20
},
inputContainer: {
width: '100%',
justifyContent: 'center',
alignItems: 'center',
marginBottom: hp(1),
},
headerContainer: {
marginTop: hp(3),
marginBottom: hp(2),
width: '75%',
},
headerText: {
fontSize: hp(3.5),
fontFamily: 'BalooPaaji2Bold',
color: COLORS.royalBlue
},
checkBoxContainer: {
width: '70%',
justifyContent: 'flex-start',
marginBottom: hp(1)
}
})
export default Register;
and here below is the result. btw I used behavior and keyboardVerticalOffset props to control the way this component behave but still same problem.
Your KeyboardAvoidingView component must be on top of render tree
In order to scroll onto your Join us view, you must set a ScrollView in your KeyboardAvoidingView and those component must be on top of renderer.
Try this (based on my iOS / android setup) :
import { KeyboardAvoidingView, ScrollView } from 'react-native';
const Register = (props) => {
const [lisenceTerm, setLisenceTerm] = useState(true);
const [privacyPolicy, setPrivacyPolicy] = useState(true);
return (
<KeyboardAvoidingView
style={{ flex: 1 }}
behavior={(Platform.OS === 'ios') ? 'padding' : null}
enabled
keyboardVerticalOffset={Platform.select({ ios: 80, android: 500 })}>
<ScrollView>
<View style={styles.screen}>
<View style={styles.imageContainer}>
<Image style={styles.image} source={require('../assets/Img/office_5.jpg')} />
</View>
<View style={styles.loginContainer}>
<View style={styles.headerContainer}>
<Text style={styles.headerText}>Join us</Text>
</View>
<View style={styles.inputContainer}>
<PrimaryInput placeholder='Name Surname' />
<PrimaryInput placeholder='Your Email' />
<PrimaryInput placeholder='Phone Number (05***)' />
<PrimaryInput placeholder='Birth Date' />
</View>
<View style={styles.checkBoxContainer}>
<CheckBox text='Kvkk sözleşmesini okudum, kabul ediyorum' active={lisenceTerm} onPress={() => setLisenceTerm(!lisenceTerm)} />
<CheckBox text='Kullanıcı sözleşmesini okudum, kabul ediyorum' active={privacyPolicy} onPress={() => setPrivacyPolicy(!privacyPolicy)} />
</View>
<View style={styles.buttonsContainer}>
<PrimaryButton
buttonStyle={styles.button}
textStyle={styles.buttonText}
title='Register' />
</View>
</View>
</View>
</ScrollView>
</KeyboardAvoidingView>
)
}
you need to install react-native-keyboard-aware-scroll-view by
yarn add react-native-keyboard-aware-scroll-view
and you need to wrap KeyboardAwareScrollView instead of KeyboardAvoidingView
like
import {KeyboardAwareScrollView} from 'react-native-keyboard-aware-scroll-view';
<KeyboardAwareScrollView>
<View>
...
</View
</KeyboardAwareScrollView>
import { useHeaderHeight } from "#react-navigation/elements"
import {
Keyboard,
Platform,
TouchableWithoutFeedback,
View,
KeyboardAvoidingView
} from "react-native"
const Chat = () => {
// This is the crucial variable we will place it in
// KeyboardAvoidingView -> keyboardVerticalOffset
const headerHeight = useHeaderHeight()
return (
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<KeyboardAvoidingView
style={{ position: "absolute", bottom: 0, left: 0, right: 0 }}
behavior={Platform.OS === "ios" ? "padding" : "height"}
// If you want the input not to stick exactly to the keyboard
// add a const here for example headerHeight + 20
keyboardVerticalOffset={headerHeight}
>
<View style={{ flex: 1, justifyContent: "flex-end" }}>
<InputWrapper>
<RawInput />
</InputWrapper>
</View>
</KeyboardAvoidingView>
</TouchableWithoutFeedback>
)
}
Also take a look at this article it might be helpful: https://medium.com/#nickyang0501/keyboardavoidingview-not-working-properly-c413c0a200d4

React Native animation transition opacity from 0 to 1 does not work

I am using expo with Android Virtual Device .I am trying to mimic this animation behavior from this example, https://codepen.io/borntofrappe/pen/qwqPwq,
Here is what it looks like right now:
I use flag array to represent which button is clicked, if the certain button is clicked, I will call handleSpeedBtn function to change its corresponding flag, when the flag is true, it will provide buttonTextLayer style to this button. Right now,I am having trouble displaying the buttonTextLayer style in animation style.It applies buttonTextLayer style to the button without any animation, besides, I do not have any errors when I run the code. Just change the opacity from 0 to 1 probably would not make my app looks like the example I found on codepen, but I just want to see the animation at least. Thanks for help!
import React, { useState } from "react";
import {
Text,
StyleSheet,
View,
TouchableOpacity,
Animated,
} from "react-native";
import { MaterialCommunityIcons } from "#expo/vector-icons";
const SortTitle = ({ callBack, title }) => {
const [flag, setFlag] = useState([false, true, false]);
const opacity = useState(new Animated.Value(0))[0];
const handleSpeedBtn = (index) => {
let ary = [false, false, false];
ary[index] = ary[index] == true ? false : true;
setFlag(ary);
Animated.timing(opacity, {
toValue: 1,
duration: 2000,
useNativeDriver: true,
}).start();
};
return (
<View>
<View style={styles.titleContainer}>
<Text style={styles.titleText}>{title}</Text>
</View>
<View style={styles.btnGroupContainer}>
<View style={styles.btnContainer}>
<TouchableOpacity onPress={() => handleSpeedBtn(0)}>
<Animated.View
style={[flag[0] ? styles.buttonTextLayer : {}, opacity]}>
<MaterialCommunityIcons
name="speedometer-slow"
size={46}
color="white"
style={[flag[0] ? styles.icon : {}]}
/>
<Text style={[styles.buttonText, flag[0] ? {} : styles.hidden]}>
Slow
</Text>
</Animated.View>
</TouchableOpacity>
</View>
<View style={styles.btnContainer}>
<TouchableOpacity onPress={() => handleSpeedBtn(1)}>
<Animated.View
style={[flag[1] ? styles.buttonTextLayer : {}, opacity]}>
<MaterialCommunityIcons
name="speedometer-medium"
size={46}
color="white"
style={[flag[1] ? styles.icon : {}]}
/>
<Text style={[styles.buttonText, flag[1] ? {} : styles.hidden]}>
Medium
</Text>
</Animated.View>
</TouchableOpacity>
</View>
<View style={styles.btnContainer}>
<TouchableOpacity onPress={() => handleSpeedBtn(2)}>
<Animated.View
style={[flag[2] ? styles.buttonTextLayer : {}, opacity]}>
<MaterialCommunityIcons
name="speedometer"
size={46}
color="white"
style={[flag[2] ? styles.icon : {}]}
/>
<Text style={[styles.buttonText, flag[2] ? {} : styles.hidden]}>
Fast
</Text>
</Animated.View>
</TouchableOpacity>
</View>
</View>
</View>
);
};
const styles = StyleSheet.create({
titleText: {
fontSize: 25,
color: "#000000",
fontWeight: "bold",
marginLeft: 10,
marginVertical: 5,
},
titleContainer: {
borderBottomColor: "#e3e3e3",
borderBottomWidth: 2,
},
icon: {
marginLeft: 10,
},
textDiv: {},
btnGroupContainer: {
height: 80,
flex: 1,
flexDirection: "row",
alignItems: "center",
justifyContent: "space-around",
backgroundColor: "#889bf2",
},
btnContainer: {
paddingVertical: 10,
justifyContent: "space-around",
},
buttonText: {
textAlign: "center",
color: "#ffffff",
fontSize: 15,
alignSelf: "center",
flex: 1,
},
buttonTextLayer: {
width: 130,
backgroundColor: "#5c69a4",
borderRadius: 50,
flexDirection: "row",
},
hidden: {
width: 0,
height: 0,
},
});
export default SortTitle;
Working example
Link: ----> https://snack.expo.io/#msbot01/aa6537
import React, { Component } from 'react';
import { Text, View, StyleSheet, Image } from 'react-native';
import Constants from 'expo-constants';
import Icon from 'react-native-vector-icons/FontAwesome';
// You can import from local files
import AssetExample from './components/AssetExample';
import AwesomeIcon from 'react-native-vector-icons/FontAwesome';
// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';
import Ripple from 'react-native-material-ripple';
export default class App extends Component<Props> {
constructor(props) {
super(props);
this.state = {
tabSelected:1
};
}
onClick(e){
this.setState({
tabSelected:e
})
}
render() {
return (
<View style={{ flex: 1 }}>
<View style={{position:"absolute", bottom:0, height:50,backgroundColor:'white', width:'100%', alignItems:'center', flexDirection:'row', justifyContent:'space-around'}}>
<Ripple style={{height:'100%', width:100, justifyContent:'center', alignItems:'center'}} onPress={()=>{this.onClick(0)}}>
{this.state.tabSelected==0?
<View style={{flexDirection:'row', padding:10, borderRadius:20, alignItems:'center', backgroundColor:'#bffab6', width:'80%', justifyContent:"center", height:'70%'}}>
<Icon name="user" size={15} color="#1fdb02"/>
<Text style={{fontSize:15, paddingLeft:5, color:'#1fdb02'}}>user</Text>
</View>
:
<Icon name="user" size={15} color="#1fdb02"/>
}
</Ripple>
<Ripple style={{height:'100%', width:100, justifyContent:'center', alignItems:'center'}} onPress={()=>{this.onClick(1)}}>
{this.state.tabSelected==1?
<View style={{flexDirection:'row', padding:10, borderRadius:20, alignItems:'center', backgroundColor:'#fce29a', width:'80%', justifyContent:"center", height:'70%'}}>
<Icon name="search" size={15} color="#e6ac02"/>
<Text style={{fontSize:15, paddingLeft:5, color:'#e6ac02'}}>search</Text>
</View>
:
<Icon name="search" size={15} color="#e6ac02"/>
}
</Ripple>
<Ripple style={{height:'100%', width:100, justifyContent:'center', alignItems:'center'}} onPress={()=>{this.onClick(2)}}>
{this.state.tabSelected==2?
<View style={{flexDirection:'row', padding:10, borderRadius:20, alignItems:'center', backgroundColor:'#b6e9fa', width:'80%', justifyContent:"center", height:'70%'}}>
<Icon name="heart" size={15} color="#048dba"/>
<Text style={{fontSize:15, paddingLeft:5, color:'#048dba'}}>like</Text>
</View>
:
<Icon name="heart" size={15} color="#048dba"/>
}
</Ripple>
<Ripple style={{height:'100%', width:100, justifyContent:'center', alignItems:'center'}} onPress={()=>{this.onClick(3)}}>
{this.state.tabSelected==3?
<View style={{flexDirection:'row',padding:10, borderRadius:20, alignItems:'center', backgroundColor:'#f3b8ff', width:'80%', justifyContent:"center", height:'70%'}}>
<Icon name="share" size={15} color="#9c04ba"/>
<Text style={{fontSize:15, paddingLeft:5, color:'#9c04ba'}}>share</Text>
</View>
:
<Icon name="share" size={15} color="#9c04ba"/>
}
</Ripple>
</View>
</View>
);
}
}

React Native - Image bigger than parent

Like in tinder, i have a screen with 3 Image Pickers. When I pick an image from the gallery, I want it to persist inside the TouchableOpacity container. However, the image overlaps the container.
I have been been trying various things mentioned here but the image still overlaps like in the image below:
What is the problem?
/* eslint-disable react-native/no-inline-styles */
import React, { useState } from 'react'
import {
Text,
View,
Image,
Button,
TouchableOpacity,
StyleSheet,
Dimensions,
} from 'react-native'
import ImagePicker from 'react-native-image-crop-picker'
import { FontAwesomeIcon } from '#fortawesome/react-native-fontawesome'
import { faPlusCircle } from '#fortawesome/free-solid-svg-icons'
import Colors from '../../../constants/Colors'
const { width: WIDTH } = Dimensions.get('window')
const ProfileEdit = () => {
const [photo, setPhoto] = useState(null)
const handleChoosePhoto = () => {
ImagePicker.openPicker({
width: 300,
height: 400,
cropping: true,
}).then((image) => {
setPhoto({
uri: image.path,
width: image.width,
height: image.height,
mime: image.mime,
})
console.log(image)
})
}
return (
<View style={styles.main}>
<View style={styles.button_row}>
<View style={styles.buttonWrapper}>
<TouchableOpacity
onPress={() => handleChoosePhoto()}
style={styles.button}>
{photo ? (
<Image source={photo} style={styles.photo} />
) : (
<FontAwesomeIcon
icon={faPlusCircle}
color={Colors.defaultColor}
size={22}
style={styles.icon}
/>
)}
</TouchableOpacity>
</View>
<View style={styles.buttonWrapper}>
<TouchableOpacity
onPress={() => handleChoosePhoto()}
style={styles.button}>
<FontAwesomeIcon
icon={faPlusCircle}
color={Colors.defaultColor}
size={22}
style={styles.icon}
/>
</TouchableOpacity>
</View>
<View style={styles.buttonWrapper}>
<TouchableOpacity
onPress={() => handleChoosePhoto()}
style={styles.button}>
<FontAwesomeIcon
icon={faPlusCircle}
color={Colors.defaultColor}
size={22}
style={styles.icon}
/>
</TouchableOpacity>
</View>
</View>
</View>
)
}
const styles = StyleSheet.create({
main: {
flex: 1,
},
icon: {},
button: {
height: `${100}%`,
backgroundColor: Colors.defaultWhite,
padding: 2,
alignItems: 'center',
justifyContent: 'center',
borderRadius: 3,
},
buttonWrapper: {
width: WIDTH / 3,
padding: 10,
},
button_row: {
flex: 0.3,
flexWrap: 'wrap',
flexDirection: 'row',
},
photo: {
width: WIDTH / 2,
height: WIDTH / 2,
// borderRadius: 3,
resizeMode: 'contain',
},
})
export default ProfileEdit
correct answer as described here
"However because images will try to set the width and height based on the actual size of the image, you need to override these style properties"
<Image
style={{
flex: 1,
alignSelf: 'stretch',
width: undefined,
height: undefined
}}
source={require('../../assets/images/onboarding-how-it-works.png')}
/>
Instead of calculating image width try this:
<TouchableOpacity
onPress={() => handleChoosePhoto()}
style={styles.button}>
{photo ? (
<View style={styles.photoContainer}>
<Image source={photo} style={styles.photo} />
</View>
) : (
<FontAwesomeIcon
icon={faPlusCircle}
color={Colors.defaultColor}
size={22}
style={styles.icon}
/>
)}
</TouchableOpacity>
const styles = StyleSheet.create({
photoContainer: {
// flex: 1
},
photo: {
height: WIDTH / 2,
width: '100%',
resizeMode: 'contain'
}
});
I prefer ImageBackground instead. Below is the code to achieve you the desired View:
<ImageBackground
imageStyle={styles.image}
style={styles.imageContainer}
source={{uri: this.state.imageUri}}>
<TouchableOpacity onPress={this._pickImage} style={{
height: WIDTH/2,
width: WIDTH/2,
justifyContent: "center",
alignItems: "center"}}>
<Entypo name="camera" color={Colors.GREY} size={35}/>
</TouchableOpacity>
</ImageBackground>

Touchable Highlight not getting blurry when modal opens in react native

I have a modal which blurs the background view when it opens up.But the background has a Touchable Highlight which is not getting blurred.My code for the background View looks like this:
import React, {Component} from 'react';
import {View, Text, Image, StyleSheet, TouchableHighlight} from 'react-native';
import ModalContent from './modalView';
export default class Customquotesconfirmation extends Component {
constructor(props) {
super(props);
this.state={
modalVisible: false,
phoneNumber: '9018938260',
clearTextInputValue: false
}
this.hideModal=this.hideModal.bind(this);
this.showModal=this.showModal.bind(this);
}
showModal(modalState) {
this.setState({ modalVisible: modalState});
}
hideModal(modalState) {
this.setState({modalVisible: modalState});
}
render(){
return(
<View style={(this.state.modalVisible == true) ? styles.modalStyle : styles.modalCloseStyle }>
<Text style={{textAlign:'center', color: '#097543', fontSize: 28, fontFamily: 'SourceSansPro-Black', marginTop:40, marginLeft:40, marginRight:40}}>Thank you for your interest!</Text>
<Text style={{textAlign: 'center', color: '#666666',fontSize: 18, fontFamily: 'SourceSansPro-Black', marginLeft: 40, marginRight:40, marginTop:20 }}>Tap "Submit" and a lawn care professional will contact you with information about tailored service.</Text>
<Image style={{width: 150, height:150, justifyContent:'center', alignItems:'center', marginLeft:150, marginTop: 40}}
source={require('../../../assets/img/mobile_image.png')}></Image>
<Text style={{marginLeft: 20, marginRight:20, marginTop:20, fontSize:8, color:'#666666'}}>By tapping "Submit" I provide my personal information including phone number and consent to: (1) receive autodialed calls, texts, and prerecorded messages from Trygreen regarding my account, including current and possible future services, customer service and billing; and (2) Trugreen's Privacy Policy and Terms and Conditions(including this arbitration provision).I understand that my consent is not required to purchase TruGreen services and that I may revoke consent for automated communications at any time.</Text>
<TouchableHighlight
onPress={() => {this.setState({ modalVisible: true})}}
underlayColor="#FFFFFF">
<View style={styles.submitButtonStyle}>
<Text style={{color: '#FFFFFF', fontSize:25, textAlign: 'center',marginTop:10, fontStyle:'SourceSansPro-Bold'}}>Submit</Text>
</View>
</TouchableHighlight>
{/* Modal Content */}
{
this.state.modalVisible ?
<ModalContent
modalVisible={this.state.modalVisible}
showModal={this.showModal}
hideModal={this.hideModal}
phoneNumber={this.state.phoneNumber}
/> : null
}
</View>
);
}
}
const styles = StyleSheet.create({
modalStyle: {
backgroundColor: 'rgba(0,0,0,0.5)', height:800
},
modalCloseStyle: {
backgroundColor:'rgba(0,0,0,0)'
},
modalViewStyle:{
flex: 1, flexDirection:'column' , marginTop:200, marginBottom:200, marginLeft:80, marginRight:80, height:220, backgroundColor:'#FFFFFF'
},
submitButtonStyle: {
width:200, height:50, backgroundColor: '#ff9933', marginLeft:120, borderRadius:2, marginTop:20
},
blurButtonOnModalOpen: {
width:200, height:50, marginLeft:120, borderRadius:2, marginTop:20, backgroundColor: 'rgba(0,0,0,0.5)',
}
})
And my modal view code is:
import React, { Component } from 'react';
import {View, Text, TouchableHighlight, Modal, TextInput, StyleSheet} from 'react-native';
export default class ModalPopup extends Component {
constructor(props) {
super(props);
this.state={
modalVisible: this.props.modalVisible,
}
}
render() {
return(
<View style={{flex:1, alignItems:'center', justifyContent:'center'}}>
<Modal
animationType='slide'
visible={this.state.modalVisible}
transparent={true}
onRequestClose={() => {
this.props.hideModal(false);
}
}
>
<View style={ (this.state.modalVisible ? styles.modalViewStyle : null )}>
<View style={{ backgroundColor: '#32CD32', height: 100}}>
<TouchableHighlight
onPress={() => {this.setState({ clearTextInputValue: true})}}
underlayColor="##32CD32">
<Text style={{ marginLeft:10, marginTop:10, color:'#FFFFFF', fontSize: 20}}> x </Text>
</TouchableHighlight>
<Text style={{ marginLeft:60, color:'#FFFFFF', fontSize: 15, marginTop: -25}}> Contact Info </Text>
<Text style={{ marginLeft:40, marginTop:10, color:'#FFFFFF', fontSize: 20, fontStyle: 'SourceSansPro-Bold' }}> Phone Number </Text>
</View>
<View style={{backgroundColor:'#FFFFFF'}}>
<Text style={{ marginLeft:20, marginTop:10, color:'grey', fontSize: 20, fontStyle: 'SourceSansPro-Bold', marginRight:20 }}> Please confirm best contact number. </Text>
<TextInput
style={{fontSize:20, height:40, justifyContent:'center', borderTopWidth: 2, borderLeftWidth: 2, borderColor:'grey', width:200, height: 50, marginLeft: 20, marginTop: 20}}
keyboardType= 'numeric'
defaultValue={this.state.clearTextInputValue ? '' : this.props.phoneNumber}
maxLength={10}></TextInput>
<TouchableHighlight
onPress={() => {this.props.hideModal(false)}}
underlayColor="#FFFFFF">
<View style={{ width:200, height:50, backgroundColor: '#ff9933', marginLeft:20, borderRadius:2, marginTop:20}}>
<Text style={{color:'#FFFFFF', fontSize:25, textAlign: 'center',marginTop:10, fontStyle:'SourceSansPro-Bold'}}>Submit</Text>
</View>
</TouchableHighlight>
</View>
</View>
</Modal>
</View>
);
}
}
const styles = StyleSheet.create({
modalViewStyle:{
flex: 1, flexDirection:'column' , marginTop:200, marginBottom:200, marginLeft:80, marginRight:80, height:220, backgroundColor:'#FFFFFF'
},
submitButtonStyle: {
width:200, height:50, backgroundColor: '#ff9933', marginLeft:120, borderRadius:2, marginTop:20
},
})
Now when the modal opens up, the screen looks like this:
Can someone please help me with this issue.

React native elements header background image

I made a header using react native elements, and I want to add background image into it. Is there anyway to do it?
I am using react-native-elements: "^0.19.1"
Here is my header code
render() {
return (
<View style={{ paddingTop: Constants.statusBarHeight, backgroundColor: color.light_grey }}>
<Header
leftComponent={this.props.left ? this.props.left : <IconWrapper name='menu' color='black' size={28} style={styles.icon} onPress={() => Actions.drawerOpen()} />}
centerComponent={<Text style={styles.headerText}>{this.props.title}</Text>}
rightComponent={this.props.right ? this.props.right : <IconWrapper name='handbag' type='simple-line-icon' color='black' size={28} style={styles.icon} onPress={() => Actions.BagContents()} />}
outerContainerStyles={[styles.headerOuterContainer, { height: 0.08 * windowHeight() }]}
/>
</View>
)
}
}
You can always create your own <Header/> component, probably will take you more time but you will be able to understand it and edit it as you like. I created a simple Header component to show you how you can accomplish adding a background image to your header. See the snack #abranhe/stackoverflow-56729412
Header.js
import React, { Component } from 'react';
import { View, TouchableOpacity, StyleSheet, Dimensions, ImageBackground } from 'react-native';
export default class Header extends Component {
renderContent() {
return (
<View style={styles.content}>
<View style={styles.left}>{this.props.left}</View>
<View style={styles.center}>{this.props.center}</View>
<View style={styles.right}>{this.props.right}</View>
</View>
);
}
renderHeaderWithImage() {
return (
<ImageBackground style={styles.container} source={this.props.imageSource}>
{this.renderContent()}
</ImageBackground>
);
}
renderHeaderWithoutImage() {
return (
<View style={[{ backgroundColor: '#f8f8f8' }, styles.container]}>
{this.renderContent()}
</View>
);
}
render() {
return this.props.image
? this.renderHeaderWithImage()
: this.renderHeaderWithoutImage();
}
}
const styles = StyleSheet.create({
container: {
top: 0,
position: 'absolute',
width: Dimensions.get('window').width,
backgroundColor: '#f8f8f8',
borderBottom: 1,
borderColor: '#f8f8f8',
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.5,
},
content: {
width: '100%',
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
marginTop: Dimensions.get('window').height * 0.03,
height: Dimensions.get('window').height * 0.045,
},
left: {
marginHorizontal: 5,
},
center: {
marginHorizontal: 5,
},
right: {
marginHorizontal: 5,
},
});
and then on when you want to use the Header component you can set the image prop to true, eg:
import React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import { Ionicons } from '#expo/vector-icons';
import Header from './components/Header';
export default () => {
return (
<View>
<Header
image
imageSource={{ uri: 'https://yourimage.png' }}
left={<Ionicons name="md-arrow-round-back" size={25} />}
center={<Text>Projects</Text>}
right={<Ionicons name="ios-camera" size={25} />}
/>
</View>
);
};
and then if you set the image prop to false you will remove the image from the background.
<Header
image={false}
imageSource={{ uri: 'https://yourimage.png' }}
left={<Ionicons name="md-arrow-round-back" size={25} />}
center={<Text>Projects</Text>}
right={<Ionicons name="ios-camera" size={25} />}
/>
There is ReactNative's component ImageBackground you can use it.
like this,
<ImageBackground source={...} style={{width: '100%', height: '100%'}}>
<Header
leftComponent={this.props.left ? this.props.left : <IconWrapper name='menu' color='black' size={28} style={styles.icon} onPress={() => Actions.drawerOpen()} />}
centerComponent={<Text style={styles.headerText}>{this.props.title}</Text>}
rightComponent={this.props.right ? this.props.right : <IconWrapper name='handbag' type='simple-line-icon' color='black' size={28} style={styles.icon} onPress={() => Actions.BagContents()} />}
outerContainerStyles={[styles.headerOuterContainer, { height: 0.08 * windowHeight() }]}
/>
</ImageBackground>
You can always style it your way.
assuming you are using react-navigation
You need to add a custon header component in navigationOptions,
import { Header } from 'react-navigation';
static navigationOptions = ({ navigation }) => {
return {
header: (props) => <View >
<LinearGradient
style={{ height: '100%', width: '100%', position: 'absolute' }}
start={{ x: 0, y: 1 }} end={{ x: 1, y: 0 }} colors={['#1A9EAE', '#4EAE7C']}
/>
<Header {...props} style={{ backgroundColor: Colors.transparent }} />
</View>,
}
}
This works for me.
<Header
backgroundImage={require("../../assets/images/btn-header-background.png")}
centerComponent={{
text: i18n.t("stats.title"),
style: { fontFamily: "FunctionLH", fontSize: 30, color: "#fff" }
}}
containerStyle={{
backgroundColor: "transparent",
justifyContent: "space-around"
}}
statusBarProps={{ barStyle: "light-content" }}
/>