Skip to content

All topics  /  React Native

React Native Material switch Example

React Native ·

Hi Guys,

In this blog, I will explain you how to create material ui switch in react native. You can easily create material ui switch in react native. First i will import switch namespace from react-native-paper, after I will make material ui switch using in react native.

Here, I will give you full example for simply display material ui switch using react native as bellow.

Step 1 - Create project


In the first step Run the following command for create project.

expo init MaterialuiSwitch 

Step 2 - Install Package

In the step,I will install npm i react-native-paper package .

npm i react-native-paper

Step 3 - App.js

In this step, You will open App.js file and put the code.

import * as React from 'react';
import { StyleSheet } from 'react-native';
import { Switch } from 'react-native-paper';
const SwitchExample = () => {
  const [isSwitchOn, setIsSwitchOn] = React.useState(false);
  const onToggleSwitch = () => {
    if (isSwitchOn == true) {
      alert('Switch On');
    }else{
      alert('Switch Off');
    }
    setIsSwitchOn(!isSwitchOn);
  }
  return <Switch value={isSwitchOn} onValueChange={onToggleSwitch} style={styles.switch} color="green">
};
export default SwitchExample;
const styles = StyleSheet.create ({
  switch: {
    flexDirection: 'column',
    justifyContent: 'center',
    alignItems: 'center',
    marginTop: 250,
    marginLeft: 160,
    width: 50,
    height: 70,
  }
})

Step 4 - Run project

In the last step run your project using bellow command.

npm start

Output

It will help you...