I am working on a react native application and I was dealing with Modal and Pressable components, in fact the problem I got is not an error or a bug but I was trying to place a pressable text at the very right down side of the screen but I am struggling with styling a little bit so if anyone can give me a hand in this I would be so grateful.
this the code I wrote for the pop up form component:
import React from 'react';
import {View, Text, Modal, StyleSheet, Pressable, Alert} from 'react-native';
const PopUpModal = ({visible, onPress})=>{
return(
<View style={styles.container}>
<Modal
animationType="slide"
transparent={true}
visible={visible}
onRequestClose={() => {
Alert.alert("Modal has been closed.");
}}
>
<View style={styles.container}>
<View style={styles.modalView}>
<Text style={styles.modalText}>Hello World!</Text>
<Pressable
style={[styles.button, styles.buttonClose]}
onPress={onPress}
>
<Text style={styles.textStyle}>Cancel</Text>
</Pressable>
</View>
</View>
</Modal>
</View>
)
}
const styles = StyleSheet.create({
container:{
width: "100%",
height : "100%"
},
modalView: {
margin: 20,
backgroundColor: "white",
borderRadius: 5,
padding: 35,
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 2
},
shadowOpacity: 0.25,
shadowRadius: 4,
elevation: 5,
},
button: {
display: "flex",
borderRadius: 2,
padding: 10,
flexDirection:"row-reverse",
width: 65,
backgroundColor: "#051316",
},
buttonClose: {
},
textStyle: {
color: "white",
fontWeight: "bold",
textAlign: "center",
alignItems: "flex-end"
},
modalText: {
marginBottom: 15,
flexDirection: "row-reverse"
}
})
export default PopUpModal
and this is what it looks like as an output of the above code example
output pic
Put your Pressable inside a View with the style property flexDirection:"row-reverse"
<View style={{flexDirection:"row-reverse"}}>
<Pressable
style={[styles.button, styles.buttonClose]}
onPress={onPress}
>
<Text style={styles.textStyle}>Cancel</Text>
</Pressable>
</View>
Related
Screenshot
Screenshot2
import React from 'react';
import { View, SafeAreaView, StyleSheet, TextInput, Text, TouchableOpacity, Platform } from 'react-native';
import MapView, { UrlTile } from 'react-native-maps';
import tw from 'tailwind-react-native-classnames';
import { Card } from 'react-native-shadow-cards';
import { useNavigation } from '#react-navigation/native';
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
const MapScreen = () => {
const navigation = useNavigation();
let location = {
latitude: 5.354846154402075,
longitude: 100.30152659738222,
latitudeDelta: 0.001,
longitudeDelta: 0.002
}
return (
<SafeAreaView style={tw`bg-white h-full`}>
<View style={tw`h-2/3`}>
<MapView
style={styles.map}
mapType='standard'
region={location}>
<UrlTile
urlTemplate='https://api.maptiler.com/maps/openstreetmap/256/{z}/{x}/{y}.jpg?key=2fFGEOiBVDCSMRNfaU70'
shouldReplaceMapContent={true}>
</UrlTile>
</MapView>
</View>
<KeyboardAwareScrollView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
bounces={false}
enableOnAndroid={true}
scrollEnabled={true}
enableAutomaticScroll={true}
contentContainerStyle={{ flex: 1 }}>
<View style={styles.cardContainer}>
<Card style={styles.cardView}>
<Text style={{ fontSize: 11, paddingTop: 10, paddingLeft: 10 }}>From</Text>
<View style={styles.inputBox}>
<TextInput
style={{ paddingVertical: 0, paddingLeft: 10 }}
placeholder='Current Location'
keyboardType='current-location'>
</TextInput>
</View>
<Text style={{ fontSize: 11, paddingTop: 10, paddingLeft: 10 }}>To</Text>
<View style={styles.inputBox}>
<TextInput
style={{ paddingVertical: 0, paddingLeft: 10 }}
placeholder='Destination'
keyboardType='destination'>
</TextInput>
</View>
<View>
<TouchableOpacity
onPress={() => navigation.navigate('ShowResultScreen')}
style={styles.button}>
<Text
style={{
textAlign: 'center',
fontWeight: '700',
fontSize: 17,
color: '#fff'
}}>
Search
</Text>
</TouchableOpacity>
</View>
</Card>
</View>
</KeyboardAwareScrollView>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
map: {
width: '100%',
height: '100%',
},
cardContainer: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
borderRadius: 25,
shadowOpacity: 0.8
},
cardView: {
width: '97%',
height: '95%',
paddingLeft: 10,
paddingTop: 3
},
button: {
backgroundColor: '#5AA9E6',
padding: 10,
borderRadius: 10,
marginTop: 15,
marginRight: 10
},
inputBox: {
borderRadius: 10,
width: '97%',
height: '15%',
backgroundColor: '#F5F3F4',
marginTop: 10
}
});
export default MapScreen;
I want to make the input section below is scrollable and not covered by the keyboard when the keyboard is pop up so that the Search button can be pressed after typing the required input.
Update: I have used KeyboardAwareScrollView instead of using KeyboardAvoidingView and ScrollView. However, the result is like the Screenshot2.
The easiest way is to use the package react-native-keyboard-aware-scroll-view.
I am only 3 months in React-Native, and I was going through a lot of styling and shaping, and I encountered a problem with this diagonal cut, every time I try to use dimensions, it always ends up with no image and no text, I think I am missing something obvious, but I gave up on searching and trying it has been almost 2 weeks experimenting with dimensions.my code and what i tired below and the images can be seen if you copy and paste the links generated by stackoverflow.
[![Expected results](https://i.stack.imgur.com/flJyJ.jpg)](https://i.stack.imgur.com/flJyJ.jpg)
[![What my current selection screen looks like](https://i.stack.imgur.com/gUoyZ.jpg)](https://i.stack.imgur.com/gUoyZ.jpg)
[![when i try and use dimensions and try to cut it diagonal](https://i.stack.imgur.com/JYprq.png)](https://i.stack.imgur.com/JYprq.png)
`import { StyleSheet, Text, View, StatusBar, ImageBackground, TouchableOpacity, Dimensions } from 'react-native'
import React from 'react'
const OnBoarding = ({navigation}) => {
return (
<View style={{flex: 1, backgroundColor:"transparent"}}>
<StatusBar barStyle="light-content" backgroundColor='black' />
{/* background one image */}
<TouchableOpacity style={{flex: 1, backgroundColor:'transparent'}} onPress={()=> navigation.navigate("Selection")}>
<ImageBackground source={require('../assets/images/w20.jpg')} resizeMode='stretch' style={styles.image}>
<Text style={styles.titleText}>THE WORLD</Text>
<Text style={styles.surText}>Start playing now!</Text>
</ImageBackground>
</TouchableOpacity>
{/* background two image */}
<TouchableOpacity style={{flex: 1, backgroundColor:'transparent'}} onPress={()=>navigation.navigate("About")}>
<ImageBackground source={require('../assets/images/wp21.jpg')} resizeMode='stretch' style={styles.image}>
<Text style={styles.titleText}>HOW TO LIVE</Text>
<Text style={styles.surText}>Learn how to play!</Text>
</ImageBackground>
</TouchableOpacity>
</View>
)
}
export default OnBoarding
const styles = StyleSheet.create({
image:{
flex:1,
width:'100%',
backgroundColor:'transparent',
borderRightWidth: Dimensions.get('window').width,
borderTopWidth:225,
borderLeftColor: 'transparent',
borderRightColor: 'transparent',
},
titleText:{
flex: 1,
fontSize:42,
fontFamily:"Philosopher-Bold",
textAlign:'center',
textAlignVertical:'bottom',
color: 'white',
textShadowColor: 'black',
textShadowOffset: { width: -1, height: 0 },
textShadowRadius: 30,
},
surText:{
flex: 1,
fontSize:22.5,
textAlign:'center',
fontFamily:"Philosopher-Regular",
textAlignVertical:'top',
color: 'white',
textShadowColor: 'black',
textShadowOffset: { width: -1, height: 0 },
textShadowRadius: 30,
},
})
I'm trying to make a layout like this:
In order to do so, I've made two components named HalfWidthFullHeightCard and HalfWithHalfHeightCard.
I've created the HalfWidthFullHeightCell component as?
<TouchableOpacity onPress={pressEvent}>
<ImageBackground
source={sourcePath}
imageStyle={{ borderRadius: 8, resizeMode: 'cover', width: '100%' }}
style={styles.halfWidthCard}>
<Text style={styles.halfWidthCardHeading}>{heading}</Text>
<Text style={styles.halfWidthCardText}>{cardText}</Text>
</ImageBackground>
</TouchableOpacity>
...
halfWidthCard: {
backgroundColor: colors.brightYellow,
marginBottom: 10,
borderRadius: 8,
},
Based on the cardText the width of the card is calculated automatically and in the halfWidthCardText StyleSheet I've only had padding: 10
Next for HalfWithHalfHeightCard everything is the same except for the styling which is:
...
smallHalfWidthCard: {
backgroundColor: colors.brightYellow,
borderRadius: 8,
marginBottom: 10
},
smallHalfWidthCardHeading: {
padding: 10,
},
smallHalfWidthCardText: {
padding: 10,
},
Where I'm putting both of these components together I'm doing:
<ScrollView contentContainerStyle={{padding: 15}}>
<View style={{flexDirection: 'row',}}>
<HalfWidthFullHeightCell />
<View>
<HalfWithHalfHeightCell />
<HalfWithHalfHeightCell />
</View>
</View>
</ScrollView>
Now there are two problems:
Consider the gray area as the width of the device. The HalfWidthFullHeightCard takes 100% of the space and
The HalfWithHalfHeightCard are outside of the screen and also not of the same height as HalfWidthFullHeightCard.
So, how can I make these components flexible so that they adapt to the layout as screen size changes?
I would have made it like this
import * as React from 'react';
import { Text, View, StyleSheet, Dimensions } from 'react-native';
const ScreenWidth = Dimensions.get('window').width;
export default function App() {
return (
<View style={styles.container}>
<View style={styles.WholeBox}>
<View style={styles.Block}></View>
<View style={{ flex: 1 }}>
<View style={styles.Block}></View>
<View style={styles.Block}></View>
</View>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#ecf0f1',
},
WholeBox: {
width: ScreenWidth,
height: 300,
flexDirection: 'row',
},
Block: {
flex: 1,
backgroundColor: '#DDA73A',
margin: 6,
borderRadius: 8,
},
});
Working Example Here
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View, TouchableOpacity, Image } from 'react-native';
import check from './assets/icons/covid.png'
import pill from './assets/icons/pill.png'
export default function App() {
return (
<View style={styles.container}>
<View style={styles.containerone}>
<TouchableOpacity style={{borderRadius: 100, height: 150, width: 150, backgroundColor: '#fff', marginTop: 80}}>
<Image source={pill} style={styles.pillButton}/>
<Image source={check} style={styles.checkButton}/>
</TouchableOpacity>
</View>
<View style={styles.containertwo}>
<Text style={{color:'#982fc2', fontWeight:'bold', fontSize:20}}>Text b</Text>
<Text style={{color:'#982fc2', fontWeight:'bold', fontSize:20}}>Text a</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#982fc2'
},
containerone: {
flex: 1,
alignItems: 'center'
},
containertwo: {
flex: 1,
backgroundColor: '#fff',
borderTopRightRadius: 60,
borderTopLeftRadius: 60,
padding: 40
},
button: {
backgroundColor: '#fff',
borderRadius: 100,
height: 150,
width: 150,
marginTop: 100,
alignItems:'center',
justifyContent: 'center'
},
checkButton: {
height: 130,
width: 130,
display:'none'
},
pillButton: {
height: 130,
width: 130,
display: 'flex',
marginTop: 10
}
});
I have a simple question, how can I change the style of two buttons in press the first:
I created one style for each button, the first button is visible, and the second is hidden, so, I want to, when I press the first button, I change it style to display: 'none', and in the second, to display:'flex', can you guys help me?
Ciao, you colud use state to alternate Image visibility avoding to use css. Something like:
export default function App() {
const[alternateImage, setAlternateImage] = useState(true);
const changeImage = () => {
setAlternateImage(alternateImage => !alternateImage);
}
return (
<View style={styles.container}>
<View style={styles.containerone}>
<TouchableOpacity style={{borderRadius: 100, height: 150, width: 150, backgroundColor: '#fff', marginTop: 80}} onPress={changeImage}>
{alternateImage && <Image source={pill} style={styles.pillButton}/>}
{!alternateImage && <Image source={check} style={styles.checkButton}/>}
</TouchableOpacity>
</View>
<View style={styles.containertwo}>
<Text style={{color:'#982fc2', fontWeight:'bold', fontSize:20}}>Text b</Text>
<Text style={{color:'#982fc2', fontWeight:'bold', fontSize:20}}>Text a</Text>
</View>
</View>
);
}
and you could remove display: 'none' from Image css.
Formy post screen I want to take the title and body of each one
import React, { Component } from 'react';
import { StyleSheet, TouchableOpacity, Image, Text, View, Dimensions, TouchableWithoutFeedback } from 'react-native';
import { withNavigation } from 'react-navigation';
class Card extends Component {
render() {
return (
<TouchableWithoutFeedback onPress={() => this.props.navigation.navigate('Post')}>
<View style={{
flexDirection: 'row',
backgroundColor: 'white',
borderBottomWidth: 1,
borderRightWidth: 1,
borderLeftWidth: 1,
borderColor: 'rgba(200,200,200,0.3)',
borderRadius: 6,
width: '86%',
maxHeight: 120,
minHeight: 120,
alignSelf: 'center',
marginBottom: 5
}}>
<Image style={{
width: 100,
maxHeight: 100,
minHeight: 100,
borderRadius: 20,
}}
source={{ uri: 'https://im.ziffdavisinternational.com/ign_br/screenshot/default/screenshot-2_5s54.png',
}}
/>
<View style={{
width: '66%',
padding: 5}}
>
<Text style={{
fontSize: 14,
fontWeight: 'bold',
fontFamily: 'verdana',
}}
numberOfLines={2}
>
{this.props.item.title}
</Text>
<Text style={{
fontSize: 12,
marginTop: 5
}} numberOfLines={3}
>
{this.props.item.body}
</Text>
</View>
</View>
</TouchableWithoutFeedback>
);
}
}
export default withNavigation(Card);
The question is very unclear about what you want to accomplish with a very little piece of information.
If the question is about passing parameters from a screen to another (in this case, title and body), using react navigation, then you just have to change your navigate function in the TouchableWithoutFeedback to:
this.props.navigation.navigate('Post',{ "title":this.props.item.title, "body": this.props.item.body })
Then, when you want to use them in the destination screen (in this case, Post), you can access them using:
this.props.navigation.state.params.title //or this.props.navigation.state.params.body
or do
var title=this.props.navigation.getParam("title")