> Documentation index: https://developer.android.com/reference/llms.txt

Summary: [Nested Classes](https://developer.android.com/reference/android/widget/Toast.md.txt#nestedclasses) \| [Constants](https://developer.android.com/reference/android/widget/Toast.md.txt#constants) \| [Ctors](https://developer.android.com/reference/android/widget/Toast.md.txt#pubctors) \| [Methods](https://developer.android.com/reference/android/widget/Toast.md.txt#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/widget/Toast.md.txt#inhmethods)

# Toast

*** ** * ** ***


`
public


class
Toast
`


`

extends Object


`

`


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.md.txt) ||
| ↳ | android.widget.Toast |

<br />

*** ** * ** ***

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](https://developer.android.com/reference/com/google/android/material/snackbar/Snackbar.md.txt) 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](https://developer.android.com/guide/topics/ui/notifiers/toasts.md.txt) developer
guide.

<br />

## 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(Context,CharSequence,int)` method, or use a [Snackbar](https://developer.android.com/reference/com/google/android/material/snackbar/Snackbar.md.txt) 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(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(Context,CharSequence,int)` method, or use a [Snackbar](https://developer.android.com/reference/com/google/android/material/snackbar/Snackbar.md.txt) 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 timeoutMillis, int nanos)
`

Causes the current thread to wait until it is awakened, typically
by being *notified* or *interrupted*, or until a
certain amount of real time has elapsed.


`


final

void` `
wait(long timeoutMillis)
`

Causes the current thread to wait until it is awakened, typically
by being *notified* or *interrupted*, or until a
certain amount of real time has elapsed.


`


final

void` `
wait()
`

Causes the current thread to wait until it is awakened, typically
by being *notified* or *interrupted*.


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

<br />

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

<br />

**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()`.

<br />

| Parameters ||
|---|---|
| `context` | `Context`: The context to use. Usually your `Activity` object. <br /> |

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

<br />

| Parameters ||
|---|---|
| `callback` | `Toast.Callback`: This value cannot be `null`. <br /> |

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

<br />

### getDuration

```
public int getDuration ()
```

Return the duration.

<br />

| Returns ||
|---|---|
| `int` | Value is one of the following: - `LENGTH_SHORT` - `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.

<br />

| Returns ||
|---|---|
| `int` | <br /> |

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

<br />

| Returns ||
|---|---|
| `float` | <br /> |

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

<br />

| Returns ||
|---|---|
| `float` | <br /> |

### getView

<br />

Deprecated in [API level
30](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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(Context,CharSequence,int)` method, or use a
[Snackbar](https://developer.android.com/reference/com/google/android/material/snackbar/Snackbar.md.txt)
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(Context)` that haven't called `setView(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(Context,CharSequence,int)` or its variants will also return `null` here unless they had called
`setView(View)` with a non-`null` view. If you want to be notified when the
toast is shown or hidden, use `addCallback(Callback)`.

<br />

| Returns ||
|---|---|
| `View` | <br /> |

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

<br />

| Returns ||
|---|---|
| `int` | <br /> |

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

<br />

| Returns ||
|---|---|
| `int` | <br /> |

### makeText

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

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

<br />

| Parameters ||
|---|---|
| `context` | `Context`: The context to use. Usually your `Activity` object. <br /> |
| `resId` | `int`: The resource id of the string resource to use. Can be formatted text. <br /> |
| `duration` | `int`: How long to display the message. Either `LENGTH_SHORT` or `LENGTH_LONG` Value is one of the following: - `LENGTH_SHORT` - `LENGTH_LONG` |

| Returns ||
|---|---|
| `Toast` | <br /> |

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

<br />

| Parameters ||
|---|---|
| `context` | `Context`: The context to use. Usually your `Activity` object. <br /> |
| `text` | `CharSequence`: The text to show. Can be formatted text. <br /> |
| `duration` | `int`: How long to display the message. Either `LENGTH_SHORT` or `LENGTH_LONG` Value is one of the following: - `LENGTH_SHORT` - `LENGTH_LONG` |

| Returns ||
|---|---|
| `Toast` | <br /> |

### removeCallback

```
public void removeCallback (Toast.Callback callback)
```

Removes a callback previously added with `addCallback(Callback)`.

<br />

| Parameters ||
|---|---|
| `callback` | `Toast.Callback`: This value cannot be `null`. <br /> |

### setDuration

```
public void setDuration (int duration)
```

Set how long to show the view for.

<br />

| Parameters ||
|---|---|
| `duration` | `int`: Value is one of the following: - `LENGTH_SHORT` - `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.

<br />

| Parameters ||
|---|---|
| `gravity` | `int` <br /> |
| `xOffset` | `int` <br /> |
| `yOffset` | `int` <br /> |

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

<br />

| Parameters ||
|---|---|
| `horizontalMargin` | `float`: The horizontal margin, in percentage of the container width, between the container's edges and the notification <br /> |
| `verticalMargin` | `float`: The vertical margin, in percentage of the container height, between the container's edges and the notification <br /> |

### setText

```
public void setText (int resId)
```

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

<br />

| Parameters ||
|---|---|
| `resId` | `int`: The new text for the Toast. <br /> |

### setText

```
public void setText (CharSequence s)
```

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

<br />

| Parameters ||
|---|---|
| `s` | `CharSequence`: The new text for the Toast. <br /> |

### setView

<br />

Deprecated in [API level
30](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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(Context,CharSequence,int)` method, or use a
[Snackbar](https://developer.android.com/reference/com/google/android/material/snackbar/Snackbar.md.txt)
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.

<br />

| Parameters ||
|---|---|
| `view` | `View` <br /> |

**See also:**

- `getView()`

### show

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

<br />