This API is Android-only. It will not work on iOS or other platforms.
This exposes the native ToastAndroid module as a JS module. This has methods to display toast notifications on Android.
Example
import React from 'react';
import { View, Button, ToastAndroid, StyleSheet } from 'react-native';
const App = () => {
const showToast = () => {
ToastAndroid.show('A pikachu appeared nearby!', ToastAndroid.SHORT);
};
const showToastWithGravity = () => {
ToastAndroid.showWithGravity(
'All Your Base Are Belong To Us',
ToastAndroid.SHORT,
ToastAndroid.CENTER
);
};
const showToastWithGravityAndOffset = () => {
ToastAndroid.showWithGravityAndOffset(
'A wild toast appeared!',
ToastAndroid.LONG,
ToastAndroid.BOTTOM,
25,
50
);
};
return (
<View style={styles.container}>
<Button title="Show Toast" onPress={showToast} />
<Button title="Show Toast With Gravity" onPress={showToastWithGravity} />
<Button
title="Show Toast With Gravity And Offset"
onPress={showToastWithGravityAndOffset}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingHorizontal: 10,
gap: 10,
},
});
export default App;
Methods
show()
ToastAndroid.show(message, duration);
Display a toast message for a specified duration.
Parameters:
| Name | Type | Required | Description |
|---|
| message | string | Yes | A string with the text to toast |
| duration | number | Yes | The duration of the toast – either ToastAndroid.SHORT or ToastAndroid.LONG |
Example:
ToastAndroid.show('This is a toast!', ToastAndroid.SHORT);
showWithGravity()
ToastAndroid.showWithGravity(message, duration, gravity);
Display a toast message for a specified duration with a given gravity.
On Android R (API 30) or later (when targeting API 30+), the gravity setting may not have any effect on text toast placement due to setGravity becoming a no-op.Reference: Android Toast.setGravity documentation
Parameters:
| Name | Type | Required | Description |
|---|
| message | string | Yes | A string with the text to display in the toast |
| duration | number | Yes | The duration of the toast – ToastAndroid.SHORT or ToastAndroid.LONG |
| gravity | number | Yes | Positioning on the screen – ToastAndroid.TOP, ToastAndroid.BOTTOM, or ToastAndroid.CENTER |
Example:
ToastAndroid.showWithGravity(
'Hello from the top!',
ToastAndroid.SHORT,
ToastAndroid.TOP
);
showWithGravityAndOffset()
ToastAndroid.showWithGravityAndOffset(message, duration, gravity, xOffset, yOffset);
Display a toast message for a specified duration with a given gravity and custom offsets. These offset values will translate to pixels.
On Android R (API 30) or later (when targeting API 30+), setting gravity and offsets may not visibly affect the placement of text toasts.
Parameters:
| Name | Type | Required | Description |
|---|
| message | string | Yes | A string with the text to display in the toast |
| duration | number | Yes | The duration of the toast – ToastAndroid.SHORT or ToastAndroid.LONG |
| gravity | number | Yes | Positioning on the screen – ToastAndroid.TOP, ToastAndroid.BOTTOM, or ToastAndroid.CENTER |
| xOffset | number | Yes | Horizontal offset from the given gravity (in pixels) |
| yOffset | number | Yes | Vertical offset from the given gravity (in pixels) |
Example:
ToastAndroid.showWithGravityAndOffset(
'Custom positioned toast',
ToastAndroid.LONG,
ToastAndroid.BOTTOM,
25,
50
);
Constants
Duration Constants
SHORT
Type: number
Value: 2000 (milliseconds / 2 seconds)
Indicates a short duration on the screen.
ToastAndroid.show('Quick message', ToastAndroid.SHORT);
LONG
Type: number
Value: 3500 (milliseconds / 3.5 seconds)
Indicates a long duration on the screen.
ToastAndroid.show('Longer message', ToastAndroid.LONG);
Gravity Constants
On Android R (API 30) or later, these gravity constants may not have any visible effect on toast positioning.
TOP
Type: number
Indicates that the toast message should appear at the top of the screen.
ToastAndroid.showWithGravity(
'Top toast',
ToastAndroid.SHORT,
ToastAndroid.TOP
);
BOTTOM
Type: number
Indicates that the toast message should appear at the bottom of the screen.
ToastAndroid.showWithGravity(
'Bottom toast',
ToastAndroid.SHORT,
ToastAndroid.BOTTOM
);
CENTER
Type: number
Indicates that the toast message should appear at the center of the screen.
ToastAndroid.showWithGravity(
'Center toast',
ToastAndroid.SHORT,
ToastAndroid.CENTER
);
Complete Example
import React from 'react';
import { View, Button, ToastAndroid, StyleSheet } from 'react-native';
const App = () => {
return (
<View style={styles.container}>
<Button
title="Basic Toast (Short)"
onPress={() =>
ToastAndroid.show('Short toast message', ToastAndroid.SHORT)
}
/>
<Button
title="Basic Toast (Long)"
onPress={() =>
ToastAndroid.show('Long toast message', ToastAndroid.LONG)
}
/>
<Button
title="Toast at Top"
onPress={() =>
ToastAndroid.showWithGravity(
'Toast at top',
ToastAndroid.SHORT,
ToastAndroid.TOP
)
}
/>
<Button
title="Toast at Center"
onPress={() =>
ToastAndroid.showWithGravity(
'Toast at center',
ToastAndroid.SHORT,
ToastAndroid.CENTER
)
}
/>
<Button
title="Toast at Bottom"
onPress={() =>
ToastAndroid.showWithGravity(
'Toast at bottom',
ToastAndroid.SHORT,
ToastAndroid.BOTTOM
)
}
/>
<Button
title="Toast with Offset"
onPress={() =>
ToastAndroid.showWithGravityAndOffset(
'Toast with custom offset',
ToastAndroid.LONG,
ToastAndroid.BOTTOM,
25,
100
)
}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingHorizontal: 20,
gap: 10,
},
});
export default App;