Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change button color react native

Tags:

reactjs

native

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',   }  });  
like image 866
Gisa Avatar asked Jan 20 '17 01:01

Gisa


People also ask

How do you change color of a button in React Native?

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.

How do you customize a button in React Native?

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.

How do I change the text color in React Native?

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.

How do you change the color of a button in HTML?

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.


2 Answers

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.

like image 183
Kevin Velasco Avatar answered Oct 03 '22 01:10

Kevin Velasco


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   } }) 
like image 38
Luchian Chivoiu Avatar answered Oct 03 '22 01:10

Luchian Chivoiu