android-studio
  1. android-studio-toast

Android Studio Toast

Syntax

Toast.makeText(Context context, CharSequence text, int duration).show();

Example

Toast.makeText(MainActivity.this, "Hello World!", Toast.LENGTH_SHORT).show();

Output

A small pop-up appears at the bottom of the screen, displaying the message "Hello World!" for a short duration.

Explanation

Toast is a small pop-up message that appears for a short duration to provide feedback to the user. It is often used to display information such as the status of an operation or to provide a quick confirmation message to the user.

In the above example, we use the makeText() method of the Toast class to create a new toast message. The first parameter is the Context object, which is usually the Activity that is currently running. The second parameter is the text that we want to display in the toast message. The third parameter is the duration for which we want the toast message to appear, which can be either Toast.LENGTH_SHORT or Toast.LENGTH_LONG.

After creating the toast message, we call the show() method to display it on the screen.

Use

Toasts are commonly used to provide quick feedback to the user, such as when an operation has completed successfully or when an error has occurred. They are useful when you want to display a message to the user that doesn't require them to take any action.

Important Points

  • Toasts are short-lived and disappear after a brief duration.
  • To control the duration for which a toast message appears on screen, use the duration parameter while creating the toast message.
  • To customize the appearance and behavior of the toast message, you can create a custom layout and use the setView() method of the Toast class to set it as the view for the toast message.

Summary

Toast is a small pop-up message that appears for a short duration on the screen. It is used to provide feedback to the user about the status of an operation or to display a quick confirmation message. Toasts are created using the makeText() method of the Toast class and can be customized by using a custom layout and the setView() method.

Published on: