React Native Toast

Introduction: It is a message that will be displayed on the screen for a short span of time to interrupt the user. Primarily it is displayed by any user action. Toast can be displayed to indicate any operation's failure or success or for warning purposes. After some time, toast disappears automatically. Only android supports this feature. The toast feature is not available for IOS.

It generally takes two parameters, message, and duration, as- shown (message, duration)

Message: A string of words will be displayed as text. Whatever would be passed as a message will be displayed on our screen as toast, and it will disappear after some time.

Duration: it decides how long the toast will stay on screen, but we cannot decide by passing any specific time. We can do this by ToastAndroid.SHORT or ToastAndroid.LONG method.

Severity: We can use severity to decide which part of the screen toast will appear. It can be passed along with message and duration as  (message, duration, severity).there can be three possible portions where toast can be displayed top, bottom, or center. We can specify this using ToastAndroid.TOP, ToastAndroid.BOTTOM or ToastAndroid.CENTER.

Importing toast in react native app:

You can import ToastAndroid as - import { ToastAndroid} from 'react-native' .

Types of toast methods-

Generally, there are three types of toast methods

Show: Here we can only specify the message and duration.

Show With Gravity: Here we can specify severity along with the message and duration.

Show With Gravity and Offset: Here we can specify the x and y-axis along with the message, duration, and severity.

Let's learn toast with a practical example :

App.js code:

import React from 'react';
import {
  SafeAreaView,
  Text,
  View,
   TouchableHighlight,
  StyleSheet,
   ToastAndroid,
} from 'react-native';


const App = () => {
  const toast_With_Duration_Handler = () => {
    ToastAndroid.show(
      'Hi I am Simple Toast nice to see you', ToastAndroid.LONG
    );
  };
 
  const toast_With_Duration_Gravity_Handler = () => {
    ToastAndroid.showWithGravity(
      'Hi I am center gravity Toast nice to see you',
      ToastAndroid.CENTER, 
      ToastAndroid.CENTER, 
    );
  };
 
  const toast_With_Duration_Gravity_Offset_Handler = () => {
  
    ToastAndroid.showWithGravityAndOffset(
      'Hi I am Toast with garavity and offset nice to see you',
      ToastAndroid.LONG, 
      ToastAndroid.BOTTOM, 
      25, 
      500,
    );
  };
 
  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.container}>
        <Text style={styles.titleStyle}>
        Example of  React Native Toast 
        </Text>
        <TouchableHighlight
          style={styles.buttonStyle}
          onPress={toast_With_Duration_Handler}>
          <Text style={styles.buttonTextStyle}>
            Generate Toast number 1
          </Text>
        </TouchableHighlight>
 
        <TouchableHighlight
          style={styles.buttonStyle}
          onPress={toast_With_Duration_Gravity_Handler}>
          <Text style={styles.button_TextStyle}>
            Generate Toast number 2
          </Text>
        </TouchableHighlight>
 
        <TouchableHighlight
          style={styles.buttonStyle}
          onPress={toast_With_Duration_Gravity_Offset_Handler}>
          <Text style={styles.button_TextStyle}>
            Generate Toast number 3
          </Text>
        </TouchableHighlight>
      </View>
    </SafeAreaView>
  );
};
export default App;
 
const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: 'gray',
    padding: 25,
  },
  buttonStyle: {


flexDirection: 'row',
flex:1,
width:80,
height: 1,
alignItems: 'bottom',
borderRadius: 25,
borderWidth: 10,
borderColor: 'pink',
backgroundColor: '#2bd64d',
},
  
  button_TextStyle: {
    color: 'white',
    textAlign: 'center',
  },
  titleStyle: {
    color: 'black',
    textAlign: 'center',
    fontSize: 15,
    marginTop: 15,
    fontweight: 'bold',
      backgroundColor: 'white',
      borderRadius: 25,
borderWidth: 5,
borderColor: 'pink',
  },
});

Output:

React native toast

Here three buttons are available. When you click on them, you will get respective toasts.

Here three buttons are available. When you click on them, you will get respective toasts.

Explanation of code :

We have given the example of all three available methods here show, Show With Gravity and Show With Gravity and offset .firstly er have imported all the necessary components, including the toaster toast android.

Method used in First button: here simple toast method is used just look at the code

  const toast_With_Duration_Handler = () => {
    ToastAndroid.show(
      'Hi I am Simple Toast nice to see you', ToastAndroid.LONG
    );

We have used the show method and passed two parameters first is a string and the second one is duration. The passed string is 'Hi, I am Simple Toast. Nice to see you. And this will stay on the screen for some time and will not disappear frequently. It will stay for a few seconds as a long parameter is passed in the method. This was our first button's method. If the first button is pressed, this simple toast will appear on the screen.

The method used in the second button is the toast with gravity method. Just look at the code.

  ToastAndroid.showWithGravity(
      'Hi I am center gravity Toast nice to see you',
      ToastAndroid.SHORT, 
      ToastAndroid.CENTER, 
    );
  };

We have used the show with gravity method. Here three parameters are passed string, duration, and severity. If the user clicks on the second button, the passed string     'Hi I am center gravity Toast nice to see you' will be displayed for a short duration, and then it will disappear, and the toast will appear at the center of the screen because of ToastAndroid.CENTER,  is passed by default. The toast appears at the bottom.

The method used in the third button: here toast with Gravity and offset method is used look at the code.

 ToastAndroid.showWithGravityAndOffset(
      'Hi I am Toast with garavity and offset nice to see you',
      ToastAndroid.LONG, 
      ToastAndroid.BOTTOM, 
      25, 
      500,
    );
  };

We have used the show with gravity offset method and passed five parameters to the method, which is similar to the previous one. The last two parameters add offset in the pixel. The passed string will appear as toast on the screen, and here it will stay for a long duration as long is passed.

Creation of button: we have created button using touchable highlights and all the three buttons are responsive .

<TouchableHighlight
          style={styles.buttonStyle}
          onPress={toast_With_Duration_Handler}>
          <Text style={styles.buttonTextStyle}>
            Generate Toast number 1
          </Text>
        </TouchableHighlight>

onPress decides that what will the button’s response when it is pressed so here we have called show method and this show method will display toast which we have already discussed.rest two buttons terminology is also same as the first one.

Styling

We have styled the components using a stylesheet as per our choice. You can even add animation to this.


Related Topics

React Native Pressable

It manages all the touch inputs, and it is one of the core wrappers introduced in July 2022. The Two important features of pressable are listed below- onPressInonPressOut onpressIn: We already know the...

3 minutes read.

React Native Fonts

The font is one of the important essences of any good UI(User Interface). It is very important to have a clear and beautiful font in your app. In this module,...

4 minutes read.

Redux in React Native

 It is a really amazing library of javascript, react, and react-native. It is very popular in react native domain. Every react and react native developer must know the redux it...

7 minutes read.

React Native stack navigation

You must know about the stack data structure. Stack is one of the important data structures which works in the FILO(first in last out) method. As you know, mobile apps are...

4 minutes read.

Why to Learn React Native

The demand for smartphone applications is ever-changing. The elder ones disappear from our remembrances, and smartphones and new applications come each day. In this turbulent ecosystem, it is struggling for...

4 minutes read.

Difference Between React.js and React Native

React js vs. React-native React native:  react-native is an open-source javascript framework that can be used to develop native mobile apps for IOS and android. Facebook released it in 2015 and witnessed...

3 minutes read.

Basic knowledge of firebase for React Native

Firebase for react-native : Firebase is a mobile and web application development platform which is created in 2014 by google. It is a product of google which is offered to developers...

7 minutes read.

Lifecycle of React Native Components

We are familiar with the word “lifecycle”. In human life, the lifecycle has three phases - birthlifedeath In react native its component lifecycle is almost the same as the human lifecycle. The...

6 minutes read.

React Native Components

React Native components are the same as ordinary react components, but they are different in terms of rendering and styling. All react code lives in react components. They are independent...

16 minutes read.

Advantages and Disadvantages of React Native

Advantages of using React Native It is observed that React Native stands out from most of the existing methods of cross-platform application development, like Ionic or Cordova, because React Native renders...

3 minutes read.

States in React Native

We can control data of components of react-native by state and props. We are familiar with props and used them multiple times previously. Props: This is used to customize components and...

3 minutes read.

React Native Sound

It is a great way to connect to the user. Just a tiny "ting" of any email received, or some sound for deleting a file or sound for sending a...

8 minutes read.

How Does React Native Work

Working of React Native React Native is a framework that lets us write android and iOS mobile applications using JavaScript. It is interesting to see how easy it is to develop...

4 minutes read.

HTTP in React Native

Networking HTTP in React-native: we all are aware that react native is a javascript framework.HTTP is nothing but an API request, and you might think that what is the need for an...

4 minutes read.

React Native Creating Components

React elements are where all React codes reside. The React elements are very similar to the regular React components, which have a unique style and offer contrast. Working with views When you...

3 minutes read.

React Native Tutorial

Introduction to react native : React-native is an open-source javascript framework that can be used to develop native mobile apps for IOS and android. It is released by Facebook in 2015...

4 minutes read.

What is a Bridge in React Native?

A React Native is generally composed of two fields or two parts: The first part is the code of JavaScript.The second one is native code. The ability to build a bridge between...

3 minutes read.

React Native indicator

React native activity indicator : React native activity indicator is one of the react-native components. We all have seen it several times. It's the loader. Look at the picture below - So now...

4 minutes read.

React Native Alert

In this part of the tutorial, we will learn about react-native's alert API. It is compatible with both android and IOS. What is API? Answer - API stands for Application programming interface,...

4 minutes read.

React Native Status Bar

What is the status bar? The simplest and easiest answer to this question can be answered by looking at your mobile phone's top section. What can you see? Most probably, it's...

4 minutes read.