47

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',
  }

}); 
Gisa
  • 473
  • 1
  • 4
  • 8

4 Answers4

64

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.

choster
  • 130
  • 1
  • 7
Kevin Velasco
  • 4,568
  • 1
  • 16
  • 6
  • 8
    color='#ff5c5c' change background color in android. How can I change text color and font size? – Amir Shabani Jan 07 '18 at 07:45
  • 13
    There's a problem here. Android takes the "color=" and makes it the button background color (overriding backgroundColor: in the stylesheet). While, iOS takes the "color=" and makes it the text color and uses the background color appropriately in the style sheet. This makes it very difficult to have a single code set handle all buttons correctly. – David Urry Mar 20 '18 at 01:21
12

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
  }
})
Luchian Chivoiu
  • 300
  • 4
  • 14
  • i'm actually doing this but i still am forced to give the Text its own color... even though the TouchableOpacity has its own class with its own color. Is there even a way to inherit? Like if TouchableOpacity has color: purple.. then all text nodes under will also? – carinlynchin Apr 27 '20 at 20:16
4

Yes button does not respond to styles. But alternative solution is that we can use react-native-element component.

First install the packages which is mentioned below

npm install react-native-elements npm i react-native-linear-gradient npm i react-native-vector-icons

And then import the packages to your code

    import { Button } from 'react-native-elements';
    import LinearGradient from 'react-native-linear-gradient';
    import Icon from 'react-native-vector-icons/FontAwesome';

    <Button ViewComponent={LinearGradient} // Don't forget this!
    linearGradientProps={{
    colors: ['#ffffff','blue', 'grey'],
    start: { x: 0, y: 0.5 },
    end: { x: 1, y: 0.5 },
    }} onPress={()=> this.props.navigation.navigate('First')} title="Click me"/>

For more info here is the link: https://react-native-elements.github.io/react-native-elements/docs/button.html

Aaron Ebinezer
  • 129
  • 2
  • 7
-1

You can make it like this

<Button title="Hello World!" buttonStyle={styles.btnstylehere}/>

 const styles = StyleSheet.create({
 btnstylehere:{
        width: 75,
        height: 75,
        borderRadius: 0,
        backgroundColor: '#000',
        color: '#fff'

    }
})
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Manuel
  • 1
  • 1