Kahibaro
Discord Login Register

9.4 Toasts and Snackbars

Overview

Toasts and Snackbars are both simple ways to show short messages to the user. They do not require a full screen or a dialog, and they disappear automatically after a short time. In Android apps, these components are useful for lightweight feedback, such as confirming an action or showing a non critical error.

Toasts belong to the core Android framework, while Snackbars are part of the Material Design components. Snackbars are usually preferred in modern apps because they follow Material guidelines and often provide an optional action button.

What is a Toast

A Toast is a small popup message that appears near the bottom of the screen. It is not interactive. The user cannot tap it or dismiss it manually. It simply shows a message for a short or long duration and then disappears.

A Toast is not tied to a specific view, and it is not part of the activity layout. It is a system controlled overlay that can appear even when your app is not in the foreground, although modern usage focuses on use while your app is visible.

To create and show a basic Toast in an activity, you typically use the Toast class and the makeText function. For example, you pass a Context, the text to show, and a duration, then call show() on the result.

Toast.makeText(this, "Item saved", Toast.LENGTH_SHORT).show()

In this example, this refers to the current activity context, "Item saved" is the message, and Toast.LENGTH_SHORT is the duration constant.

Toast Durations and Behavior

Toasts support two standard durations, short and long. They are represented by the constants Toast.LENGTH_SHORT and Toast.LENGTH_LONG. The exact time depends on the system, but a short toast shows for a briefer period than a long toast.

Use Toast.LENGTH_SHORT or Toast.LENGTH_LONG for duration. Custom numeric durations are not supported by the standard Toast.makeText API.

Toasts appear above your app layout, usually near the bottom. They do not block user input, and the user can continue interacting with your UI while the toast is visible. Since toasts vanish on their own, you do not need to manually hide them.

Custom Toast Messages

Although the default toast only shows simple text, you can customize its layout if needed. This is less common in modern apps but still possible. The idea is to create a layout in XML, inflate it, and assign it to a Toast instance.

First, you define a custom layout file, for example custom_toast.xml in the res/layout directory, with views such as an ImageView and a TextView. Then, in code, you inflate that layout, create a Toast, and set the custom view.

val inflater = layoutInflater
val layout = inflater.inflate(R.layout.custom_toast, null)
val toast = Toast(this)
toast.duration = Toast.LENGTH_LONG
toast.view = layout
toast.show()

Since custom toasts override the default appearance, you are responsible for making the design clear and readable. However, with Material Design available through Snackbars, custom toasts are used less frequently in new designs.

Limitations of Toasts

Toasts provide very basic feedback, but they also have several limitations. They cannot contain interactive elements, so you cannot add buttons that the user can tap. Toasts are also not directly aware of the current view hierarchy, so they are not placed relative to specific views.

They are loosely tied to the app context and do not follow Material guidelines as closely as Snackbars. Because of this, for events directly related to the current screen and for messages that might require an action, Snackbars are often recommended over Toasts.

What is a Snackbar

A Snackbar is a message bar that appears at the bottom of the screen, typically above the navigation bar. It belongs to Material Design and is part of the Material Components library. Unlike Toasts, Snackbars are attached to a view in your layout, such as your root layout or a CoordinatorLayout.

Snackbars are usually used to communicate a short and specific message about a process, especially after a user action. For example, you can show a Snackbar to confirm that an item was deleted, and optionally give the user a chance to undo the deletion.

Snackbars can also contain an optional action button that lets the user react directly to the message. For example, a message can say "Item deleted" with an "UNDO" action that restores the item.

Creating a Basic Snackbar

To use Snackbars, your project needs the Material Components dependency. Once that is present, you can use the Snackbar class and its make function. A Snackbar always needs a view to attach to, a text, and a duration.

The view parameter is typically a root view in your layout, for example the top level layout from your activity. For a basic message, you then call show() to display the Snackbar.

Snackbar.make(
    findViewById(R.id.root_layout),
    "Item saved",
    Snackbar.LENGTH_SHORT
).show()

Here R.id.root_layout should be the id of your root view in the activity layout, commonly a ConstraintLayout or another top level container.

Snackbars have predefined durations similar to Toasts. Besides LENGTH_SHORT and LENGTH_LONG, Snackbars add LENGTH_INDEFINITE, which keeps the Snackbar visible until it is dismissed or hidden through code.

Use Snackbar.LENGTH_INDEFINITE only when you provide a clear way to dismiss the Snackbar, usually through an action button or another event.

Adding Actions to Snackbars

One key feature that makes Snackbars more powerful than Toasts is the optional action. The action appears as a button on the right side of the Snackbar and can perform code when tapped.

To add an action, you call setAction on the Snackbar instance before showing it. You provide a text label and a listener that runs when the user taps the action.

Snackbar.make(
    findViewById(R.id.root_layout),
    "Item deleted",
    Snackbar.LENGTH_LONG
).setAction("UNDO") {
    // Restore the deleted item here
}.show()

In this example, the code inside the lambda is executed when the user presses the "UNDO" button. If the user ignores the Snackbar, it simply disappears after the duration ends and the undo logic is not run.

Actions should be concise and directly related to the message. The Material guidelines suggest that actions should be short verbs such as "UNDO", "RETRY", or "DISMISS".

Choosing Between Toast and Snackbar

To decide whether to use a Toast or a Snackbar, you should consider where the message comes from and what kind of response you expect from the user.

A Toast is suitable for very simple, low priority messages that only need to be seen briefly, such as debug information during development or quick confirmations that do not require further action. Since Toasts are non interactive and less visually integrated with your layout, they work better for background style feedback.

A Snackbar is the better choice when the message is directly related to what the user just did in the current screen. For example, submitting a form, deleting an item, or encountering a small error loading data. If you want to offer a way to fix or reverse the result, such as retrying a network request or undoing a deletion, a Snackbar with an action is ideal.

For user facing apps that follow Material Design, prefer Snackbars over Toasts for messages connected to on screen actions, especially when an immediate user response or undo option is useful.

Handling Context and View Requirements

When working with Toasts and Snackbars, it is important to understand which objects they need. Toasts require a context, which is often the activity instance, or in some cases the application context. Snackbars require a view, typically a parent layout, so that they can attach themselves and be positioned correctly.

Inside an activity, this can often be used as the context for a Toast, and findViewById can retrieve the root layout view for a Snackbar. Inside a fragment, you typically use requireContext() for Toasts and requireView() or a passed in view for Snackbars.

For example, inside a fragment:

// Toast in a Fragment
Toast.makeText(requireContext(), "Hello from Fragment", Toast.LENGTH_SHORT).show()
// Snackbar in a Fragment
Snackbar.make(
    requireView(),
    "Item updated",
    Snackbar.LENGTH_SHORT
).show()

Using the correct context and view ensures that your messages are displayed without errors and are attached to the current visible UI.

Visual Behavior and Interaction

Toasts appear floating over the UI, usually near the bottom center. The user cannot interact with them. If multiple Toasts are created in rapid succession, they can appear one after another, which may feel overwhelming, so you should avoid spamming toasts.

Snackbars slide in at the bottom and can move other views, especially if you use certain layout containers that respond to them. For example, a FloatingActionButton can automatically move up when a Snackbar is shown. Snackbars can be dismissed by user actions like swiping them away, if the behavior is supported by the parent container.

Because of these visual behaviors, Snackbars feel more integrated with the screen and are more consistent with Material Design interactions than Toasts.

Summary

Toasts and Snackbars are both useful for short messages, but they fit different needs. Toasts offer quick, non interactive messages that are easy to show with only a context and a text. Snackbars, built on Material Components, attach to a view, support actions, and integrate more naturally with modern Android layouts.

By understanding when each is appropriate, you can provide clear feedback to users without interrupting their flow, and you can give them a simple way to undo or fix actions when it matters.

Views: 1

Comments

Please login to add a comment.

Don't have an account? Register now!