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
-
- Simple Toast
ToastAndroid.show(message, duration)
- Show with gravity
ToastAndroid.showWithGravity(message, duration, gravity)
- With gravity and offset
ToastAndroid.showWithGravityAndOffset( message, duration, gravity, xOffset, yOffset )
- Simple Toast
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.
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
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 Screenshot
Output in Online Emulator
That was the React Native Toast (For Android only). If you have any doubts or 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. 🙂
greate 11!!!!
Thank you 🙂