top of page

Android Toast

A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. Toasts automatically disappear after a timeout.


For example, clicking Send we get a message a "Hi there! This is a toast." toast, as shown in the following screen capture:





If your app targets Android 12 (API level 31) or higher, its toast is limited to two lines of text and shows the application icon next to the text. Be aware that the line length of this text varies by screen size, so it's good to make the text as short as possible.


Instantiate a Toast object

Use makeToast() method, which takes the following parameters:

  1. The application Context.

  2. The text should appear to the user.

  3. The duration that the toast should remain on the screen.

The makeText() method returns a properly initialized Toast object.


Demonstration of toast code example in java:-


Context context = getApplicationContext(); CharSequence text = "Hello toast!"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show();

Commenti


bottom of page