Table of contents
1.
Introduction
2.
Toast in Kotlin
3.
Setting Up Android Studio For Kotlin
4.
Create an Application in Kotlin
5.
Creating A Toast in Kotlin
5.1.
Output
6.
Frequently Asked Questions
6.1.
What is a Toast in android?
6.2.
Why is the Toast message called Toast?
6.3.
What is a Snackbar?
6.4.
What is the difference between a Toast and a Snackbar?
6.5.
What is a thread?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

How to Represent Toast in Kotlin on Android?

Introduction

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.

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.

activity_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
    tools:context="com.demo.myapplication.ToastActivity">
    <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:gravity="center"
     android:padding="25dp"
     android:orientation="horizontal">
        <Button
         android:id="@+id/btn_click_me"
         android:background="@drawable/btn_round_edge"
         android:text="Click me for Toast"
         android:textAllCaps="false"
         android:padding="10dp"
         android:textSize="25dp"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>


ToastActivity.kt

package com.demo.myapplication
 
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import android.widget.Toast
import android.os.Bundle
 
class ToastActivity : AppCompatActivity() {
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_toast)
 
        var btn_click_me = findViewById(R.id.btn_click_me) 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()); 
	handler.post(new Runnable(){ 
		public void run(){ 
			Toast.makeText(context, text, Toast.LENGTH_LONG).show(); 
		} 
	});
}

Output

Output

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

Output2

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.

Conclusion

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