React Native Toast – Toast Alert for Android

React Native Toast

This is an example of How to Show Toast Message in React Native for Android Only. React Native Toast is a component that is related to Android only and can be used to display information for a short period of time. A Toast contains the message to be displayed quickly and disappears after some time.

React Native Toast is only for those who are targeting to Android platform only IOS doesn’t support Toast.

To Import ToastAndroid in Code

import { ToastAndroid} from 'react-native'

Render Using

    1. Simple Toast
      ToastAndroid.show(message, duration)
    2. Show with gravity
      ToastAndroid.showWithGravity(message, duration, gravity)
    3. With gravity and offset
      ToastAndroid.showWithGravityAndOffset(
        message, duration, gravity, xOffset, yOffset
      )

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 init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

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

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

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

Code

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

App.js

// React Native Toast – Toast Alert for Android
// https://aboutreact.com/react-native-toast-android-only/

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

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

const App = () => {
  const toastWithDurationHandler = () => {
    // To make Toast with duration
    ToastAndroid.show(
      'Hi I am Simple Toast', ToastAndroid.SHORT
    );
  };

  const toastWithDurationGravityHandler = () => {
    // To make Toast with
    // * Duration
    // * Gravity
    ToastAndroid.showWithGravity(
      'Hi I am Toast with center gravity',
      ToastAndroid.SHORT, //can be SHORT, LONG
      ToastAndroid.CENTER, //can be TOP, BOTTON, CENTER
    );
  };

  const toastWithDurationGravityOffsetHandler = () => {
    // To make Toast With
    // * Duration
    // * Gravity
    // * Offset
    ToastAndroid.showWithGravityAndOffset(
      'Hi I am Toast with garavity and offset',
      ToastAndroid.LONG, //can be SHORT, LONG
      ToastAndroid.BOTTOM, //can be TOP, BOTTON, CENTER
      25, //xOffset
      500, //yOffset
    );
  };

  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.container}>
        <Text style={styles.titleStyle}>
          React Native Toast – Toast Alert for Android
        </Text>
        {/*To generate Toast With Duration*/}
        <TouchableHighlight
          style={styles.buttonStyle}
          onPress={toastWithDurationHandler}>
          <Text style={styles.buttonTextStyle}>
            Generate Toast With Duration
          </Text>
        </TouchableHighlight>

        {/*To generate Toast With Duration And Gravity*/}
        <TouchableHighlight
          style={styles.buttonStyle}
          onPress={toastWithDurationGravityHandler}>
          <Text style={styles.buttonTextStyle}>
            Generate Toast With Duration AND Gravity
          </Text>
        </TouchableHighlight>

        {/*To generate Toast With Duration, Gravity And Offset*/}
        <TouchableHighlight
          style={styles.buttonStyle}
          onPress={toastWithDurationGravityOffsetHandler}>
          <Text style={styles.buttonTextStyle}>
            Generate Toast With Duration, Gravity And Offset
          </Text>
        </TouchableHighlight>
      </View>
    </SafeAreaView>
  );
};
export default App;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: '#307ecc',
    padding: 16,
  },
  buttonStyle: {
    minWidth: '100%',
    padding: 10,
    backgroundColor: '#f5821f',
    margin: 15,
  },
  buttonTextStyle: {
    color: 'white',
    textAlign: 'center',
  },
  titleStyle: {
    color: 'white',
    textAlign: 'center',
    fontSize: 20,
    marginTop: 10,
  },
});

To Run the React Native App

Open the terminal again and jump into your project using.

cd ProjectName

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

react-native run-android

or on the iOS Simulator by running (macOS only)

react-native run-ios

Download Source Code

Output Screenshot

toast_example2   toast_example3   toast_example4

Output in Online Emulator

That was the React Native Toast (For Android only). If you have any doubts or you want to share something about the topic you can comment below or contact us here. The remaining components will be covered in the next article. Stay tuned!

Hope you liked it. 🙂

2 thoughts on “React Native Toast – Toast Alert for Android”

Leave a Comment

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