Toast messages in Android provide brief feedback about an operation in a small popup that disappears automatically. They are lightweight, non-blocking, and ideal for informing users about actions such as form submission, status updates, or simple confirmations.
This updated guide on javatechig.com covers how to show Toast messages using modern Android APIs in both Kotlin and Java — including custom layouts, duration control, positioning, and best practices.
What Is a Toast in Android?
A Toast is a transient message that pops up on the screen for a short duration without blocking user interaction. It’s part of the Android UI framework and requires no extra permission.
Typical use cases:
- Showing success message
- Confirming user actions
- Indicating brief status feedback
Toasts are not suitable for critical alerts — use Snackbars or Dialogs for that.
Basic Toast Implementation
Kotlin Example
Toast.makeText(
this,
"This is a simple Toast message",
Toast.LENGTH_SHORT
).show()
Java Example
Toast.makeText(
MainActivity.this,
"This is a simple Toast message",
Toast.LENGTH_SHORT
).show();
Duration Options
| Constant | Duration |
|---|---|
Toast.LENGTH_SHORT | ~2 seconds |
Toast.LENGTH_LONG | ~3.5 seconds |
Custom Toast Layout
For richer UI, you can create a custom toast layout.
Step 1: Create Custom Layout (toast_layout.xml)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="8dp"
android:background="@drawable/toast_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imgIcon"
android:src="@drawable/ic_check"
android:layout_width="24dp"
android:layout_height="24dp"/>
<TextView
android:id="@+id/txtMessage"
android:text="Custom Toast"
android:textColor="@android:color/white"
android:layout_marginStart="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Displaying Custom Toast
Kotlin
val inflater = layoutInflater
val layout = inflater.inflate(R.layout.toast_layout, null)
val toast = Toast(applicationContext)
toast.duration = Toast.LENGTH_LONG
toast.view = layout
toast.show()
Java
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout, null);
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Positioning Toast on Screen
By default, Toast appears near the bottom center. You can adjust the position:
Kotlin
toast.setGravity(Gravity.TOP or Gravity.CENTER_HORIZONTAL, 0, 200)
toast.show()
Java
toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 200);
toast.show();
Use offsets (xOffset, yOffset) to fine-tune placement.
Best Practices
Prefer Snackbars for Actionable Feedback
Toasts are ephemeral and non-interactive. Use Snackbars when you need user action (e.g., undo).
Avoid Overuse
Too many toasts can annoy users. Display them only for meaningful events.
Respect Localization
Use string resources (strings.xml) instead of hard-coded text.
<string name="toast_msg">Item saved successfully</string>
Common Errors & Solutions
Toast Not Appearing
Cause: Wrong Context
Fix: Use Activity context (this / MainActivity.this)
Long Toast Duration Too Short
Cause: Android handles durations internally
Fix: Use LENGTH_LONG or custom timer logic
When Not to Use Toast
- Critical alerts
- Mandatory acknowledgements
- UI interruptions requiring action
Use Dialogs or Snackbars for those cases.


