Change font color with TouchableHighlight - react-native

How to change the font color of the button, the problem is that the font color doesn't look good so each time the user tap on the button I need to change the font color too.
My approach:
Normal Button
Pressed Button
<TouchableHighlight
underlayColor="#E5E6EA"
onPress={onOpen2}
style={{
fontFamily: "AktivGroteskCorp",
paddingLeft: 15,
paddingRight: 15,
borderRadius: 25.5,
flexDirection: "row",
justifyContent: "center",
marginBottom: 15,
width: vw * 67.2,
height: vw * 13.6,
alignItems: "center",
alignSelf: "center",
borderColor: "#E5E6EA",
borderWidth: 1,
}}
>
<Text
style={{
color: "#ffffff",
fontWeight: "bold",
fontSize: 16,
textAlign: "center",
alignSelf: "center",
}}
>
LOG IN
</Text>
</TouchableHighlight>

Here is a functional way
First, create useState then manipulate it on onPressIn/onPressOut
const TouchableHighlightExample = () => {
const [BtnColor, setBtnColor] = useState("red");
return (
<View>
<TouchableHighlight onPressIn={()=>setBtnColor("blue")} onPressOut={()=>setBtnColor("red")}>
<View>
<Text style={{color: BtnColor}}>Touch Here</Text>
</View>
</TouchableHighlight>
</View>
);
}

using onPressIn and onPressOut
https://reactnative.dev/docs/pressable#onpressin
state = {
fontColor: 'red',
}
<TouchableHighlight
onPressIn={()=>{ this.setState({fontColor:'blue'});}}
onPressOut={()=>{ this.setState({fontColor:'red'});}}
>
<Text
style={{
color: this.state.fontColor,
}}
>
LOG IN
</Text>
</TouchableHighlight>

Related

Set state is only working after clicking twice react native

Im trying to change the opacity of a button when the user clicks it, (between 2 buttons the darker one would be his choice). For some reason whenever i click the button once the opacity doesn't change and it only changes after 2 clicks. Any ideas where im going wrong?
export default function lastName({ navigation }) {
let [opac, setOpacity] = useState(0.3);
return (
<View style={styles.container}>
<View
style={{
flex: 0.5,
justifyContent: "flex-end",
//backgroundColor: "red",
}}
>
<Text style={styles.textStyle}>
Select a button
</Text>
</View>
<View
style={{
flex: 1,
justifyContent: "flex-start",
}}
>
<TouchableOpacity
style={{
marginTop: 70,
alignItems: "center",
justifyContent: "center",
width: 180,
height: 40,
backgroundColor: "#004953",
opacity: opac,/// here
borderRadius: 100,
}}
onPress={() => {
setOpacity(1); // here
}}
>
<Text
style={{
color: "white",
textAlign: "center",
fontSize: 18,
}}
>
Button 1
</Text>
</TouchableOpacity>
Apparently, its a bug with touchable opacity, you have to wrap opacity in a view, refer to this link Dynamic Opacity not changing when component rerenders in react native

React native background image not showing

I am showing simple image background on my app but don't know why its not showing
my code
return (
<View style={styles.container} >
<StatusBar translucent={true} backgroundColor="transparent" />
<View>
<ImageBackground
style={{ height: '100%', width: '100%', resizeMode: 'cover', opacity: 1, }}
source={require('../../../public/images/hhh.png')}
>
<View style={{
justifyContent: 'center', alignItems: 'center', marginTop: 'auto',
}}>
<Image
source={require('../../../public/icons/success.png')}
// style={styles.iconStyle}
/>
<Text style={styles.orderPlaceText}>
Order Placed
</Text>
<Text style={{
fontSize: 16,
fontWeight: "bold",
color: "white"
}}>
Order# {route.params.orderNumber}
</Text>
<Text style={styles.heading}>
{route.params.brandName}, {route.params.branchLocation.Address}
</Text>
<Text style={styles.description}>
{route.params.description}
</Text>
<Text style={styles.thanks}>
Thank you !
</Text>
</View>
<View style={{
justifyContent: 'center', alignItems: 'center',
marginTop: 'auto', marginBottom: 40
}}>
<TouchableOpacity
style={{
backgroundColor: '#ff8955',
borderRadius: 8,
alignItems: 'center',
justifyContent: 'center',
width: 300,
height: 50,
}}
onPress={() => {
navigation.dispatch(
StackActions.popToTop()
);
navigation.navigate('Orders');
}}
>
<Text style={{ color: 'white', fontSize: 14, fontWeight: 'bold', lineHeight: 23.5 }}>
TRACK MY ORDER
</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => navigation.navigate('Home')}
style={{
width: 300,
height: 50,
borderWidth: 1.5,
borderColor: 'white',
borderRadius: 8,
marginTop: 16,
alignItems: 'center',
justifyContent: 'center'
}}
>
<Text style={{ fontSize: 15, color: 'white' }}>Back to Home</Text>
</TouchableOpacity>
</View>
</ImageBackground>
</View>
</View>
)
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor:'grey'
},
orderPlaceText: {
fontSize: 28,
color: 'white',
fontWeight: 'bold',
marginTop: 16
},
heading: {
fontSize: 16,
color: 'white',
fontWeight: 'bold',
marginTop: 16
},
description: {
fontSize: 16,
color: 'white',
marginTop: 16,
textAlign: 'center'
},
thanks: {
fontSize: 26,
lineHeight: 31,
color: 'rgb(255, 255, 255)',
fontWeight: 'bold',
marginTop: 16
},
buttonText: {
color: 'white',
fontSize: 18,
fontWeight: 'bold',
lineHeight: 23
}
})
I am facing an unexpected issue if I change the image to another its working fine means only one image and if any other image it's not showing -_-
I try to rename my image and then try to use it also but that image is not showing is there any issue related to image size or something ? because it's not possible only few image is working and others are not.
Here in your image code
<Image source={require('../../../public/icons/success.png')}
// style={styles.iconStyle}
/>
You haven't added width/height, that must be the reason why your image isn't shown
Please do it this way
<Image style={{width:20, height:20}}
source={require('../../../public/icons/success.png')} />

How to change value only from radio button group in react native

Thanks for your time.
This is supposed to be very simple, but I am having trouble dealing with.
I have a "survey" which contains a few questions and 3 radio buttons for each question.
The problem is: when I select an option from any radio button, the same option is selected for every question.
I just need to change the value from this group only.
I am using RadioButton from react-native-paper.
The RadioButton.Group is supposed to deal with this but I am getting to nowhere.
How can I do this? I saw an example using react-native-simple-paper but it uses Classes and I using a Function component, so I can't use setState like almost every other library does.
Here's my code sample:
const [value, setValue] = React.useState('');
<ScrollView style={{ flexDirection: 'column' }}>
{questions &&
questions .map((p) => {
return (
<View style={{
//flexDirection: 'column',
elevation: 1,
borderRadius: 5,
backgroundColor: '#fff',
marginTop: 15,
marginLeft: 15,
marginRight: 15,
padding: 10
}}>
<Text
style={{
color: '#16416e',
fontFamily: 'Khula-Bold',
textTransform: 'capitalize',
fontSize: 20
}}>
{myCount++ + ' - ' + p.QUESTION_TEXT}
</Text>
<View style={{ marginTop: 15 }}>
<RadioButton.Group onValueChange={newValue => setValue(newValue)} value={value} key={myCount - 1}>
<View style={{ flexDirection: 'row' }}>
<RadioButton key={"S" + myCount - 1} color='#1b2f75' uncheckedColor='#1b2f75' value="S" />
<Text onPress={() => { setValue("S") }} style={{ fontFamily: 'Khula-Bold', color: '#16416e', fontSize: 20, marginTop: 5, marginLeft: 5, paddingBottom: 15 }} >Sim</Text>
</View>
<View style={{ flexDirection: 'row' }}>
<RadioButton key={"N" + myCount - 1} color='#1b2f75' uncheckedColor='#1b2f75' value="N" />
<Text onPress={() => { setValue("N") }} style={{ fontFamily: 'Khula-Bold', color: '#16416e', fontSize: 20, marginTop: 5, marginLeft: 5, paddingBottom: 15 }}>Não</Text>
</View>
<View style={{ flexDirection: 'row' }}>
<RadioButton key={"NA" + myCount - 1} color='#1b2f75' uncheckedColor='#1b2f75' value="NA" />
<Text onPress={() => { setValue("NA") }} style={{ fontFamily: 'Khula-Bold', color: '#16416e', fontSize: 20, marginTop: 5, marginLeft: 5, paddingBottom: 15 }}>N/A</Text>
</View>
</RadioButton.Group>
</View>
</View>
);
})}
</ScrollView>
It is working using react-native-radio-buttons-group
Can provide you more detailed support.

Flexbox alignment issue in react-native

I'm trying to align the contents of a View component into left and right side of the screen. Unfortunately, whatever I do, I can only align the parent View and not the child View components.
In the snippet below, item is the Parent View, leftIcon and title are supposed to be aligned together in the left and rightArrow is supposed to be aligned in the right.
<TouchableOpacity onPress={() => navigation.navigate(`${item.page}`)}>
<View style={styles.item}>
<Text style={styles.leftIcon}>a</Text>
<Text style={styles.title}>{item.title}</Text>
<Text style={styles.rightArrow}> > </Text>
</View>
</TouchableOpacity>
const styles = StyleSheet.create({
item: {
//backgroundColor: '#f9c2ff',
paddingTop: 7,
marginVertical: 4,
marginHorizontal: 16,
flex: 1,
flexDirection: 'row',
},
leftIcon: {
fontSize: 25,
paddingRight: '5%',
alignSelf: 'flex-start',
},
title: {
fontSize: 25,
alignSelf: 'flex-start',
},
rightArrow: {
fontSize: 25,
alignSelf: 'flex-end'
}
});
This might be an incorrect approach, but what can be done to achieve this?
You can wrap leftIcon and title in a view with a flexDirection: 'row' style.
Then add justifyContent: 'space-between' to your item styles.
Example (using inline styles):
<TouchableOpacity>
<View
style={{
marginVertical: 4,
marginHorizontal: 16,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
}}>
<View style={{ flexDirection: 'row' }}>
<Text
style={{
fontSize: 25,
paddingRight: '5%',
}}>
a
</Text>
<Text style={{ fontSize: 25 }}>Title</Text>
</View>
<Text style={{ fontSize: 25 }}> > </Text>
</View>
</TouchableOpacity>

How to align multiline text in react native

In my screen, I need to split the text into column view and need to align the right side text with the left side text. also, need to show the whole text line by line. Tested with giving some height, width, position: absolute but didn't work. Any idea how to solve this?
I'll post my current code and screenshot for your ref.
Many thanks for your help
<View style={styles.twoCol}>
<View style={styles.leftField}>
<Text style={styles.leftFieldTitle}>Location:</Text>
</View>
<View style={styles.locationRightSide}>
<Text style={styles.rightFieldTitle}>{location}</Text>
</View>
</View>
const styles = StyleSheet.create({
twoCol: {
alignItems: 'center',
flexDirection: 'row',
marginTop: 25,
},
leftField: {
alignItems: 'flex-end',
width: '25%',
},
leftFieldTitle: {
fontWeight: '700',
color: 'black'
},
},
locationRightSide: {
width: '70%',
alignItems: 'flex-start',
paddingLeft: 10
},
rightFieldTitle: {
fontSize: RF(2.8),
color: 'black'
},
)}
Problem solved!!! Thanks everyone!
<View style={{
padding: 10,
flexDirection: "row",
marginLeft: 10,
marginRight: 5,
marginTop: 5,
marginBottom: 3,
justifyContent: "flex-start",
width: '95%',
}} >
<View style={{
position: 'absolute',
marginTop: 10
}}>
<Text style={styles.leftFieldTitle}>Location:</Text>
</View>
<View style={{ marginLeft: '26%' }}>
<Text style={styles.rightFieldTitle}>{location}</Text>
</View>
</View>