I want to simply change the color of the button, but i can't. I tried to change directly in the button, and pass a style to it. But neither of them worked. Here's my very simple code.
export default class Dots extends Component { render() { return ( <Image style={styles.container} source={require('./background3.png')}> <Button title='play' style = {{color:'red'}}/> </Image> ); } } const styles = StyleSheet.create({ container: { flex:1, backgroundColor:'transparent', resizeMode:'cover', justifyContent:'center', alignItems:'center', width:null, height:null }, button:{ backgroundColor:'#ff5c5c', } });
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.
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.
To set text color in React, we can set the style prop to an object with the color property. to set the style prop of the h1 element to an object that has the color property set to 'red' . Now we should see that the color of the text is red.
Type background-color: in the quotation marks after "style=". This element is used to change the background color of the button. Type a color name or hexadecimal code after "background-color:". You can type name of a color (i.e, blue) or a hexadecimal color.
The react Button
component renders the native button on each platform it uses. Because of this, it does not respond to the style
prop. It has its own set of props.
The correct way to use it would have been
<Button color="#ff5c5c" title="I'm a button!" />
You can see the documentation at https://facebook.github.io/react-native/docs/button.html
Now, say you do want to make super customizable button, for that you'll have to use views and touchable opacity. Something along the lines of this.
<TouchableOpacity onPress={...}> {... button markup} </TouchableOpacity>
You'll wrap that up in your own button component and use it.
I think it is definitely better to use TouchableOpacity instead of Button tag as Button is creating styling discrepancies in Android and iOS platforms .
You can use the code below and it looks very similar to a button and it acts like one:
<TouchableOpacity style={styles.button} onPress={this.onPress} > <Text> Touch Here </Text> </TouchableOpacity> const styles = StyleSheet.create({ button: { alignItems: 'center', backgroundColor: '#DDDDDD', padding: 10 } })
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