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.
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:
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
ToastActivity.kt
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.
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
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.
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.