Toast Android Studio Kotlin

Toast Android Studio Kotlin | Toast Kotlin Working Example

Android Toast is a simple message that appears on the screen. It appears at the bottom of the screen and it provides the simple feedback about the current operation of the activity. A Toast message disappears  from the screen after a short time. So, lets discuss Toast in Android Studio with Kotlin.

 

 

 

Toast is a simple message that appears on the screen. It is displayed on the current running activity and remains for a short period of time. The public class Toast is responsible for creating and showing Toast messages.

 

 

 

Toast Android Studio Kotlin
Toast Android Studio Kotlin

 

 

What is the use of Toast in Android?

Simply, Toasts messages are used to display any information or feedback about the current task in an activity. The Toast.makeText() method is responsible for creating the Toast.

 

 

Toast Android Studio Kotlin
Toast Android Studio Kotlin

 

 

Toast Android Studio Kotlin Example:

So, open your Android Studio and we start with a new Android Studio Project. Name this project “ToastKotlin” or provide any name of your choice. Select KOTLIN as the default language and then choose any API level according to your need.

 

 

 

Toast Android Studio Kotlin
Toast Android Studio Kotlin

 

 

After your project builds you will have two files in your Android Studio. The activity_main.xml file and MainActivity.kt file. Go to your activity_main.xml file and first change the default Layout from Constraint Layout to Relative Layout because it is pretty much easier to work in Relative Layout. Then, remove the default TextView that displays the “HelloWorld” text and add a Button with wrap_content width and height. After that, below the Button take a TextView with a wrap_content width and height again.

We will add some properties in our Button and TextView. So, after that the Button and TextView in activity_main.xml file will look like this:

 

 

So, after doing all this the final activity_main.xml file will be like this:

 

 

Toast Android Studio Kotlin:

MainActivity.kt

 

In our MainActivity.kt file now we set onClickListeners on our Button and TextView which will display a Toast message. We will register onClickListener on our Views in onCreate() method like this.

 

We can also use Toast as a variable. For doing this, we will take a Variable (var) of type Toast in our MainActivity class which means it will be a global variable.

 

 

Then, we just have to put Toast message into this variable and call “show()” method. Since, Toast.makeText() method takes three parameters so we will give the context of the current activity, the message to be displayed, and time duration.

 

 

After all the coding the complete MainActivity.kt file will be like this:

 

 

 

So, this is how we create Toast messages in Android applications. I hope you understand this Toast Android Studio Kotlin example perfectly. Still, if you have any queries you can ask in comments section.

 

OUTPUT

 

 

 

 

 

 

 

      More Android Posts

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *