Toast in Android Using Kotlin - Complete Guide with Examples

๐Ÿ‘๏ธ 14 Views
|
๐Ÿ“… Jul 09, 2026
|
โฑ๏ธ 11 min read
Toast in Android Using Kotlin - Complete Guide with Examples

If you have ever used an Android app and seen that small popup message appear at the bottom of the screen for a second or two โ€” that is a Toast. It is one of the simplest and most commonly used UI components in Android development, and if you are just getting started with Kotlin, it is also one of the first things you will want to know how to use.

In this guide we will cover everything about Toast notifications in Android using Kotlin โ€” from the most basic usage all the way to customising the appearance, positioning it on screen, and avoiding the mistakes that trip up most beginners.

What Is a Toast in Android?

A Toast is a small, non-interactive popup message that appears briefly on the screen and then disappears automatically. It does not require any user action to dismiss โ€” it just shows up, delivers a short piece of information, and fades away on its own.

Toasts are perfect for situations where you want to give the user quick feedback without interrupting what they are doing. Some common real-world examples include:

  • Confirming that a form was submitted successfully
  • Telling the user that a file was saved
  • Showing a "Copied to clipboard" message after a copy action
  • Warning the user that there is no internet connection
  • Displaying a "Press back again to exit" message

They are not suitable for critical information that the user must acknowledge โ€” for that, you would use a Dialog or a Snackbar. But for quick, low-priority notifications, Toast is the go-to solution.

Basic Toast in Kotlin

Creating a basic Toast in Kotlin is a single line of code. Here is the simplest possible example:

Toast.makeText(this, "Hello! This is a Toast message.", Toast.LENGTH_SHORT).show()

Let us break down what each part of this line does:

  • Toast.makeText() โ€” the static method that creates the Toast. It takes three parameters: context, message, and duration.
  • this โ€” the context. Inside an Activity, this refers to the current Activity context. If you are inside a Fragment, use requireContext() instead.
  • "Hello! This is a Toast message." โ€” the text string you want to display. Keep it short โ€” Toasts are designed for brief messages, not paragraphs.
  • Toast.LENGTH_SHORT โ€” how long the Toast stays visible. LENGTH_SHORT is approximately 2 seconds. LENGTH_LONG is approximately 3.5 seconds.
  • .show() โ€” this is critical. Without calling .show(), the Toast is created but never displayed. Forgetting this is the most common beginner mistake.

Toast Duration โ€” SHORT vs LONG

Android gives you exactly two duration options for Toast messages. There is no way to set a custom duration in milliseconds โ€” you must choose one of these two constants:

// Stays visible for approximately 2 seconds
Toast.makeText(this, "Short toast message", Toast.LENGTH_SHORT).show()

// Stays visible for approximately 3.5 seconds
Toast.makeText(this, "Long toast message", Toast.LENGTH_LONG).show()

Use LENGTH_SHORT for most situations. Use LENGTH_LONG only when the message is important enough that the user needs a little more time to read it.

Showing a Toast From an Activity

Here is a complete, working example inside an Activity that shows a Toast when a button is clicked:

package com.example.myapp

import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val button = findViewById<Button>(R.id.myButton)

        button.setOnClickListener {
            Toast.makeText(this, "Button clicked!", Toast.LENGTH_SHORT).show()
        }
    }
}

Notice that the context passed is this โ€” which refers to the current MainActivity instance. This works perfectly inside an Activity.

Showing a Toast From a Fragment

Inside a Fragment, this refers to the Fragment itself, not an Activity context. You need to use requireContext() instead:

// Inside a Fragment
Toast.makeText(requireContext(), "This is inside a Fragment", Toast.LENGTH_SHORT).show()

You can also use activity if you need the Activity context specifically, but requireContext() is the safest and most recommended approach inside Fragments.

Changing Toast Position on Screen

By default, Toast messages appear near the bottom center of the screen. You can change this using the setGravity() method:

import android.view.Gravity

val toast = Toast.makeText(this, "Toast at the top!", Toast.LENGTH_SHORT)
toast.setGravity(Gravity.TOP or Gravity.CENTER_HORIZONTAL, 0, 100)
toast.show()

The three parameters of setGravity() are:

  • gravity โ€” where to position the Toast. Common values are Gravity.TOP, Gravity.BOTTOM, Gravity.CENTER, and you can combine them using or.
  • xOffset โ€” horizontal offset in pixels from the gravity position.
  • yOffset โ€” vertical offset in pixels from the gravity position.

Important note: From Android 11 (API level 30) onwards, setGravity() is ignored for apps targeting API 30+. Custom positioning only works on Android 10 and below. This is a deliberate change by Google to improve consistency across devices.

Displaying a Variable in a Toast

You will often want to show dynamic values inside a Toast message โ€” a username, a count, a result. Use Kotlin string templates for this:

val username = "Randhir"
val score = 95

// Show a variable in Toast using string template
Toast.makeText(this, "Welcome, $username!", Toast.LENGTH_SHORT).show()

// Show multiple variables
Toast.makeText(this, "Your score is $score out of 100", Toast.LENGTH_LONG).show()

Kotlin string templates (the $ syntax) make this much cleaner than Java's string concatenation. For expressions, use curly braces:

val items = listOf("apple", "banana", "mango")
Toast.makeText(this, "You have ${items.size} items in your list", Toast.LENGTH_SHORT).show()

Common Mistakes to Avoid

1. Forgetting to call .show()

// โŒ Wrong โ€” Toast is created but never shown
Toast.makeText(this, "This will not appear", Toast.LENGTH_SHORT)

// โœ… Correct โ€” always chain .show() at the end
Toast.makeText(this, "This will appear", Toast.LENGTH_SHORT).show()

2. Calling Toast from a Background Thread

Toast must always be shown from the main (UI) thread. Calling it from a background thread will either crash your app or simply not work:

// โŒ Wrong โ€” calling Toast from a background thread
Thread {
    Toast.makeText(this, "This will crash", Toast.LENGTH_SHORT).show()
}.start()

// โœ… Correct โ€” run on the UI thread
runOnUiThread {
    Toast.makeText(this, "This is safe", Toast.LENGTH_SHORT).show()
}

3. Using applicationContext When Activity Context Is Needed

// โš ๏ธ This works but is not ideal inside an Activity
Toast.makeText(applicationContext, "Message", Toast.LENGTH_SHORT).show()

// โœ… Preferred inside an Activity
Toast.makeText(this, "Message", Toast.LENGTH_SHORT).show()

4. Stacking Multiple Toasts

If you call Toast multiple times quickly (for example inside a loop or on rapid button presses), Android queues them up and shows them one after another. This can result in Toast messages appearing long after the user has moved on. To avoid this, cancel any existing Toast before showing a new one:

// Declare toast at class level
private var currentToast: Toast? = null

// Then when showing a new Toast
currentToast?.cancel()
currentToast = Toast.makeText(this, "Fresh message", Toast.LENGTH_SHORT)
currentToast?.show()

Toast vs Snackbar โ€” Which One Should You Use?

A question that comes up often is when to use Toast versus Snackbar. Both display brief messages but they behave differently:

  • Toast โ€” floats above the UI, cannot be interacted with, no action button, works anywhere in the app including background services.
  • Snackbar โ€” anchored to the bottom of the screen within a specific view, can include an action button (like "UNDO"), follows Material Design guidelines more closely.

As a general rule โ€” if you just need to show a quick informational message with no action required, use Toast. If you need the user to be able to act on the message (like undoing a deletion), use Snackbar.

Quick Reference

// Basic short toast
Toast.makeText(this, "Message here", Toast.LENGTH_SHORT).show()

// Basic long toast
Toast.makeText(this, "Message here", Toast.LENGTH_LONG).show()

// Toast inside Fragment
Toast.makeText(requireContext(), "Message here", Toast.LENGTH_SHORT).show()

// Toast with variable
val name = "Developer"
Toast.makeText(this, "Hello, $name!", Toast.LENGTH_SHORT).show()

// Toast from background thread
runOnUiThread {
    Toast.makeText(this, "Safe from thread", Toast.LENGTH_SHORT).show()
}

// Cancel previous toast before showing new one
currentToast?.cancel()
currentToast = Toast.makeText(this, "New message", Toast.LENGTH_SHORT)
currentToast?.show()

Final Thought

Toast is one of those small features that you will use in almost every Android project. It takes one line to implement, but knowing the edge cases โ€” threading, stacking, Fragment context, and the API 30 gravity change โ€” will save you debugging time later.

Once you are comfortable with Toast, the next natural step is learning Snackbar for cases where the user needs to interact with the notification. The two together cover most of your in-app notification needs without reaching for a third-party library.

Have a question about a specific Toast use case in your app? Drop us a message on our contact page and we will help you figure it out.

Subscribe to Our Newsletter

Join Our Developer Community!

Unsubscribe Anytime | No Spam