In React Native, what does ToastAndroid.show do, and what happens when the same call runs on an iOS device?
answer
- native Android toast, not a component
- message plus SHORT or LONG
- gravity ignored on Android 11+
- iOS gets a warning, no toast
- text only, no callback
basics
~20 sToastAndroid.show(message, duration) shows a native Android toast for ToastAndroid.SHORT or ToastAndroid.LONG. On iOS the module is a fallback stub: the call logs a 'not supported on this platform' warning and shows nothing, so iOS needs another feedback path.
solid answer
~40 s`ToastAndroid` wraps Android's system toast: a short text message that appears over the app and disappears on its own. `ToastAndroid.show(message, ToastAndroid.SHORT)` or `ToastAndroid.LONG` is the basic call; `showWithGravity` adds `TOP`, `BOTTOM` or `CENTER`, and `showWithGravityAndOffset` adds x/y offsets. Since Android 11 (API 30), gravity and offsets have no effect on text toasts, so those variants only position the toast on API 29 and below. The call returns nothing, and a toast carries text only — no button, no press callback, no styling. On iOS React Native resolves `ToastAndroid` to a fallback whose methods only log a warning, so nothing crashes but the user sees nothing; cross-platform code branches on `Platform.OS` and shows its own in-app message on iOS.
code
tsx · 10 linesimport { Alert, Platform, ToastAndroid } from 'react-native';
export function notifyDraftSaved(): void {
const message = 'Draft quote saved';
if (Platform.OS === 'android') {
ToastAndroid.show(message, ToastAndroid.SHORT);
return;
}
Alert.alert(message); // iOS has no system toast
}go deeper
Recall show(message, ToastAndroid.SHORT or LONG), that it is Android-only, and that on iOS it logs a warning instead of showing anything.
Explain the gravity limitation from Android 11, the fire-and-forget API with no callback, and how the iOS fallback resolves so shared code still imports cleanly.
Decide when a native toast is acceptable versus a shared in-app message component, especially for errors, undo actions and consistent cross-platform UX.
Treat transient feedback as a design-system concern: one component and rules for when feedback may disappear on its own, rather than per-screen platform branches.
## What a toast is A **toast** is Android's lightest feedback surface: one line or two of text, drawn by the system over the current app, that disappears after a fixed time without any user action. React Native exposes it through **`ToastAndroid`**, a JavaScript module backed by a native Android module. It is not a React component — you call a function, React renders nothing, and Android draws the toast. Typical uses are low-stakes confirmations: "Draft quote saved", "Copied", "You are offline". Anything the user must act on or read carefully belongs in the app's own UI instead. ## The API | Member | What it does | | --- | --- | | `show(message, duration)` | Shows a text toast | | `showWithGravity(message, duration, gravity)` | Same, with a position hint | | `showWithGravityAndOffset(message, duration, gravity, xOffset, yOffset)` | Same, with a position hint and pixel offsets | | `SHORT`, `LONG` | Duration constants | | `TOP`, `BOTTOM`, `CENTER` | Gravity constants | All three methods return nothing. There is no promise, no "dismissed" callback, and no way to attach an action button or custom styling: the message is a plain string. ## The Android 11 gravity change Starting with **Android 11 (API level 30)**, setting the gravity has **no effect on text toasts**. React Native's documentation therefore notes that `showWithGravity` and `showWithGravityAndOffset` only position the toast on API 29 and below. On current devices a toast appears where the system puts it regardless of the gravity you pass. If placement matters, a toast is the wrong tool. ## What happens on iOS iOS has no system toast. React Native still exports `ToastAndroid` everywhere so shared code compiles and imports cleanly, but on iOS it resolves to a **fallback implementation**: - the duration and gravity constants are dummy values (`0`); - every method only calls `console.warn('ToastAndroid is not supported on this platform.')`; - nothing is shown and nothing throws. That means the classic junior surprise is **not a crash** but silent missing feedback on iOS — easy to miss when the team tests mostly on one platform. ## A cross-platform pattern Branch on the platform where the message is triggered and use an in-app surface on iOS: ```tsx import { Alert, Platform, ToastAndroid } from 'react-native'; export function notifyDraftSaved() { const message = 'Draft quote saved'; if (Platform.OS === 'android') { ToastAndroid.show(message, ToastAndroid.SHORT); } else { Alert.alert(message); } } ``` `Alert.alert` is a blunt substitute because it needs a tap to dismiss; production apps usually render their own snackbar-style component on both platforms so the two behave alike. The decision to use the native toast on Android and a custom component on iOS is legitimate, but it should be deliberate. ## Deciding which feedback surface to use 1. **Is the message informational and safe to miss?** A toast on Android is fine. 2. **Does the user need to act on it** (undo, retry)? Render an in-app component with a button. 3. **Must the user acknowledge it?** Use `Alert.alert`, whose dialog stays until the user taps a button. 4. **Must both platforms look identical?** Use one in-app component everywhere. In an insurance-quote flow, "Draft quote saved" passes the first test; "Payment failed" fails it and belongs in the form itself, next to the field the user has to fix. ## When not to use ToastAndroid - **Errors that need action.** The toast disappears; the user may never read it. Show the error inline or in a dialog. - **Anything with a button.** "Undo" or "Retry" needs a component; the toast has no press callback. - **Precise placement.** Gravity is ignored on Android 11 and later. - **Consistent cross-platform UX.** If design wants identical feedback on both platforms, one in-app component serves better than a native toast plus a substitute. ## Interview summary State the call (`show` with `SHORT` or `LONG`), the Android 11 gravity limitation, and the iOS behaviour — a warning and no toast, not a crash — then describe the platform branch you would write.
- In React Native, why might ToastAndroid.showWithGravity(message, ToastAndroid.SHORT, ToastAndroid.TOP) still show the toast at the bottom?From Android 11 (API 30), the system ignores gravity for text toasts, so the position argument has no effect there. React Native's docs note that `showWithGravity` and `showWithGravityAndOffset` only position toasts on API 29 and below.
- Can a React Native ToastAndroid toast carry an Undo button?No. The methods take a plain string and return nothing; there is no action, press callback or dismissal event. An Undo affordance needs an in-app component that the app renders and controls itself.
saying these in an interview costs you the question
- ToastAndroid crashes the app on iOS
- ToastAndroid is a component you render in JSX
- showWithGravity positions the toast on every Android version
- show accepts a custom duration in milliseconds
- A toast can carry an action button through an options argument