Toast


public class Toast
extends Object

java.lang.Object
   ↳ android.widget.Toast


A toast is a view containing a quick little message for the user. The toast class helps you create and show those.

When the view is shown to the user, appears as a floating view over the application. It will never receive focus. The user will probably be in the middle of typing something else. The idea is to be as unobtrusive as possible, while still showing the user the information you want them to see. Two examples are the volume control, and the brief message saying that your settings have been saved.

The easiest way to use this class is to call one of the static methods that constructs everything you need and returns a new Toast object.

Note that Snackbars are preferred for brief messages while the app is in the foreground.

Note that toasts being sent from the background are rate limited, so avoid sending such toasts in quick succession.

Starting with Android 12 (API level 31), apps targeting Android 12 or newer will have their toasts limited to two lines.

Developer Guides

For information about creating Toast notifications, read the Toast Notifications developer guide.

Summary

Nested classes

class Toast.Callback

Callback object to be called when the toast is shown or hidden. 

Constants

int LENGTH_LONG

Show the view or text notification for a long period of time.

int LENGTH_SHORT

Show the view or text notification for a short period of time.

Public constructors

Toast(Context context)

Construct an empty Toast object.

Public methods

void addCallback(Toast.Callback callback)

Adds a callback to be notified when the toast is shown or hidden.

void cancel()

Close the view if it's showing, or don't show it if it isn't showing yet.

int getDuration()

Return the duration.

int getGravity()

Get the location at which the notification should appear on the screen.

float getHorizontalMargin()

Return the horizontal margin.

float getVerticalMargin()

Return the vertical margin.

View getView()

This method was deprecated in API level 30. Custom toast views are deprecated. Apps can create a standard text toast with the makeText(android.content.Context, java.lang.CharSequence, int) method, or use a Snackbar when in the foreground. Starting from Android Build.VERSION_CODES.R, apps targeting API level Build.VERSION_CODES.R or higher that are in the background will not have custom toast views displayed.

int getXOffset()

Return the X offset in pixels to apply to the gravity's location.

int getYOffset()

Return the Y offset in pixels to apply to the gravity's location.

static Toast makeText(Context context, int resId, int duration)

Make a standard toast that just contains text from a resource.

static Toast makeText(Context context, CharSequence text, int duration)

Make a standard toast that just contains text.

void removeCallback(Toast.Callback callback)

Removes a callback previously added with addCallback(android.widget.Toast.Callback).

void setDuration(int duration)

Set how long to show the view for.

void setGravity(int gravity, int xOffset, int yOffset)

Set the location at which the notification should appear on the screen.

void setMargin(float horizontalMargin, float verticalMargin)

Set the margins of the view.

void setText(int resId)

Update the text in a Toast that was previously created using one of the makeText() methods.

void setText(CharSequence s)

Update the text in a Toast that was previously created using one of the makeText() methods.

void setView(View view)

This method was deprecated in API level 30. Custom toast views are deprecated. Apps can create a standard text toast with the makeText(android.content.Context, java.lang.CharSequence, int) method, or use a Snackbar when in the foreground. Starting from Android Build.VERSION_CODES.R, apps targeting API level Build.VERSION_CODES.R or higher that are in the background will not have custom toast views displayed.

void show()

Show the view for the specified duration.

Inherited methods

Constants

LENGTH_LONG

Added in API level 1
public static final int LENGTH_LONG

Show the view or text notification for a long period of time. This time could be user-definable.

See also:

Constant Value: 1 (0x00000001)

LENGTH_SHORT

Added in API level 1
public static final int LENGTH_SHORT

Show the view or text notification for a short period of time. This time could be user-definable. This is the default.

See also:

Constant Value: 0 (0x00000000)

Public constructors

Toast

Added in API level 1
public Toast (Context context)

Construct an empty Toast object. You must call setView(View) before you can call show().

Parameters
context Context: The context to use. Usually your Activity object.

Public methods

addCallback

Added in API level 30
public void addCallback (Toast.Callback callback)

Adds a callback to be notified when the toast is shown or hidden. Note that if the toast is blocked for some reason you won't get a call back.

Parameters
callback Toast.Callback: This value cannot be null.

cancel

Added in API level 1
public void cancel ()

Close the view if it's showing, or don't show it if it isn't showing yet. You do not normally have to call this. Normally view will disappear on its own after the appropriate duration.

getDuration

Added in API level 1
public int getDuration ()

Return the duration.

Returns
int Value is LENGTH_SHORT, or LENGTH_LONG

See also:

getGravity

Added in API level 1
public int getGravity ()

Get the location at which the notification should appear on the screen.

Warning: Starting from Android Build.VERSION_CODES.R, for apps targeting API level Build.VERSION_CODES.R or higher, this method shouldn't be called on text toasts as its return value may not reflect actual value since text toasts are not rendered by the app anymore.

Returns
int

getHorizontalMargin

Added in API level 1
public float getHorizontalMargin ()

Return the horizontal margin.

Warning: Starting from Android Build.VERSION_CODES.R, for apps targeting API level Build.VERSION_CODES.R or higher, this method shouldn't be called on text toasts as its return value may not reflect actual value since text toasts are not rendered by the app anymore.

Returns
float

getVerticalMargin

Added in API level 1
public float getVerticalMargin ()

Return the vertical margin.

Warning: Starting from Android Build.VERSION_CODES.R, for apps targeting API level Build.VERSION_CODES.R or higher, this method shouldn't be called on text toasts as its return value may not reflect actual value since text toasts are not rendered by the app anymore.

Returns
float

getView

Added in API level 1
Deprecated in API level 30
public View getView ()

This method was deprecated in API level 30.
Custom toast views are deprecated. Apps can create a standard text toast with the makeText(android.content.Context, java.lang.CharSequence, int) method, or use a Snackbar when in the foreground. Starting from Android Build.VERSION_CODES.R, apps targeting API level Build.VERSION_CODES.R or higher that are in the background will not have custom toast views displayed.

Return the view.

Toasts constructed with Toast(android.content.Context) that haven't called setView(android.view.View) with a non-null view will return null here.

Starting from Android Build.VERSION_CODES.R, in apps targeting API level Build.VERSION_CODES.R or higher, toasts constructed with makeText(android.content.Context, java.lang.CharSequence, int) or its variants will also return null here unless they had called setView(android.view.View) with a non-null view. If you want to be notified when the toast is shown or hidden, use addCallback(android.widget.Toast.Callback).

Returns
View

See also:

getXOffset

Added in API level 1
public int getXOffset ()

Return the X offset in pixels to apply to the gravity's location.

Warning: Starting from Android Build.VERSION_CODES.R, for apps targeting API level Build.VERSION_CODES.R or higher, this method shouldn't be called on text toasts as its return value may not reflect actual value since text toasts are not rendered by the app anymore.

Returns
int

getYOffset

Added in API level 1
public int getYOffset ()

Return the Y offset in pixels to apply to the gravity's location.

Warning: Starting from Android Build.VERSION_CODES.R, for apps targeting API level Build.VERSION_CODES.R or higher, this method shouldn't be called on text toasts as its return value may not reflect actual value since text toasts are not rendered by the app anymore.

Returns
int

makeText

Added in API level 1
public static Toast makeText (Context context, 
                int resId, 
                int duration)

Make a standard toast that just contains text from a resource.

Parameters
context Context: The context to use. Usually your Activity object.

resId int: The resource id of the string resource to use. Can be formatted text.

duration int: How long to display the message. Either LENGTH_SHORT or LENGTH_LONG Value is LENGTH_SHORT, or LENGTH_LONG

Returns
Toast

Throws
Resources.NotFoundException if the resource can't be found.

makeText

Added in API level 1
public static Toast makeText (Context context, 
                CharSequence text, 
                int duration)

Make a standard toast that just contains text.

Parameters
context Context: The context to use. Usually your Activity object.

text CharSequence: The text to show. Can be formatted text.

duration int: How long to display the message. Either LENGTH_SHORT or LENGTH_LONG Value is LENGTH_SHORT, or LENGTH_LONG

Returns
Toast

removeCallback

Added in API level 30
public void removeCallback (Toast.Callback callback)

Removes a callback previously added with addCallback(android.widget.Toast.Callback).

Parameters
callback Toast.Callback: This value cannot be null.

setDuration

Added in API level 1
public void setDuration (int duration)

Set how long to show the view for.

Parameters
duration int: Value is LENGTH_SHORT, or LENGTH_LONG

setGravity

Added in API level 1
public void setGravity (int gravity, 
                int xOffset, 
                int yOffset)

Set the location at which the notification should appear on the screen.

Warning: Starting from Android Build.VERSION_CODES.R, for apps targeting API level Build.VERSION_CODES.R or higher, this method is a no-op when called on text toasts.

Parameters
gravity int

xOffset int

yOffset int

setMargin

Added in API level 1
public void setMargin (float horizontalMargin, 
                float verticalMargin)

Set the margins of the view.

Warning: Starting from Android Build.VERSION_CODES.R, for apps targeting API level Build.VERSION_CODES.R or higher, this method is a no-op when called on text toasts.

Parameters
horizontalMargin float: The horizontal margin, in percentage of the container width, between the container's edges and the notification

verticalMargin float: The vertical margin, in percentage of the container height, between the container's edges and the notification

setText

Added in API level 1
public void setText (int resId)

Update the text in a Toast that was previously created using one of the makeText() methods.

Parameters
resId int: The new text for the Toast.

setText

Added in API level 1
public void setText (CharSequence s)

Update the text in a Toast that was previously created using one of the makeText() methods.

Parameters
s CharSequence: The new text for the Toast.

setView

Added in API level 1
Deprecated in API level 30
public void setView (View view)

This method was deprecated in API level 30.
Custom toast views are deprecated. Apps can create a standard text toast with the makeText(android.content.Context, java.lang.CharSequence, int) method, or use a Snackbar when in the foreground. Starting from Android Build.VERSION_CODES.R, apps targeting API level Build.VERSION_CODES.R or higher that are in the background will not have custom toast views displayed.

Set the view to show.

Parameters
view View

See also:

show

Added in API level 1
public void show ()

Show the view for the specified duration.

Note that toasts being sent from the background are rate limited, so avoid sending such toasts in quick succession.