change button color react native
Asked Answered
G

4

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

}); 
Gilead answered 20/1, 2017 at 1:9 Comment(0)
A
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.

Andriaandriana answered 20/1, 2017 at 1:22 Comment(2)
color='#ff5c5c' change background color in android. How can I change text color and font size?Sliver
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.Xerophthalmia
H
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
  }
})
Handlebar answered 17/9, 2019 at 13:39 Comment(1)
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?Vertebral
F
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

Frump answered 13/10, 2019 at 5:9 Comment(0)
C
-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'

    }
})
Chauffer answered 21/6, 2022 at 14:4 Comment(1)
This is not working for me. If it works for you, it looks to be not compatible with all versionsWicker

© 2022 - 2024 — McMap. All rights reserved.