I am trying to change the style of a button in React Native, but the edits for the button colour, margin, outlines etc aren't working at all. Here is the pic. Below is my code for HomeScreen.js
import React from 'react';
import {
StyleSheet,
TextInput,
View,
Text,
Button,
ScrollView,
Image,
Keyboard,
TouchableOpacity,
KeyboardAvoidingView,
} from 'react-native';
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Image
source={require('../image/success.png')}
style={{
width: '50%',
height: 100,
resizeMode: 'contain',
marginTop: 0,
}}
/>
<Text style={{
fontSize: 20,
textAlign: 'center',
marginLeft: 35,
marginRight: 35,
marginTop: 0,
marginBottom: 10,
color: '#00a3cc'
}}>
{'\n\n'}
This is the Home Screen
</Text>
<Button
style={styles.buttonStyle}
onPress={() => navigation.navigate('Settings')}
title="Go to Settings"
/>
</View>
);
};
export default HomeScreen;
const styles = StyleSheet.create ({
buttonStyle: {
backgroundColor: '#7DE24E',
borderWidth: 0,
color: '#FFFFFF',
borderColor: '#7DE24E',
height: 40,
alignItems: 'center',
borderRadius: 30,
marginLeft: 35,
marginRight: 35,
marginTop: 20,
marginBottom: 25,
}
});
Some help would be appreciated, let me know if more info is required. Thanks in advance.
You should use TouchableOpacity instead.
import React from 'react';
import {
StyleSheet,
TextInput,
View,
Text,
ScrollView,
Image,
Keyboard,
TouchableOpacity,
KeyboardAvoidingView,
} from 'react-native';
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Image
source={require('../image/success.png')}
style={{
width: '50%',
height: 100,
resizeMode: 'contain',
marginTop: 0,
}}
/>
<Text style={{
fontSize: 20,
textAlign: 'center',
marginLeft: 35,
marginRight: 35,
marginTop: 0,
marginBottom: 10,
color: '#00a3cc'
}}>
{'\n\n'}
This is the Home Screen
</Text>
<TouchableOpacity
onPress={() => navigation.navigate('Settings')}
style={styles.buttonStyle}
>
<Text style={styles.btnText}>Go to Settings</Text>
</TouchableOpacity>
</View>
);
};
export default HomeScreen;
const styles = StyleSheet.create ({
btnText: {
color: '#FFFFFF',
fontSize: 18,
textAlign: 'center',
},
buttonStyle: {
backgroundColor: '#7DE24E',
height: 40,
alignItems: 'center',
borderRadius: 30,
marginLeft: 35,
marginRight: 35,
marginTop: 20,
marginBottom: 25,
}
});
Related
I am having issues with the opacity. Trying to have it a bit dim when the Modal is triggered, it dims the background with some form of Opacity and all. How can I dim the background when the modal is triggered?
The image is looking thus :
Here is my source code
import {
ImageBackground,
Modal,
ScrollView,
StyleSheet,
Text,
TextInput,
TouchableOpacity,
View,
} from 'react-native';
import {Picker} from '#react-native-picker/picker';
import React, {useState} from 'react';
import {useNavigation} from '#react-navigation/native';
const LocalPayments = () => {
const navigation = useNavigation();
const [modalVisible, setModalVisible] = useState(false);
return (
<View style={styles.container}>
<View style={styles.space} />
<ScrollView
contentContainerStyle={{
justifyContent: 'space-between',
alignItems: 'center',
}}>
<ImageBackground
source={{
uri: 'asset:/logo/bg.JPG',
}}
imageStyle={{borderRadius: 6}}
style={{
top: -30,
paddingTop: 95,
alignSelf: 'center',
width: 328,
height: 115,
borderadius: 9,
justifyContent: 'center',
alignSelf: 'center',
alignItems: 'center',
}}>
<View>
<Text style={styles.accText}>Wallet Balance</Text>
<Text style={styles.text}> 250,000 </Text>
</View>
</ImageBackground>
<View
style={{
borderRadius: 5,
borderWidth: 1,
overflow: 'hidden',
height: 35,
padding: 0,
borderColor: '#00BB23',
}}>
{
<Picker
style={{
width: 300,
height: 55,
borderBottomWidth: 1,
}}
itemStyle={{
fontSize: 25,
fontFamily: 'Poppins-Medium',
}}>
<Picker.Item label="Select Bank" value="accNum" />
</Picker>
}
</View>
<TextInput
placeholder="Destination Account"
onChangeText={creditAccount => this.setState({creditAccount})}
style={styles.input}
/>
<TextInput
placeholder=" Amount"
onChangeText={amount => this.setState({amount})}
style={styles.input}
/>
<TextInput
placeholder=" Narration"
onChangeText={description => this.setState({description})}
style={styles.input}
/>
<View
style={{
borderRadius: 5,
borderWidth: 1,
overflow: 'hidden',
height: 35,
padding: 0,
top: 10,
borderColor: '#00BB23',
}}>
{
<Picker
style={{
width: 300,
height: 55,
borderBottomWidth: 1,
}}
itemStyle={{
fontSize: 25,
fontFamily: 'Poppins-Medium',
}}>
<Picker.Item label="Currency" value="accNum" />
<Picker.Item label="NGN" value="NGN" />
</Picker>
}
</View>
<TouchableOpacity
onPress={() => {
setModalVisible(true);
}}
style={styles.button}>
<Text style={styles.loginbtn}> Transfer </Text>
</TouchableOpacity>
<Modal
hasBackdrop={true}
backdropOpacity={0.2}
backdropColor="black"
transparent
visible={modalVisible}
onRequestClose={() => setModalVisible(false)}>
<View style={styles.modal}>
<Text>Hello From Modal</Text>
<TouchableOpacity>
<Text>Modal! Modal!</Text>
</TouchableOpacity>
</View>
</Modal>
</ScrollView>
</View>
);
};
export default LocalPayments;
const styles = StyleSheet.create({
container: {
paddingTop: 40,
flex: 1,
flexDirection: 'row',
justifyContent: 'space-between',
},
modal: {
top: '50%',
height: '50%',
backgroundColor:'#fff'
},
accText: {
top: -50,
paddingTop: 10,
justifyContent: 'center',
alignItems: 'center',
fontFamily: 'Poppins-Medium',
fontSize: 12,
color: 'white',
textAlign: 'center',
},
text: {
top: -50,
fontSize: 20,
color: 'white',
textAlign: 'center',
fontFamily: 'Poppins-Bold',
},
input: {
top: 10,
width: 300,
height: 55,
margin: 10,
fontSize: 12,
borderColor: '#00BB23',
fontFamily: 'Poppins-Bold',
borderRadius: 5,
borderWidth: 1,
marginBottom: 30,
},
button: {
marginTop: 40,
width: 150,
height: 50,
padding: 10,
borderRadius: 10,
backgroundColor: '#00BB23',
alignItems: 'center',
},
Regbutton: {
width: 150,
height: 52,
padding: 10,
borderRadius: 10,
backgroundColor: '#ffffff',
alignItems: 'center',
borderWidth: 2,
borderColor: '#030303',
},
loginbtn: {
color: '#ffff',
fontSize: 15,
fontFamily: 'Poppins-Medium',
},
AccountBalance: {
fontSize: 13,
fontWeight: 'bold',
textAlign: 'left',
},
loginbtn2: {
color: '#030303',
fontSize: 20,
fontWeight: 'bold',
},
logo: {
width: 150,
height: 150,
},
space: {
top: 10,
width: 10,
height: 20,
},
space2: {
width: 10,
height: 10,
},
imageStyle: {
flexDirection: 'row',
justifyContent: 'center',
padding: 5,
margin: 2,
height: 15,
width: 15,
resizeMode: 'stretch',
marginBottom: 8,
marginTop: 8,
alignItems: 'center',
},
});
Been trying to get this resolved without any form of help. Please help is needed here.
Try the following:
Create this separate file: BackgroundOpacity.js
import React, {memo} from 'react';
import {Platform, View} from 'react-native';
const BackgroundOpacity = ({display}) => {
return (
<View
style={{
display: Platform.OS === 'ios' ? 'none' : display ? 'flex' : 'none'
flex: 1,
position: 'absolute',
top: 0,
height: '200%'
backgroundColor: 'black',
opacity: 0.35,
left: 0,
right: 0,
top: 0,
marginTop: -200,
zIndex: 3
}}
/>
);
};
export default memo(BackgroundOpacity);
Then implement it like this:
.... some imports
const LocalPayments = () => {
const navigation = useNavigation();
const [modalVisible, setModalVisible] = useState(false);
return (
<View style={styles.container}>
<BackgroundOpacity
display={
Platform.OS === 'ios'
? false
: modalVisible
}
/>
<View style={styles.space} />
<ScrollView
contentContainerStyle={{
justifyContent: 'space-between',
alignItems: 'center',
}}>
... Rest of your code
So we created a reusable component that can be added anywhere when you have a modal. But the question is, why are we checking that it's always an android device when we use this backgroundOpacity?
Cause for some reasons, on iOS, whenever you have an opened modal, it will automatically add a background opacity on its own.
so I'm fairly new to react-native. I'm trying to implement a carousel with react-native-swiper.
Issue -
I want to set the carousel height to 150px, for this I set the property height to 150px, with this the carousel height got changed to 150px but when I try to render a text component below carousel, it is not rendering just below the carousel.
import React from 'react';
import { View, Text, StyleSheet } from "react-native";
import Swiper from 'react-native-swiper';
const styles = StyleSheet.create({
wrapper: { backgroundColor: "black", height: 150 },
slide1: {
height: 150,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#9DD6EB'
},
slide2: {
height: 150,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#97CAE5'
},
slide3: {
height: 150,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#92BBD9'
},
text: {
color: '#fff',
fontSize: 30,
fontWeight: 'bold'
}
})
const HomeScreen_ = () => {
return (
<>
<Swiper
style={styles.wrapper}
height={150}
showsButtons
autoplay
paginationStyle={{ height: 8, position: 'absolute', top: 130 }}
activeDot={
<View
style={{
backgroundColor: '#c3383833', width: 8,
height: 8, borderRadius: 4, marginLeft: 3,
marginRight: 3, marginTop: 3, marginBottom: 3
}} />
}>
<View style={styles.slide1}>
<Text style={styles.text}>Hello Swiper</Text>
</View>
<View style={styles.slide2}>
<Text style={styles.text}>Beautiful</Text>
</View>
<View style={styles.slide3}>
<Text style={styles.text}>And simple</Text>
</View>
</Swiper >
<Text style={{ height: 100, color: "black", }}>Just a Random Text</Text>
</>
)
};
export default HomeScreen_;
you can check this sample, in React Native, we have covered any component using View
import React from 'react';
import {View, Text, StyleSheet, SafeAreaView} from 'react-native';
import Swiper from 'react-native-swiper';
const SwiperExample = () => {
return (
<>
<View style={{flex: 0.3}}> // here
<Swiper
style={styles.wrapper}
showsButtons
autoplay
paginationStyle={{height: 8, position: 'absolute', top: 130}}
activeDot={
<View
style={{
backgroundColor: '#c3383833',
width: 8,
height: 8,
borderRadius: 4,
marginLeft: 3,
marginRight: 3,
marginTop: 3,
marginBottom: 3,
}}
/>
}>
<View style={styles.slide1}>
<Text style={styles.text}>Hello Swiper</Text>
</View>
<View style={styles.slide2}>
<Text style={styles.text}>Beautiful</Text>
</View>
<View style={styles.slide3}>
<Text style={styles.text}>And simple</Text>
</View>
</Swiper>
</View>
<Text style={{height: 100, color: 'black'}}>Just a Random Text</Text>
</>
);
};
const styles = StyleSheet.create({
wrapper: {backgroundColor: 'black', flex: 1},
slide1: {
height: 150,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#9DD6EB',
},
slide2: {
height: 150,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#97CAE5',
},
slide3: {
height: 150,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#92BBD9',
},
text: {
color: '#fff',
fontSize: 30,
fontWeight: 'bold',
},
});
export default SwiperExample;
I solved this issue by removing the height property in the wrapper object of StyleSheet and passing containerStyle={{ height: 150, flex: 0 }} as a prop in Swiper
I leave a small code of sample of as I have my code, in fact my code I show it inside a FlatList... but here I leave him something similar in .map that is practically the same thing, they are dynamic Views, I wish that on having clicked in the button, that View change Styles (Position Abosulte, Width, Backgorund color and more)
import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';
import Constants from 'expo-constants';
import AssetExample from './components/AssetExample';
let shopping = ['bread', 'milk', 'cheese', 'hummus', 'noodles'];
export default function App() {
return (
<View style={styles.container}>
{shopping.map((data, idx) => {
return (
<View
style={{
width: '100%',
height: 70,
backgroundColor: '#f7f7f7',
marginTop: 5,
marginBottom: 5,
borderColor: '#dfdfdf',
borderWidth: 1,
alignItems: 'center',
}}>
<Text>Tex: {data}</Text>
//I press this buttom, i want change this View Styles!!!, only this view
<TouchableOpacity onPress={() => {}}>
<View
style={{
width: '100%',
height: 30,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#f7f7f7',
marginTop: 5,
marginBottom: 5,
borderColor: '#dfdfdf',
borderWidth: 1,
borderRadius: 15,
}}>
<Text style={{ width: '100%' }}>Change</Text>
</View>
</TouchableOpacity>
</View>
);
})}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
});
You can have a state variable and then set it to change the style.
<TouchableOpacity onPress={() => this.setState({changeStyleIndex:index})}>
<View
style={this.state.changeStyleIndex != index{
width: '100%',
height: 30,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#f7f7f7',
marginTop: 5,
marginBottom: 5,
borderColor: '#dfdfdf',
borderWidth: 1,
borderRadius: 15,
}:{change style goes here}}>
<Text style={{ width: '100%' }}>Change</Text>
</View>
</TouchableOpacity>
import React from 'react'
import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';
import Constants from 'expo-constants';
let shopping = ['bread', 'milk', 'cheese', 'hummus', 'noodles'];
export default class App extends React.Component {
constructor(props) {
super(props)
this.state = {
BackColor = '#f7f7f7',
StateWidth: '100%'
}
}
HandleChange = () => {
this.setState({
BackColor: 'red',
StateWidth: '50%'
})
}
render() {
const { BackColor, StateWidth } = this.state
return (
<View style={styles.container}>
{shopping.map((data, idx) => {
return (
<View
style={{
width: { StateWidth },
height: 70,
backgroundColor: { BackColor },
marginTop: 5,
marginBottom: 5,
borderColor: '#dfdfdf',
borderWidth: 1,
alignItems: 'center',
}}>
<Text>Tex: {data}</Text>
//I press this buttom, i want change this View Styles!!!, only this view
<TouchableOpacity onPress={() => { this.HandleChange }}>
<View
style={{
width: '100%',
height: 30,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#f7f7f7',
marginTop: 5,
marginBottom: 5,
borderColor: '#dfdfdf',
borderWidth: 1,
borderRadius: 15,
}}>
<Text style={{ width: '100%' }}>Change</Text>
</View>
</TouchableOpacity>
</View>
);
})}
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
I used the component by class, but change it to function and it will give crt, the logic is basically this.
Problem:
I have created a react application. In there I have created a page header in my app which is common to all my components and the title of the Header will be change accordingly.
This image shows what my code does.
What I want to achieve is this.
This is my code of that Page header.
/* eslint-disable react-native/no-inline-styles */
import React, {Component} from 'react';
import {StyleSheet, View, Text, Image} from 'react-native';
import LinearGradient from 'react-native-linear-gradient';
class PageHeader extends Component {
render() {
return (
<View style={{zIndex: 0}}>
<LinearGradient
colors={['#fdc830', '#ff9a00']}
style={{alignItems: 'center', borderBottomLeftRadius: 80}}>
<View
// eslint-disable-next-line react-native/no-inline-styles
style={{
flexDirection: 'row',
marginTop: '10%',
marginBottom: '5%',
paddingTop: '5%',
}}>
<Text
style={{
fontSize: 18,
fontWeight: 'bold',
fontStyle: 'normal',
fontFamily: 'sans-serif',
letterSpacing: 0.81,
color: '#000104',
marginTop: '2%',
marginLeft: this.props.marginLeft,
marginRight: '2%',
}}>
{this.props.title}
</Text>
<Image
style={{
width: 20,
height: 10,
marginTop: this.props.marginTop,
}}
source={this.props.image}
/>
</View>
</LinearGradient>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
top: 0,
flex: 3,
},
homeHeader: {
flexDirection: 'column',
flex: 1,
},
homeHeaderImage: {
flexDirection: 'row',
},
headerText: {
fontSize: 18,
fontWeight: 'bold',
fontStyle: 'normal',
fontFamily: 'sans-serif',
letterSpacing: 0.81,
color: '#000104',
marginTop: '2%',
marginLeft: '30%',
marginRight: '3%',
},
hederContentContainer: {
flexDirection: 'row',
marginTop: '30%',
marginBottom: '10%',
},
qrCodeGeneraterContainer: {
alignItems: 'center',
justifyContent: 'center',
},
viewDetails: {
color: '#ff9a00',
fontSize: 12,
marginLeft: '75%',
fontWeight: 'bold',
marginTop: '2%',
marginBottom: '1%',
},
});
export default PageHeader;
This is how I am using this component in my home component.
<PageHeader
title="Settings"
image={require('../../../assets/settings.png')}
width={DEVICE_WIDTH / 14}
height={DEVICE_HEIGHT / 24}
marginTop="2%"
marginLeft="40%"
/>
What wrong with my code is it not showing the image fully. I tried so many ways to get the image full but I was unable to do so. And It shows different difference sizes on different device sizes as I have given the width based on the device with. Can someone help me to solve these two issues? Thank you.
Try this in your image component :
<Image
style={{
width: 20,
height: 20,
marginTop: this.props.marginTop,
resizeMode="contain"
}}
source={this.props.image}
/>
Hope it helps. feel free for doubts
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")