# Print output for @column tags ?>
Developers

- Design
- Develop
- Distribute

Developer Console

## Most visited

## Recently visited

## Results for

Summary: Nested Classes | Constants | Ctors | Methods | Inherited Methods

# 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.

### 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 is deprecated. 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 is deprecated. 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
From class `java.lang.Object`
`Object` | `clone()` Creates and returns a copy of this object.
`boolean` | `equals(Object obj)` Indicates whether some other object is "equal to" this one.
`void` | `finalize()` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
`final Class<?>` | `getClass()` Returns the runtime class of this `Object`.
`int` | `hashCode()` Returns a hash code value for the object.
`final void` | `notify()` Wakes up a single thread that is waiting on this object's monitor.
`final void` | `notifyAll()` Wakes up all threads that are waiting on this object's monitor.
`String` | `toString()` Returns a string representation of the object.
`final void` | `wait(long timeout, int nanos)` Causes the current thread to wait until another thread invokes the `notify()` method or the `notifyAll()` method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
`final void` | `wait(long timeout)` Causes the current thread to wait until either another thread invokes the `notify()` method or the `notifyAll()` method for this object, or a specified amount of time has elapsed.
`final void` | `wait()` Causes the current thread to wait until another thread invokes the `notify()` method or the `notifyAll()` method for this object.

## Constants

### LENGTH_LONG

    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:

- `setDuration(int)`

Constant Value: 1 (0x00000001)

### LENGTH_SHORT

    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:

- `setDuration(int)`

Constant Value: 0 (0x00000000)

## Public constructors

### Toast

    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 `Application` or `Activity` object.

## Public methods

### addCallback

    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`.

See also:

- `removeCallback(Callback)`

### cancel

    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

    public int getDuration ()

Return the duration.

Returns
`int` | Value is `LENGTH_SHORT`, or `LENGTH_LONG`

See also:

- `setDuration(int)`

### getGravity

    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` |

See also:

- `Gravity`
- `getGravity()`

### getHorizontalMargin

    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

    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

    public View getView ()

This method is deprecated.
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:

- `setView(View)`

### getXOffset

    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

    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

    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 `Application` or `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

    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 `Application` or `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

    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

    public void setDuration (int duration)

Set how long to show the view for.

Parameters
`duration` | `int`: Value is `LENGTH_SHORT`, or `LENGTH_LONG`

See also:

- `LENGTH_SHORT`
- `LENGTH_LONG`

### setGravity

    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`

See also:

- `Gravity`
- `getGravity()`

### setMargin

    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

    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

    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

    public void setView (View view)

This method is deprecated.
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:

- `getView()`

### show

    public void show ()

Show the view for the specified duration.

Full name *
Email address *
Company / developer name
One of your Play Store app URLs *
Which best describes your business:*
Apps
Games
Apps & Games
*
*