Kotlin Android Toast

Topics Covered

Overview

In this article, we will look into the concept of Toasts in Android app development using Kotlin. Toasts are an essential part of the Android UI toolkit, helping developers to provide concise, non-interfering messages or notifications to users. In the article, we will cover the basics of Toast in Kotlin, including how to instantiate and show them, how to customise their appearance and behaviour, discuss alternatives to using Toasts, and look into a Kotlin android toast example.

What is Toast in Android?

Toast in android is a type of short popup notification which is used to display information to the user about any operation we perform in our app without disturbing the other functionalities. Toasts are widely used in Android app development due to their non-intrusive nature and ability to display short messages effectively. They are particularly helpful for displaying quick alerts, success messages, or warning notifications. Toast mainly includes three primary components:

  1. Text: This is the actual message that you want to display to the user. It could be anything like a simple notification, a success message or an error description.
  2. Duration: This is the time span for which the toast remains visible on the screen. There are two commonly used lengths to display the toasts. a.LENGTH_SHORT: This duration is typically around two seconds. It is suitable for displaying brief messages that the user can quickly read and understand. b. LENGTH_LONG: This duration is slightly longer and is usually around 3.5 seconds. It is suitable for messages that may require a bit more time to read and understand by the user.
  3. Layout: The toast message is displayed in a small, customizable layout generally at the bottom of the screen.

Let's take an example to understand the importance of toasts: Imagine you're designing a mobile app that involves sending and receiving messages. You've just finished implementing a feature that allows users to send messages to each other. Now, you're testing the app, and you notice that after sending a message, there's no visual feedback to let the user know that the message was successfully sent.

This is the scenario where toasts come into act. A toast is a small, non-intrusive message that appears briefly on the screen, to provide users with important information or feedback. In this scenario, you realize that by adding a toast notification that says "Message Sent!" whenever a user successfully sends a message, you greatly enhance the user experience and engagement of the user.

Note: There were considerable changes in toasts from Android 12 or higher, Now the toast is limited to two lines of text and shows the application icon next to the text and developers can even customize the duration for which a Toast notification remains visible.

Alternatives to Using Toasts

We know that toasts offer a convenient and unobtrusive way to communicate with users but there are many situations in which alternatives to toasts might be more suitable to use. There are many alternatives to using toasts such as:

  1. Snackbar: Snackbars are UI component in Android that provide brief messages to users. Snackbars provide more visual flexibility, allowing for longer messages and additional actions. Snackbar are useful when interaction is required and are also useful for scenarios where the user can resolve the situation by clicking a button within the Snackbar itself.
  2. Dialogs: Dialogs are modal UI components that can display complex information in a prominent and modal manner, such as forms or detailed messages. Dialogs require user interaction. Dialogs are useful when you need the user to make a critical decision or select from multiple options and are also useful when presenting users with detailed information, terms of use, privacy policies, or explanations about certain actions etc.
  3. Status Bar Notifications: Status Bar Notifications provide the facility of persistent and expandable notifications that remain visible until dismissed by the user. Status Bar Notifications are useful when the information you want to communicate is relevant for a longer period of time and are also useful when you need to display multiple notifications.

Instantiate a Toast Object

For creating a toast in android we need to instantiate a Toast object. This object will act as a container for the toast message and provides various methods to customise its behaviour. To instantiate a Toast object we will need a Context which is usually an Activity or an Application context. We can say that a Context is typically an instance of the android.content.Context class that represents the current state of your application, providing access to the resources, system services, and other application-related information. When it comes to displaying Toast, it is generally recommended to use the ActivityContext rather than the ApplicationContext because Toasts are meant to provide feedback to the user within the context of their current activity and by using the ActivityContext we ensure that the Toast appears in the appropriate activity and is visually associated with the user's ongoing interaction but in contrast by using the ApplicationContext for displaying Toasts could lead to unexpected behavior, and the Toast might not be associated with the current activity or might not follow the activity's lifecycle.

We Can Instantiate a Toast Object in Kotlin as Follows

In the above given example, the Toast.makeText() method is used to create a toast object. It takes three parameters: a Context which is usually the activity or application context, the message we want to display in the toast, and the duration for which the toast should appear on the android screen.

Show the Toast

We can display the toast by calling the show() method on the toast object. It is very important to remember that the toast won't be visible if we not call the show() method on the toast object.

We Can Display the Toast in Kotlin as Follows:

The above given code will display a short toast message with the text "Hello, I am a Toast!" using the provided Context.

Chain Your Toast Method Calls

Chaining involves calling multiple methods on the same object in sequence, which can make the code easier to understand and maintain. Chaining methods in Kotlin can lead to more concise and readable code. By chaining methods, we will be able to eliminate the need for temporary variables and also able to avoid holding on to the Toast object.

We Can Chain Our Toast Method Calls as Shown in the Following Code Snippet:

In the above example, we are chaining the makeText() method with the show() method by directly using it in sequence on the Toast object. This will create a more concise and readable code structure.

Kotlin Android Toast Example

Now, we will learn more about toast in kotlin with the help of an example. In this example, we will display a toast message by clicking on a Button. In android studio, Create a new project

Add the Following Code in the activity_main.xml File.

In the above code we have added a button to the layout to perform a click action.

Add the following code in the MainActivity.kt file.

In the above code, we are doing a click action on the button and when the button will be clicked the toast message will be displayed on the screen.

Output

kotlin-output

...

result

Conclusion

  • Toasts are a valuable tool for displaying short and non-intrusive notification to the users.
  • Since toasts are customisable, it became easier for developers to use the toasts in own way to suit their app's needs.
  • Chaining methods is often used with toast class because the process of creating and displaying a toast is straightforward and involves sequential steps.
  • While toasts are useful in many ways, it is important to consider alternative notification methods based on the requirement of the message.