onclick change img opacity - react native? - css

I'm really new to react native, and Im working on a simple app with 4 images and four button, each button should change the opacity on an image, but I'm lost om how to do that....can someonne point me into the right direccion on how to do this.
<View style={styles.card}>
<Image source={require('./assets/1.jpg')} style={styles.cardImgStyle}></Image>
<Text style={styles.cardText}>Area 1</Text>
</View>
<View style={styles.card}>
<Image source={require('./assets/2.jpg')} style={styles.cardImgStyle}></Image>
<Text style={styles.cardText}>Area 2</Text>
</View>
const styles = StyleSheet.create({
cardImgStyle: {
width : "100%",
height : 140,
borderTopLeftRadius : 10,
borderTopRightRadius: 10,
marginBottom : 3,
opacity: .2
},
});

maybe you can useState for this case, in my opinion you can handle the id on object to selected the image, and you can use the condtional rendering when user selected, set the style.selectImage and use the opacity 0.4. you can check my code example on bottom . hope this my explanation can help you.
const data = [
{
id: 1,
image : require('../../yourpath'),
id: 2,
image: require('../../yourpath')
}
]
const = [ selected, setSelected ] = useState(0)
<FlatList
horizontal
pagingEnabled={true}
data={data}
style={{height:130 }}
renderItem={({item})=>{
const content =
selected === item.id
? style.selectImage
: style.unSelectImage;
return(
<View style={style.wrapper}>
<TouchableOpacity style={content} onPress={()=>setSelectedId(item.id)}}>
<View style={{justifyContent: 'center', alignItems: 'center'}}>
<Image source={item.image} style={{height: 65, width: 60}}/>
</View>
<Text style={{color: 'black', textAlign: 'center', marginTop: 10}}>{item.title}</Text>
</TouchableOpacity>
</View>
)
}}/>

Related

React Native - Align text in centre

import React from 'react'
import { View, StyleSheet, Image, Text } from 'react-native'
function RecentlyJoinedProfileCard({title, image}) {
return (
<View style={styles.container}>
<View>
<Image style={styles.image} source={{uri: image}} />
<Text styles={styles.name}>{title}</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
paddingBottom:55
},
image: {
width:100,
height:100,
borderRadius: 75,
marginHorizontal:10,
marginVertical:10
},
name: {
fontSize: 15,
fontWeight: "bold",
alignSelf: 'center'
}
})
export default RecentlyJoinedProfileCard
This gives me the following:
The text is not aligning in the center of each image:
I've tried alignSelf: 'center' style in the text
I've also tried applying the following to the container
justifyContent: 'center',
alignItems: 'center',
Any idea on how to centre the name so its aligned in the centre rather than to the left.
Try this ->
<View style={{alignItems:'center'}}>
<Image style={styles.image} source={{uri: image}} />
<Text styles={styles.name}>{title}</Text>
</View>
Why are you nesting View inside View?
Okay for your style to be applied on the child elements you need to give the style to the nested View and use alignItems instead of alignSelf
E.g
<View style={styles.subContainer}>
<Image style={styles.image} source={{uri: image}} />
<Text styles={styles.name}>{title}</Text>
</View>
const styles = StyleSheet.create({
...
subContainer: {
justifyContent: 'center',
alignItems: 'center'
}
})
Just add alignItems && justifyContent "center" to parent View to align your items in center
just update your styles code like this:
const styles = StyleSheet.create({
container: {
justifyContent: 'center',
alignItems: 'center'
paddingBottom:55
},
})
Change the alignSelf to textAlign: 'center'
name: {
//...
textAlign: 'center'
}

React Native: Background Image not filling the entire app

I am trying to get the background image to take up the entire space on the screen
As you can see from the image this does not seem to be working. I think it may be something to do with the scroll view but unfortunately I cannot figure it out.
Ideally want to use flex box for the solution.
const App: () => React$Node = () => {
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={styles.scrollView}>
<View style={styles.container}>
<ImageBackground
source={require('./src/assets/foodflixIMG.jpg')}
style={styles.image}>
<Text style={styles.text}>Inside</Text>
</ImageBackground>
</View>
</ScrollView>
</SafeAreaView>
</>
);
};
const styles = StyleSheet.create({
scrollView: {
backgroundColor: 'red',
height: '100%',
},
container: {
flex: 1,
flexDirection: 'column',
},
image: {
flex: 1,
resizeMode: 'cover',
justifyContent: 'center',
},
text: {
color: 'grey',
fontSize: 30,
fontWeight: 'bold',
}
});
export default App;
Check your code something like this:
<ImageBackground
source={require('./assets/main.jpg')}
style={{height:"100%",
width:"100%", flex:1}}
>
<SafeAreaView>
<ScrollView>
<View style={styles.container}>
<Text>Hello</Text>
</View>
</ScrollView>
</SafeAreaView>
</ImageBackground>
Hope this helps!

I want my text to flow to my next line but flex is not letting me do it?

I want my app to add text underneath the goal section but flex is not letting me do that even if I create new components in native P.S Still a beginner
import React, { useState } from "react";
import { StyleSheet, View, TextInput, Button, Text } from "react-native";
export default function App() {
return (
<View style={styles.Container}>
<View style={styles.GoalInp}>
<TextInput
placeholder="Course Goal"
style={{ overflow: "scroll" }}
onChangeText={goalInputHandler}
value={enteredGoal}
/>
</View>
<View>
<Button title="ADD" onPress={addGoalHandler} />
</View>
<View>
{couseGoals.map(goal => (
<Text>{goal}</Text>
))}
</View>
</View>
);
}
const styles = StyleSheet.create({
Container: {
padding: 50,
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center"
},
GoalInp: {
borderWidth: 1,
width: "80%",
padding: 10,
borderColor: "black"
}
});
You need to bring the listing outside the Container View, as the flex-direction of Container is row, all items will be in a row including form.
My modified code is given below
<View style={styles.wrapper}>
<View style={styles.Container}>
<View style={styles.GoalInp}>
<TextInput
placeholder="Course Goal"
style={{ overflow: "scroll" }}
/>
</View>
<View>
<Button title="ADD" onPress={this.addGoalHandler} />
</View>
</View>
<View>
{this.state.couseGoals.map(goal => (
<Text>{goal}</Text>
))}
</View>
</View>
const styles = StyleSheet.create({
wrapper:{
flex:1,
},
Container: {
padding: 50,
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center"
},
GoalInp: {
borderWidth: 1,
width: "80%",
padding: 10,
borderColor: "black"
},
});
I think that you use to much Vieuw elements.
You should for example use a Text for where your text need to print out.
But I think I have it.
You wrapped everything into a View with a style of;
flexDirection: "row",
Not that good if you don't want to have the whole View on one line....
Issue solved?

Why do my in-line styles work, but my stylesheet isn't changing the code at all?

I am coding a navbar in React Native. Think of it as double layered -- the upper layer is a burger menu, a title, and a search icon, the second layer consists of three touchable titles to navigate to the relevant screens.
a mock-up of the navbar I'm trying to create
When I apply inline styles, they work. When I do Stylesheet.create and apply styles down there, they don't. I am new to programming and very confused.
My plan is to code a single navbar that is split into two rows (views): navbarTop and navbarBottom. I would very much appreciate some insight into whether doing so makes sense, and also how to fix my styling issue. Thank you all very much!
import React, { Component } from 'react';
import { View, Text, StyleSheet, Image, TouchableHighlight } from 'react-native';
import { withNavigation } from 'react-navigation';
class Navbar extends Component {
render() {
return (
<View style={styles.navbarTop}>
<View>
<TouchableHighlight
onPress={() => {
this.props.navigation.openDrawer();
}}
>
<Image
style={{marginBottom: 5}}
source={require('./../../../android/app/src/main/res/drawable/menu.png')}
/>
</TouchableHighlight>
</View>
<View>
<Text
style={{
fontWeight: 'bold',
color: 'white',
fontSize: 20,
marginRight: 70
}}
>
Dashboard
</Text>
</View>
<View>
<Image
style={{marginBottom: 5}}
source={require('./../../../android/app/src/main/res/drawable/search.png')}
/>
</View>
<View style={styles.navbarBottom}>
<View>
<Text
style={{
color: 'white',
fontSize: 15,
marginRight: 70
}}
> RECORDINGS </Text>
</View>
<View>
<Text
style={{
color: 'white',
fontSize: 15,
marginRight: 70
}}
> PATIENTS </Text>
</View>
<View>
<Text
style={{
color: 'white',
fontSize: 15,
marginRight: 70
}}
> DEVICES </Text>
</View>
</View>
</View>
);
}
}
export default withNavigation(Navbar);
const styles = StyleSheet.create({
navbarTop: {
backgroundColor: '#14172B',
padding: 10,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'flex-end',
},
// navbarBottom: {
// }
});
In line styles over write the style which are present in the stylesheet.
Let's say there is a style component :
<View style = {[styles.demoView,{marginTop:50}]}/>
export default StyleSheet.create({
demoView: {
marginTop:20,
flexDirection: "row",
padding: "10rem",
justifyContent: "space-between",
alignItems: "center",
},
In the above style the inline style will replace the value of marginTop because it has high priority and the it would least concern even if you remove the marginTop property from the stylesheet as it is been supressed by inline style. Hope it gives you clear vision.
Happy Coding :)
Edit :
import React, { Component } from 'react';
import { View, Text, StyleSheet, Image, TouchableHighlight } from 'react-native';
import { withNavigation } from 'react-navigation';
class Navbar extends Component {
render() {
return (
<View style={styles.navbarTop}>
<TouchableHighlight
onPress={() => { this.props.navigation.openDrawer()}}>
<Image style={{marginBottom: 5}}
source={require('./../../../android/app/src/main/res/drawable/menu.png')}
/>
</TouchableHighlight>
<Text style={{fontWeight: 'bold',color: 'white',fontSize: 20,marginRight: 70}}>
Dashboard
</Text>
<Image
style={{marginBottom: 5}}
source={require('./../../../android/app/src/main/res/drawable/search.png')}
/>
<View style={styles.navbarBottom}>
<Text style={styles.navBarText}> RECORDINGS </Text>
<Text style={styles.navBarText}> PATIENTS </Text>
<Text style={styles.navBarText}> DEVICES </Text>
</View>
</View>
);
}
}
export default withNavigation(Navbar);
const styles = StyleSheet.create({
navbarTop: {
backgroundColor: '#14172B',
padding: 10,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'flex-end',
},
// navbarBottom: {
// }
navBarText:{
color: 'white',
fontSize: 15,
marginRight: 70
}
});

How can I mix fixed and dynamic size elements in a flexbox container?

I'm trying to create a layout that utilizes flexbox' automatic sizing but also contains fixed-size items:
<View style={{ height: 70, alignItems: "center" }}>
<Text style={{ flex: 1, textAlign: "right", paddingRight: 10 }}>left text</Text>
<Image style={{ width: 70, height: 70 }} src={require('./img.png')} />
<Text style={{ flex: 1, textAlign: "left", paddingLeft: 10 }}>right text</Text>
</View>
I want the image to be centered in the UI and have the text views equally take the remaining width. The actual result is that one of the text views is larger than the other as if the rendering doesn't take the width of the image view into account.
Finally found the correct way of doing this. To have fixed size items in a flex layout one shouldn't use width/height but flex: 0 and flexBasis. This approach initially didn't work for me because a component wrapping my component wasn't sized correctly and messed with the flex rendering. flexBasis seems to be density-independent pixels when it is set to an absolute number.
Proof of concept on expo.io
import React, { Component } from 'react';
import { Text, View, StyleSheet } from 'react-native';
import { Constants } from 'expo';
export default class App extends Component {
render() {
return (
<View>
<Text>Test</Text>
<View style={styles.container}>
<Text style={styles.text1}>{"Left\nText"}</Text>
<View style={styles.view} />
<Text style={styles.text2}>Test</Text>
</View>
<View style={styles.container}>
<Text style={styles.text1}>{"Left\nText"}</Text>
<View style={styles.view} />
<Text style={styles.text2}>Test</Text>
</View>
<View style={styles.container}>
<Text style={styles.text1}>{"Left\nText"}</Text>
<View style={styles.view} />
<Text style={styles.text2}>Test</Text>
</View>
<Text>Test</Text>
<View style={styles.container}>
<Text style={styles.text1}>{"Left\nText"}</Text>
<View style={styles.view} />
<Text style={styles.text2}>Test</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flexDirection: "row",
alignItems: 'center',
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
},
view: {
flex: 0,
flexBasis: 50,
height: 50,
backgroundColor: "red",
},
text1: {
flex: 1,
textAlign: "right",
paddingRight: 10,
},
text2: {
flex: 1,
textAlign: "left",
paddingLeft: 10,
}
});

Resources