Toast — Display Messages
Imagine when you send a message on a phone and a small message appears at the bottom saying:
Message Sent
It appears for a few seconds and disappears automatically.
This small popup message is called a Toast in Android.
1. Definition
Toast is a small popup message in Android that appears for a short time and disappears automatically without user interaction.
Simple meaning:
Toast = A temporary message shown to the user.
2. Purpose
Toast is used to inform users about actions or events.
Examples:
| Action | Toast Message |
|---|---|
| Login successful | "Login Successful" |
| Button clicked | "Button Pressed" |
| File saved | "File Saved" |
| Wrong password | "Invalid Password" |
Toast messages are short and non-interactive.
3. Basic Syntax (Kotlin)
Toast.makeText(context, "Message", Toast.LENGTH_SHORT).show()
Explanation:
context→ current activity (this)"Message"→ text to showToast.LENGTH_SHORT→ display time.show()→ display the Toast
Example:
Toast.makeText(this, "Hello User!", Toast.LENGTH_SHORT).show()
Output:
Hello User!
4. Toast Durations
Android supports two durations.
| Duration | Meaning |
|---|---|
Toast.LENGTH_SHORT | Displays for ~2 seconds |
Toast.LENGTH_LONG | Displays for ~3.5 seconds |
Example:
Toast.makeText(this, "Welcome!", Toast.LENGTH_LONG).show()
5. Important Properties
| Property | Description |
|---|---|
| Context | Activity where Toast is shown |
| Message | Text displayed in Toast |
| Duration | Time Toast remains visible |
| Position | Location of Toast on screen |
Example:
Toast.makeText(this, "Saved Successfully", Toast.LENGTH_SHORT).show()
6. Important Methods
makeText()
Creates the Toast message.
Toast.makeText(this, "Hello", Toast.LENGTH_SHORT)
show()
Displays the Toast on screen.
.show()
Example:
Toast.makeText(this, "App Started", Toast.LENGTH_SHORT).show()
setGravity()
Changes position of Toast.
val toast = Toast.makeText(this, "Hello", Toast.LENGTH_SHORT)
toast.setGravity(Gravity.CENTER, 0, 0)
toast.show()
Now Toast appears in the center of the screen.
7. Real-Time Examples
Example 1: Button Click Message
XML Button:
<Button
android:id="@+id/button1"
android:text="Click Me"/>
Kotlin Code:
button1.setOnClickListener {
Toast.makeText(this, "Button Clicked", Toast.LENGTH_SHORT).show()
}
Output:
Button Clicked
Example 2: Login Success Message
Toast.makeText(this, "Login Successful", Toast.LENGTH_LONG).show()
Used in login screens.
Example 3: Form Validation
if (username.isEmpty()) {
Toast.makeText(this, "Enter Username", Toast.LENGTH_SHORT).show()
}
Used in registration forms.
Example 4: File Saved Notification
Toast.makeText(this, "File Saved Successfully", Toast.LENGTH_SHORT).show()
Used in notes apps and editors.
8. Advantages
✔ Simple to implement
✔ Quick feedback for users
✔ Does not interrupt the user
✔ Automatically disappears
9. Disadvantages
❌ Cannot interact with Toast
❌ Limited customization
❌ Short display time
10. One-Line Summary
Toast is a small temporary popup message used in Android to quickly inform users about actions or events.