Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Toast in Kotlin
Setting Up Android Studio For Kotlin
Create an Application in Kotlin
Creating A Toast in Kotlin
Frequently Asked Questions
What is a Toast in android?
Why is the Toast message called Toast?
What is a Snackbar?
What is the difference between a Toast and a Snackbar?
What is a thread?
Last Updated: Mar 27, 2024

How to Represent Toast in Kotlin on Android?

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM


You must have seen a lot of messages that appear for a short time in dialog boxes when using any application. This message is called Toast. Toast in Kotlin is used in Android to display text for a short time.

Toast in Kotlin

Kotlin is one of the popular languages that is used for application development. This article will examine how to display a toast in Kotlin on Android.

Toast in Kotlin

The best way to learn how to code is with hands-on practice. We are going to create a sample toast in Kotlin here. Below are some examples of how we can create a toast in Kotlin:

Toast.makeText(this, “Hello! This is a toast!”, Toast.LENGTH_SHORT).show()
Toast.makeText(this, “Hello! This is a toast!”, Toast.LENGTH_LONG).show()

For the above example, the makeText method needs the context, the toast message, and the Toast duration (LENGTH_LONG or LENGTH_SHORT). After this, the show() method displays the Toast in kotlin for the time specified.

The notification message pops up at the bottom of the screen like a toast from a toaster. Thus the message is called Toast.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Setting Up Android Studio For Kotlin

To set up Kotlin in Android Studio, launch Android Studio. After that follow the following steps:

Step 1: Click on the Preferences menu.

Step 2: Click on Plugins.

Step 3: Click on Browse Repository.

Step 4: Type ‘Kotlin’ in the Search Box.

Step 5: Install Kotlin Language Plugin.


Recommended Topic, android operating system

Create an Application in Kotlin

To create an application in Kotlin, follow the following steps:

Step 1: Launch Android Studio.

Step 2: Click on Start A New Android Project.

Step 3: Type in the name of the Application in the Name Field and Check the include Kotlin Support Checkbox.

Step 4: Select API level for Android application and then click the Next button.

Step 5: Select the Activity Type.

Creating A Toast in Kotlin

After setting up Android Studio, create the following files

  1. ToastActivity.kt
  2. activity_toast.xml

Here is the code for ToastActivity.kt and activity_toast.xml. These are the files that we would be primarily focusing on when creating a sample toast in Kotlin.


<?xml version="1.0" encoding="utf-8"?>
< xmlns:android=""
         android:text="Click me for Toast"
         android:layout_height="wrap_content" />


package com.demo.myapplication
import android.widget.Button
import android.widget.Toast
import android.os.Bundle
class ToastActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        var btn_click_me = findViewById( as Button
        btn_click_me.setOnClickListener {
            // make a toast on button click event
            Toast.makeText(this, "Hello! This is a Toast.", Toast.LENGTH_LONG).show()

When the application is built and run on an Android device or is built on the virtual device in Android Studio by clicking on the ‘Run App’ button (which is a green play button), A button 'Click me For Toast' should appear. After clicking on this button, a toast message will appear at the bottom of the screen.

We can also put a separate thread for displaying toasts in the application. This would need a couple of simple tweaks in the code as

public void toast(final Context context, final String text){ 
	Handler handler = new Handler(Looper.getMainLooper()); Runnable(){ 
		public void run(){ 
			Toast.makeText(context, text, Toast.LENGTH_LONG).show(); 



After clicking on Click me for Toast, below output will appear.


This method involves getting a handler to the application's main thread and branching out a process through it to display a toast in the application.

Must Read Elvis Operator Kotlin

Frequently Asked Questions

What is a Toast in android?

A toast is a message on an application's bottom screen. This appears for a short time whenever any particular event is triggered. Since this message pops up on the screen like a toast from the toaster, it is called a Toast.

Why is the Toast message called Toast?

Toast messages are simple ways of notifying the user regarding specific events. These appear in the same manner as a toast from a toaster. Therefore these messages are called Toast.

What is a Snackbar?

The Snackbar is a lightweight notification that appears on the bottom of the screen as feedback on any activity. These appear on the lower left on large devices. Only one Snackbar can be displayed at a time.

What is the difference between a Toast and a Snackbar?

The Snackbar only contains one line of text related to the operation performed. They only have text actions in them and no icons. Toasts in Android are mainly used for system messaging. They appear on the bottom of the screen and can not be dismissed by swiping on them.

What is a thread?

Thread is a lightweight process that shares its resources with the parent thread and other siblings that it may have.


In this article, we briefly discussed what a Toast in Kotlin is. We also discussed how to set up Toast in Kotlin and assign a thread for displaying Toast.

Recommended Reading:

Also, check out some of the Guided Paths on topics such as Data Structures and Algorithms, Competitive ProgrammingBasics of Javaetc., as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Live masterclass