Any application relies on successful communication between the app and the user. A great way to let the user know what's going on is by notifying them with short messages. One of the ways to do this is through Toast. In this topic, you'll learn how to use Toasts in your application.
What is a Toast?
A Toast is a message that appears on the surface of the application window, filling the amount of space it needs for the message. Toasts are generally used to display short messages.
How to create a Toast message
To create a Toast, you need to pass arguments to three parameters: context, text, and duration.
context is used to read resources and access the operating system.
text is the message that you want to show:
val text = "User's text"And duration defines how long the Toast will be displayed:
val duration = Toast.LENGTH_SHORTYou initialize the Toast object with these arguments using the Toast.makeText() method, and then call the show() method to display the message on the screen:
val toast = Toast.makeText(context, text, duration)
toast.show()So, what options do you have when specifying the Toast message's duration? Well, there are actually only two: LENGTH_SHORT and LENGTH_LONG. A Toast message lasts for several seconds, and this duration is not strictly defined.
You can also add text to the Toast from the res/values/strings.xml file instead of directly in the code. To do this, just place the required string in the strings.xml file and pass the text to the parameters using the getString() method.
val toast = Toast.makeText(applicationContext, getString(R.string.your_id),Toast.LENGTH_SHORT)The two lines of code in the previous example are often combined to make them easier to read:
Toast.makeText(context, text, duration).show()Remembering to add the show() method at the end of your Toast code line is a must. Luckily, in Android Studio, if you skip the show() method, the line will be highlighted. Then, when you hover over it, you'll see a warning message.
Toast cancel() method
You can use the cancel() method to remove a Toast from the screen if the information it contains is no longer relevant. This situation could arise if the user has moved to the next or previous screen and a long Toast message is still being displayed.
Using the cancel() method in your code will look something like the example shown below:
val toast = Toast.makeText(context, text, duration)
toast.show()
...
toast.cancel()Toast.Callback
Toast.Callback was added in Android 11 R (API 30). It allows you to identify the exact point when a Toast appears and disappears, as shown below:
val toast = Toast.makeText(this, "Here will be your text", Toast.LENGTH_SHORT)
toast.addCallback(object: Toast.Callback() {
override fun onToastShown() {
// Your code here
}
override fun onToastHidden() {
// Your code here
}
})
toast.show()
It's best not to use Toast messages too often. Otherwise, you may run into problems caused by system restrictions that you cannot resolve.
Conclusion
Toast is one of the most popular ways to show a short message while an app is in use. It's easy to use and integrate into your application. However, don't forget that overusing Toasts can negatively affect the evaluation of your application.