Change color of Switch while turning OFF react-native

30,461

Solution 1

onTintColor has been deprecated try the following.

  <Switch 
      trackColor={{true: 'red', false: 'grey'}}
      onValueChange={this.toggleSwitch}
      value={true}/>

this works

Solution 2

I've design switch with platform specific, also used border based on On Off status.

<Switch
trackColor={{ true: '#7ab8e1', false: Platform.OS=='android'?'#d3d3d3':'#fbfbfb'  }}
thumbColor={[Platform.OS=='ios'?'#FFFFFF':(item.status ?'#7ab8e1':'#ffffff')]}
ios_backgroundColor="#fbfbfb"
style={[item.status ?inline_styles.switchEnableBorder:inline_styles.switchDisableBorder]}
value={item.status}
onValueChange={() =>this.change_status(index)            }
/>

Inline border Style

const inline_styles = StyleSheet.create({
switchEnableBorder: {
borderColor: '#6fa6d3',
borderWidth: 1},

switchDisableBorder: {
borderColor: '#f2f2f2',
borderWidth: 1,  },});

Android OutPut :Android iOS Output : iOS

Solution 3

Just add:

style={{backgroundColor: '#FF0000', borderRadius: 17}}

To the Switch

Solution 4

React native has ColorSwitchExample in their docs. You can refer the same here. Good luck!

class ColorSwitchExample extends React.Component {
  state = {
    colorTrueSwitchIsOn: true,
    colorFalseSwitchIsOn: false,
  };

  render() {
    return (
      <View>
        <Switch
          onValueChange={(value) => this.setState({colorFalseSwitchIsOn: value})}
          onTintColor="#00ff00"
          style={{marginBottom: 10}}
          thumbTintColor="#0000ff"
          tintColor="#ff0000"
          value={this.state.colorFalseSwitchIsOn} />
        <Switch
          onValueChange={(value) => this.setState({colorTrueSwitchIsOn: value})}
          onTintColor="#00ff00"
          thumbTintColor="#0000ff"
          tintColor="#ff0000"
          value={this.state.colorTrueSwitchIsOn} />
      </View>
    );
  }
}

Solution 5

For anyone coming from Google: Specifically for iOS, you'll need to use the ios_backgroundColor property:

<Switch ios_backgroundColor="red" trackColor={{ true: 'blue', false: 'red' }} />

References:

https://facebook.github.io/react-native/docs/switch.html#trackcolor

https://facebook.github.io/react-native/docs/switch#ios-backgroundcolor

Share:
30,461
Ankush Rishi
Author by

Ankush Rishi

Full Stack Developer

Updated on July 27, 2022

Comments

  • Ankush Rishi
    Ankush Rishi over 1 year

    I am using react native switch component and I want to change the color of switch when I turn it OFF.

    I can add onTintColor property to change the color when it is turned on.

    Is there any way to change the color while turning it OFF?

  • Rajeev Upadhyay
    Rajeev Upadhyay over 7 years
    @Ankush Rishi I don't think you can change the appearance of the switch when it is off but you change the strip color with tintColor and change circle color with thumbTintColor.
  • Mihir
    Mihir over 7 years
    @RajeevUpadhyay Did you run the example on their page? I can see the switch changing color after turning it off! Or am I missing something?
  • Ankush Rishi
    Ankush Rishi over 7 years
    @Mihir I want to change the background color, thumbTintColor will change the color of the slider only and tintColor will change the border color. Just like onTintColor is what I want when the switch is in Off mode
  • Made in Moon
    Made in Moon over 4 years
    On Android (react-native 0.61.5), it will give a green thumb-circle (which is bad)
  • twelve17
    twelve17 almost 4 years
    It's wild that Switch allows independent on/off colors for the trackColor, but not for the thumbColor, even though the thumb will have two different colors for on/off if you do not specify a custom color.
  • jasie
    jasie over 3 years
    Please explain how your answer solves the problem. See How To Answer
  • Menon Hasan
    Menon Hasan over 3 years
    when turning off in trackColor object false property trigger and color is chaged
  • jasie
    jasie over 3 years
    Ha thanks, the explanation should go into your answer - just edit it :-)