Make Swipe Button in React Native for Android and iOS

React Native Swipe Button

This is an Example to Make Swipe Button in React Native for Android and iOS. The swipe button is a very interesting and attractive feature for submitting the form or any other thing which needs confirmation. We can use this type of button for the choice section as we do in the case of the switch or we can use it where we want the user to be sure about the action.

In some cases, the user says he/she has accidentally clicked on the button which can create a problem in a very sensitive case so to stop that I personally like to provide a swipeable button so that users have to put some effort and it can not be done accidentally. If you have used the iPhone then you have seen the same Swipeable button while switching off the device.

For this example, we are using SwipeButton component provided by rn-swipe-button library.

To Make a Swipe Button

<SwipeButton
  disabled={false}
  //disable the button by doing true (Optional)
  swipeSuccessThreshold={70}
  height={45}
  //height of the button (Optional)
  width={330}
  //width of the button (Optional)
  title="Swipe to Submit"
  //Text inside the button (Optional)
  //thumbIconImageSource={thumbIcon}
  //You can also set your own icon for the button (Optional)
  onSwipeSuccess={() => {
    alert('Submitted Successfully!');
  }}
  //After the completion of swipe (Optional)
  railFillBackgroundColor="#e688a1" //(Optional)
  railFillBorderColor="#e688ff" //(Optional)
  thumbIconBackgroundColor="#ed9a73" //(Optional)
  thumbIconBorderColor="#ed9aff" //(Optional)
  railBackgroundColor="#bbeaa6" //(Optional)
  railBorderColor="#bbeaff" //(Optional)
/>

You can see here for the more customizable props.

In this example, we will make a swipeable button that has a success threshold =70 and has a custom title with custom properties like background color, border color, thumb icon background color, and thumb icon border-color. After swiping the button it will trigger onSwipeSuccess where you can add your to-do (We are showing alert only). So let’s get started with the example.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react native command line interface to make our React Native App.

If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:

npm uninstall -g react-native-cli @react-native-community/cli

Run the following commands to create a new React Native project

npx react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

npx react-native init ProjectName --version X.XX.X

Note If the above command is failing, you may have old version of react-native or react-native-cli installed globally on your pc. Try uninstalling the cli and run the cli using npx.

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependency

To use SwipeButton we need to install rn-swipe-button dependency.

To install this open the terminal and jump into your project using

cd ProjectName

Run the following command to install

npm install rn-swipe-button --save

This command will copy all the dependencies into your node_module directory.

Code to make a swipeable button

Now Open App.js in any code editor and replace the code with the following code

App.js

// Make Swipe Button in React Native for Android and iOS
// https://aboutreact.com/make-swipe-button-in-react-native-for-android-and-ios/

// import React in our code
import React from 'react';

// import all the components we are going to use
import {
  SafeAreaView,
  View,
  Text,
  StyleSheet
} from 'react-native';

import SwipeButton from 'rn-swipe-button';

const App = () => {
  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.container}>
        <Text style={styles.titleStyle}>
          Example of React Native Swipe Button
        </Text>
        <SwipeButton
          disabled={false}
          //disable the button by doing true (Optional)
          swipeSuccessThreshold={70}
          height={45}
          //height of the button (Optional)
          width={330}
          //width of the button (Optional)
          title="Swipe to Submit"
          //Text inside the button (Optional)
          //thumbIconImageSource={thumbIcon}
          //You can also set your own icon (Optional)
          onSwipeSuccess={() => {
            alert('Submitted Successfully!');
          }}
          //After the completion of swipe (Optional)
          railFillBackgroundColor="#e688a1" //(Optional)
          railFillBorderColor="#e688ff" //(Optional)
          thumbIconBackgroundColor="#ed9a73" //(Optional)
          thumbIconBorderColor="#ed9aff" //(Optional)
          railBackgroundColor="#bbeaa6" //(Optional)
          railBorderColor="#bbeaff" //(Optional)
        />
      </View>
    </SafeAreaView>
  );
};
export default App;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'white',
    padding: 10,
  },
  titleStyle: {
    fontSize: 28,
    fontWeight: 'bold',
    textAlign: 'center',
    padding: 10,
  },
});

To Run the React Native App

Open the terminal again and jump into your project using.

cd ProjectName

1. Start Metro Bundler

First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:

npx react-native start

Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.

2. Start React Native Application

To run the project on an Android Virtual Device or on real debugging device:

npx react-native run-android

or on the iOS Simulator by running (macOS only)

npx react-native run-ios

Output Screenshots

Image   Image   Image   Image

Output in Online Emulator


This is how you can make a swipe button in React Native for Android and iOS. If you have any doubts or want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.