Displaying Notification: Toast Class, displaying message on Toast

A Toast is a small message that pops up briefly and fades on its own, created with Toast.makeText(context, message, duration) and shown by chaining .show(), and forgetting that .show() call is why beginners see nothing appear.

7 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

The tap that did nothing visible

In the last lesson, tapping Register read the name but showed the user nothing. They cannot tell it worked. You want a quick 'Welcome, Aditi' to flash and fade.

The simplest feedback in Android is the Toast: a small grey message that appears near the bottom, stays a second or two, and vanishes on its own, no button, no interruption. It is two lines to write, and one of those lines is the one everyone forgets.

Theory

Bread from a toaster

The name is literal: like toast popping from a toaster, the message pops up, sits briefly, and is gone, you do not dismiss it, it leaves by itself. That transience is the point: a Toast is for a quick 'done!' or 'saved', not for anything the user must read carefully or act on. For those you need a dialog, which comes later.

Theory

makeText, then show

Two steps:

  • Build it: Toast.makeText(context, message, duration).
  • context: usually the Activity, this (or MainActivity.this inside a listener).
  • message: the text (a String).
  • duration: Toast.LENGTH_SHORT (about 2s) or Toast.LENGTH_LONG (about 3.5s).
  • Show it: chain `.show()`.

Skip .show() and the toast is built but never displayed, the single most common Toast mistake.

Practical

FestConnect confirms the tap

register.setOnClickListener(v -> {
    String typed = name.getText().toString();

    // build AND show (the .show() is essential)
    Toast.makeText(
        MainActivity.this,
        "Welcome, " + typed,
        Toast.LENGTH_SHORT
    ).show();
});

// Without .show(), makeText builds a Toast that never appears.
// Toast.LENGTH_LONG would keep it on screen a bit longer.

Think first

Nothing appeared. Why?

A student writes Toast.makeText(this, "Hi", Toast.LENGTH_SHORT); in the onClick and no message ever shows, with no error. What one thing is missing?

Show the answer

The `.show()` call. Toast.makeText(...) only creates the Toast object; it does not display it. Without chaining .show(), the toast is built and immediately discarded, silently, which is why there is no error and no message. The fix is Toast.makeText(this, "Hi", Toast.LENGTH_SHORT).show();. It is the number-one Toast bug, so make show() a reflex.

Quiz

Which statement about a Toast is correct?

  1. It shows a brief message that auto-dismisses; you must call show() to display it
  2. It stays on screen until the user taps a button to close it
  3. It appears as a permanent icon in the status bar
  4. It blocks the app until dismissed
Show the answer

It shows a brief message that auto-dismisses; you must call show() to display it

A Toast shows a short, non-interactive message that fades on its own, and it only appears if you call show() after makeText (A). It has no dismiss button and does not wait for the user (that is an AlertDialog) (B). It is not a persistent status-bar notification (that is a Notification via NotificationManager) (C), and it does not block the app (D). Transient, non-blocking, show()-required: that is a Toast.

Watch out

Toast traps

1. Forgetting .show(): makeText builds, show() displays. No show(), no toast, and no error to hint why.

2. Confusing Toast with Notification: a Toast is transient feedback; a status-bar Notification (via NotificationManager) is persistent and interactive. Different classes, different uses.

3. Overusing toasts for errors that matter: a toast can be missed (it fades). For something the user must acknowledge, use an AlertDialog instead.

Theory

Next: a two-state button

FestConnect now gives feedback. The next widget is the ToggleButton: a button with two states, on and off, with textOn/textOff labels and methods to read and set its state, handy for a 'Notifications: On/Off' setting. It introduces reading a widget's state, which the CheckBox and RadioButton in Unit 5 build on.

Summary

Key takeaways

  • A Toast is a brief, non-interactive message that pops up and auto-dismisses.
  • Build with Toast.makeText(context, message, duration); context is usually the Activity (this).
  • You MUST chain .show() to display it (forgetting it is the top Toast bug).
  • Duration is Toast.LENGTH_SHORT (~2s) or Toast.LENGTH_LONG (~3.5s).
  • A Toast is not a persistent status-bar Notification and not a modal AlertDialog.
  • Memory hook: like toast from a toaster, it pops up and leaves on its own.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Android Widgets (UI)

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Displaying Notification: Toast Class, displaying message on Toast · Mobile Application Development - 1 (option B) · Gri-Learn