Build a Reliable Snackbar Notification System in Android
A Snackbar is Android’s lightweight, in-app feedback for actions such as saving a form, deleting a row, or retrying a failed request. It appears near the bottom of the screen, gives users a brief response, and can offer one useful action without blocking the workflow.
Building one feature at a time quickly produces inconsistent messages and timing. A small message flow instead separates UI events from presentation: the ViewModel declares what happened, while the Activity renders it through Material Components. This guide walks through that structure in Kotlin, including actions, retries, accessibility, localisation, and release checks.
For Australian users, clarity matters across busy public transport, regional areas, and varied screen sizes. In Sydney and Melbourne, people often complete bookings while moving between stations; in rural NSW or around Perth, a patchy connection can make a retry more useful than a bare failure notice. Friendly, direct wording such as “You’re all sorted” can suit local conversation, while Android apps distributed mainly through Google Play should behave consistently across that broad market.
Compare feedback options
Choose feedback that matches the consequence and duration of an event. A Snackbar suits brief, non-critical status. A Toast is less interactive, a dialog demands attention, and an inline message stays attached to the relevant control. Reserve Android system notifications for information that remains important outside the current screen.
| Mechanism | Best fit | Interaction | Visibility |
|---|---|---|---|
| Snackbar | Brief status after an action | Optional action | Anchored within the current screen |
| Toast | Simple confirmation | None | Short overlay |
| Dialog | Decision or required input | Buttons or controls | Covers the current context |
| Inline message | Validation near a field | Usually none | Remains with related content |
| System notification | Ongoing or background event | Tap opens content | Outside the app |
Put the comparison beside each event type before coding. “Saved” and “Deleted” can use a Snackbar, a payment confirmation may need a dedicated screen, and a required account change should not vanish after a few seconds. Once that boundary is clear, the Snackbar layer stays focused and predictable.
Set up the Material snackbar host
Add the Material dependency and give the screen a SnackbarHost inside a CoordinatorLayout. The host provides suitable margins, animation, accessibility behaviour, and placement around navigation controls.
dependencies {
implementation("com.google.android.material:material:1.12.0")
}
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.snackbar.SnackbarHost
android:id="@+id/snackbarHost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:anchorGravity="bottom" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Use Snackbar.make() with the host rather than an arbitrary button or text view. Material locates the coordinator, anchors the message above the host, and handles dismissal. If the design uses a bottom app bar, anchor the host to it; otherwise, the default bottom placement is suitable.
Create a centralised message flow
Model messages as a sealed set of UI outcomes. Resource IDs keep copy out of the ViewModel and make translation straightforward. A channel gives events a defined order and lets a message wait briefly while the Activity is starting.
sealed class UiMessage(@StringRes val textRes: Int) {
data class Success(@StringRes textRes: Int) : UiMessage(textRes)
data class Error(
@StringRes textRes: Int,
val canRetry: Boolean = false
) : UiMessage(textRes)
}
Expose the messages through a flow and collect them only while the screen is at least started. repeatOnLifecycle prevents an invisible Activity from showing stale feedback, while receiveAsFlow suits this single-consumer UI role.
private val messageChannel = Channel<UiMessage>(Channel.BUFFERED)
val messages = messageChannel.receiveAsFlow()
fun send(message: UiMessage) {
viewModelScope.launch {
messageChannel.send(message)
}
}
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.messages.collect { message ->
val snackbar = Snackbar.make(
binding.snackbarHost,
getString(message.textRes),
if (message is UiMessage.Error) Snackbar.LENGTH_LONG
else Snackbar.LENGTH_SHORT
)
if (message is UiMessage.Error && message.canRetry) {
snackbar.setAction(R.string.retry) {
viewModel.retryLastRequest()
}
}
snackbar.show()
}
}
}
Choose duration by meaning: LENGTH_SHORT for minor confirmations and LENGTH_LONG for errors with recovery steps. Snackbar timing is not a deadline, so never rely on it to complete a transaction or assume every user will read it.
Connect actions to application behaviour
An action turns feedback into recovery. Use “Retry” for a failed upload, “Undo” for a deletion, or “View” when a newly created item deserves confirmation. Keep the business operation in the ViewModel and pass only a safe action callback to the view.
After the action succeeds, send a new outcome through the same channel; for example, a retry can produce “Uploaded”. This makes the flow testable and prevents a button click from triggering work in a view that may already be gone. Use a single, specific verb because the action label is also read aloud by accessibility services.
Control timing and screen changes
Several requests can finish close together, especially after reconnecting in a regional area. Present one meaningful message at a time, or use Snackbar’s setNextSnackbar() mechanism for a strict sequence. Avoid stacking custom views, which obscures the action and makes touch targets unpredictable.
Screen rotation or navigation creates another edge case. Keep pending UI messages in the ViewModel, not the Activity, and decide which outcome still matters after recreation. For process death, persist only small recoverable state. Critical background events belong in a proper notification or destination screen because a Snackbar cannot follow the user after the app closes.
Localise and test the experience
Store every visible string in Android resources, including action labels, and format dynamic values with the active locale. Australian English, dates, and plurals should follow device settings rather than hard-coded assumptions. Friendly copy such as “Saved — you’re all sorted” can be reviewed with Australian users for tone.
Multilingual audiences also need bidirectional layouts. Before using directional icons or checking text alignment, follow supporting right-to-left layouts so the message, icon, and action remain in the expected order for Arabic, Hebrew, and other right-to-left locales.
Test with TalkBack, large font settings, display scaling, and slow network conditions. Check that the action has a meaningful accessible label and that success and failure differ in wording, not colour alone. Automated UI tests can click the action and assert the next message, while manual checks confirm placement on compact phones.
Prepare for production use
Treat the Snackbar path as part of the release contract. Run lint, unit, and UI tests against success, failure, retry, and duplicate-event cases before submitting a build through Google Play. Versioned strings and a small analytics event for message type and action can reveal whether users understand the flow without collecting message content.
Pre-release checks
- Verify all message and action strings exist in resources and render at large font sizes.
- Test retry, undo, rotation, and rapid duplicate responses with instrumented UI tests.
- Run accessibility checks with TalkBack and review colour-independent wording.
After release, compare Snackbar interactions with failed-request counts and support reports. This shows whether an action is useful while avoiding the storage of sensitive message text.
Release monitoring
- Track message types and action taps as anonymous events.
- Use a staged Google Play rollout and monitor crash and ANR rates.
- Review recurring failures by Android version, screen size, and network state.
A dependable implementation treats each message as a small, testable contract. When feedback is timely, actionable, and restrained to the current screen, users can keep working without being interrupted.