I am trying to change the background color of my button but nothing seems to work, I tried the raised property, maybe i am using it incorrectly. Do any of you have any ideas?
import React from 'react'; import { StyleSheet, Text, View, Button, TouchableHighlight } from 'react-native'; export default class App extends React.Component { state={ name: "Mamadou" }; myPress = () => { this.setState({ name: "Coulibaly" }); }; render() { return ( <View style={styles.container}> <Button title={this.state.name} color="red" onPress={this.myPress} /> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, });
To change background color of React Native button, we can set the color prop for Android and we set the backgroundColor for iOS. to add <Button title="Login Android" color="#1E6738" /> add a button with the background color set to #1E6738 for Android.
To change the style of a button on click with React, we can set the className prop to an object with styles controlled by states. We have the red and green classes with the color CSS property set to red and green respectively.
import { View, Button, StyleSheet, TouchableOpacity, Text } from "react-native"; To create custom buttons, you need to customize the <TouchableOpacity /> component and include the <Text /> component inside of it to display the button text.
You can use a Button from react-native like this and can also pass the color prop. For complex and custom buttons you can create it by your own styles using Touchables which are given by react-native you don't have to use any third-party libraries for that. The first code doesn't change the text colour.
Use this for adding the background color to the button in iOS:
Styles:
loginScreenButton:{ marginRight:40, marginLeft:40, marginTop:10, paddingTop:10, paddingBottom:10, backgroundColor:'#1E6738', borderRadius:10, borderWidth: 1, borderColor: '#fff' }, loginText:{ color:'#fff', textAlign:'center', paddingLeft : 10, paddingRight : 10 }
Button:
<TouchableOpacity style={styles.loginScreenButton} onPress={() => navigate('HomeScreen')} underlayColor='#fff'> <Text style={styles.loginText}>Login</Text> </TouchableOpacity>
For Android it simply works with Button color property only:
<Button onPress={onPressAction} title="Login" color="#1E6738" accessibilityLabel="Learn more about this button" />
I suggest to use React Native Elements package, which allow to insert styles throught buttonStyle
property.
styles:
const styles = StyleSheet.create({ container: { flex: 1 }, button: { backgroundColor: '#00aeef', borderColor: 'red', borderWidth: 5, borderRadius: 15 } })
render()
render() { return ( <View style={styles.container}> <Button buttonStyle={styles.button} title="Example" onPress={() => {}}/> </View> ) }
Effect:
Expo Snack: https://snack.expo.io/Bk3zI0u0G
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With