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 targetting to Android platform only IOS doesn’t support Toast.

To Import ToastAndroid in Code

import { ToastAndroid} from 'react-native'

Render Using

ToastAndroid.show(message, duration)
ToastAndroid.showWithGravity(message, duration, gravity)
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

//This is an example code to understand Toast// 

import React, { Component } from 'react';
//import react in our code. 

import { ToastAndroid, Alert, Button, View, StyleSheet } from 'react-native';
//import all the components we are going to use. 

export default class App extends Component {
  _toastWithDurationHandler=()=>{
    //function to make Toast With Duration
    ToastAndroid.show('Hi I am Simple Toast', ToastAndroid.SHORT);
  }
  _toastWithDurationGravityHandler=()=>{
    //function to make Toast With Duration And Gravity
   ToastAndroid.showWithGravity(
      'Hi I am Toast with center gravity',
      ToastAndroid.SHORT, //can be SHORT, LONG
      ToastAndroid.CENTER //can be TOP, BOTTON, CENTER
    );
  }
  _toastWithDurationGravityOffsetHandler=()=>{
    //function to make Toast With Duration, Gravity And 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
      50 //yOffset
    );
  }
  render() {
    return (
      <View style={styles.container}>

        {/*To generate Toast With Duration*/}
        <Button 
           title='Generate Toast With Duration' 
           onPress={this._toastWithDurationHandler}
        />
      
        {/*To generate Toast With Duration And Gravity*/}
        <Button 
           title='Generate Toast With Duration AND Gravity' 
           onPress={this._toastWithDurationGravityHandler}
        />
      
        {/*To generate Toast With Duration, Gravity And Offset*/}
        <Button 
           title='Generate Toast With Duration, Gravity And Offset' 
           onPress={this._toastWithDurationGravityOffsetHandler}
         />
      
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: '#ecf0f1',
  },

});

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
react-native run-ios (macOS only).

Output Screenshot

      

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. 🙂


Related Posts

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.