Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Nested Classes](https://developer.android.com/reference/android/widget/Toast#nestedclasses) \| [Constants](https://developer.android.com/reference/android/widget/Toast#constants) \| [Ctors](https://developer.android.com/reference/android/widget/Toast#pubctors) \| [Methods](https://developer.android.com/reference/android/widget/Toast#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/widget/Toast#inhmethods)

# Toast

*** ** * ** ***

` public class Toast `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | 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.

<br />

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) 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) developer guide.

<br />

## Summary

| ### Nested classes ||
|---|---|
| ` class` | `https://developer.android.com/reference/android/widget/Toast.Callback` Callback object to be called when the toast is shown or hidden. |

| ### Constants ||
|---|---|
| `int` | `https://developer.android.com/reference/android/widget/Toast#LENGTH_LONG` Show the view or text notification for a long period of time. |
| `int` | `https://developer.android.com/reference/android/widget/Toast#LENGTH_SHORT` Show the view or text notification for a short period of time. |

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/widget/Toast#Toast(android.content.Context)(https://developer.android.com/reference/android/content/Context context) ` Construct an empty Toast object. |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/widget/Toast#addCallback(android.widget.Toast.Callback)(https://developer.android.com/reference/android/widget/Toast.Callback callback) ` Adds a callback to be notified when the toast is shown or hidden. |
| ` void` | ` https://developer.android.com/reference/android/widget/Toast#cancel()() ` Close the view if it's showing, or don't show it if it isn't showing yet. |
| ` int` | ` https://developer.android.com/reference/android/widget/Toast#getDuration()() ` Return the duration. |
| ` int` | ` https://developer.android.com/reference/android/widget/Toast#getGravity()() ` Get the location at which the notification should appear on the screen. |
| ` float` | ` https://developer.android.com/reference/android/widget/Toast#getHorizontalMargin()() ` Return the horizontal margin. |
| ` float` | ` https://developer.android.com/reference/android/widget/Toast#getVerticalMargin()() ` Return the vertical margin. |
| ` https://developer.android.com/reference/android/view/View` | ` https://developer.android.com/reference/android/widget/Toast#getView()() ` *This method was deprecated in API level 30. Custom toast views are deprecated. Apps can create a standard text toast with the `https://developer.android.com/reference/android/widget/Toast#makeText(android.content.Context,%20java.lang.CharSequence,%20int)` method, or use a [Snackbar](https://developer.android.com/reference/com/google/android/material/snackbar/Snackbar) when in the foreground. Starting from Android `https://developer.android.com/reference/android/os/Build.VERSION_CODES#R`, apps targeting API level `https://developer.android.com/reference/android/os/Build.VERSION_CODES#R` or higher that are in the background will not have custom toast views displayed.* |
| ` int` | ` https://developer.android.com/reference/android/widget/Toast#getXOffset()() ` Return the X offset in pixels to apply to the gravity's location. |
| ` int` | ` https://developer.android.com/reference/android/widget/Toast#getYOffset()() ` Return the Y offset in pixels to apply to the gravity's location. |
| ` static https://developer.android.com/reference/android/widget/Toast` | ` https://developer.android.com/reference/android/widget/Toast#makeText(android.content.Context,%20int,%20int)(https://developer.android.com/reference/android/content/Context context, int resId, int duration) ` Make a standard toast that just contains text from a resource. |
| ` static https://developer.android.com/reference/android/widget/Toast` | ` https://developer.android.com/reference/android/widget/Toast#makeText(android.content.Context,%20java.lang.CharSequence,%20int)(https://developer.android.com/reference/android/content/Context context, https://developer.android.com/reference/java/lang/CharSequence text, int duration) ` Make a standard toast that just contains text. |
| ` void` | ` https://developer.android.com/reference/android/widget/Toast#removeCallback(android.widget.Toast.Callback)(https://developer.android.com/reference/android/widget/Toast.Callback callback) ` Removes a callback previously added with `https://developer.android.com/reference/android/widget/Toast#addCallback(android.widget.Toast.Callback)`. |
| ` void` | ` https://developer.android.com/reference/android/widget/Toast#setDuration(int)(int duration) ` Set how long to show the view for. |
| ` void` | ` https://developer.android.com/reference/android/widget/Toast#setGravity(int,%20int,%20int)(int gravity, int xOffset, int yOffset) ` Set the location at which the notification should appear on the screen. |
| ` void` | ` https://developer.android.com/reference/android/widget/Toast#setMargin(float,%20float)(float horizontalMargin, float verticalMargin) ` Set the margins of the view. |
| ` void` | ` https://developer.android.com/reference/android/widget/Toast#setText(int)(int resId) ` Update the text in a Toast that was previously created using one of the makeText() methods. |
| ` void` | ` https://developer.android.com/reference/android/widget/Toast#setText(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence s) ` Update the text in a Toast that was previously created using one of the makeText() methods. |
| ` void` | ` https://developer.android.com/reference/android/widget/Toast#setView(android.view.View)(https://developer.android.com/reference/android/view/View view) ` *This method was deprecated in API level 30. Custom toast views are deprecated. Apps can create a standard text toast with the `https://developer.android.com/reference/android/widget/Toast#makeText(android.content.Context,%20java.lang.CharSequence,%20int)` method, or use a [Snackbar](https://developer.android.com/reference/com/google/android/material/snackbar/Snackbar) when in the foreground. Starting from Android `https://developer.android.com/reference/android/os/Build.VERSION_CODES#R`, apps targeting API level `https://developer.android.com/reference/android/os/Build.VERSION_CODES#R` or higher that are in the background will not have custom toast views displayed.* |
| ` void` | ` https://developer.android.com/reference/android/widget/Toast#show()() ` Show the view for the specified duration. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(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` ` https://developer.android.com/reference/java/lang/Object#wait(long)(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` ` https://developer.android.com/reference/java/lang/Object#wait()() `

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

## Constants

### LENGTH_LONG

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

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

- `https://developer.android.com/reference/android/widget/Toast#setDuration(int)`

Constant Value: 1 (0x00000001)

### LENGTH_SHORT

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

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

- `https://developer.android.com/reference/android/widget/Toast#setDuration(int)`

Constant Value: 0 (0x00000000)

## Public constructors

### Toast

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

```
public Toast (Context context)
```

Construct an empty Toast object. You must call `https://developer.android.com/reference/android/widget/Toast#setView(android.view.View)` before you can call `https://developer.android.com/reference/android/widget/Toast#show()`.

<br />

| Parameters ||
|---|---|
| `context` | `Context`: The context to use. Usually your `https://developer.android.com/reference/android/app/Activity` object. <br /> |

## Public methods

### addCallback

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

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

- `https://developer.android.com/reference/android/widget/Toast#removeCallback(android.widget.Toast.Callback)`

### cancel

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

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

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

```
public int getDuration ()
```

Return the duration.

<br />

| Returns ||
|---|---|
| `int` | Value is one of the following: - `https://developer.android.com/reference/android/widget/Toast#LENGTH_SHORT` - `https://developer.android.com/reference/android/widget/Toast#LENGTH_LONG` |

**See also:**

- `https://developer.android.com/reference/android/widget/Toast#setDuration(int)`

### getGravity

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

```
public int getGravity ()
```

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

**Warning:** Starting from Android `https://developer.android.com/reference/android/os/Build.VERSION_CODES#R`, for apps targeting API level `https://developer.android.com/reference/android/os/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:**

- `https://developer.android.com/reference/android/view/Gravity`
- `https://developer.android.com/reference/android/widget/Toast#getGravity()`

### getHorizontalMargin

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

```
public float getHorizontalMargin ()
```

Return the horizontal margin.

**Warning:** Starting from Android `https://developer.android.com/reference/android/os/Build.VERSION_CODES#R`, for apps targeting API level `https://developer.android.com/reference/android/os/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

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

```
public float getVerticalMargin ()
```

Return the vertical margin.

**Warning:** Starting from Android `https://developer.android.com/reference/android/os/Build.VERSION_CODES#R`, for apps targeting API level `https://developer.android.com/reference/android/os/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

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

```
public View getView ()
```

<br />

**This method was deprecated in API level 30.**   

Custom toast views are deprecated. Apps can create a standard text toast with the `https://developer.android.com/reference/android/widget/Toast#makeText(android.content.Context,%20java.lang.CharSequence,%20int)` method, or use a [Snackbar](https://developer.android.com/reference/com/google/android/material/snackbar/Snackbar) when in the foreground. Starting from Android `https://developer.android.com/reference/android/os/Build.VERSION_CODES#R`, apps targeting API level `https://developer.android.com/reference/android/os/Build.VERSION_CODES#R` or higher that are in the background will not have custom toast views displayed.

Return the view.

Toasts constructed with `https://developer.android.com/reference/android/widget/Toast#Toast(android.content.Context)` that haven't called `https://developer.android.com/reference/android/widget/Toast#setView(android.view.View)` with a non-`null` view will return `null` here.

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

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/view/View` | <br /> |

**See also:**

- `https://developer.android.com/reference/android/widget/Toast#setView(android.view.View)`

### getXOffset

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

```
public int getXOffset ()
```

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

**Warning:** Starting from Android `https://developer.android.com/reference/android/os/Build.VERSION_CODES#R`, for apps targeting API level `https://developer.android.com/reference/android/os/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

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

```
public int getYOffset ()
```

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

**Warning:** Starting from Android `https://developer.android.com/reference/android/os/Build.VERSION_CODES#R`, for apps targeting API level `https://developer.android.com/reference/android/os/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

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

```
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 `https://developer.android.com/reference/android/app/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 `https://developer.android.com/reference/android/widget/Toast#LENGTH_SHORT` or `https://developer.android.com/reference/android/widget/Toast#LENGTH_LONG` Value is one of the following: - `https://developer.android.com/reference/android/widget/Toast#LENGTH_SHORT` - `https://developer.android.com/reference/android/widget/Toast#LENGTH_LONG` |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/widget/Toast` | <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/android/content/res/Resources.NotFoundException` | if the resource can't be found. |

### makeText

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

```
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 `https://developer.android.com/reference/android/app/Activity` object. <br /> |
| `text` | `CharSequence`: The text to show. Can be formatted text. <br /> |
| `duration` | `int`: How long to display the message. Either `https://developer.android.com/reference/android/widget/Toast#LENGTH_SHORT` or `https://developer.android.com/reference/android/widget/Toast#LENGTH_LONG` Value is one of the following: - `https://developer.android.com/reference/android/widget/Toast#LENGTH_SHORT` - `https://developer.android.com/reference/android/widget/Toast#LENGTH_LONG` |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/widget/Toast` | <br /> |

### removeCallback

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

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

Removes a callback previously added with `https://developer.android.com/reference/android/widget/Toast#addCallback(android.widget.Toast.Callback)`.

<br />

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

### setDuration

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

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

Set how long to show the view for.

<br />

| Parameters ||
|---|---|
| `duration` | `int`: Value is one of the following: - `https://developer.android.com/reference/android/widget/Toast#LENGTH_SHORT` - `https://developer.android.com/reference/android/widget/Toast#LENGTH_LONG` |

**See also:**

- `https://developer.android.com/reference/android/widget/Toast#LENGTH_SHORT`
- `https://developer.android.com/reference/android/widget/Toast#LENGTH_LONG`

### setGravity

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

```
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 `https://developer.android.com/reference/android/os/Build.VERSION_CODES#R`, for apps targeting API level `https://developer.android.com/reference/android/os/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:**

- `https://developer.android.com/reference/android/view/Gravity`
- `https://developer.android.com/reference/android/widget/Toast#getGravity()`

### setMargin

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

```
public void setMargin (float horizontalMargin, 
                float verticalMargin)
```

Set the margins of the view.

**Warning:** Starting from Android `https://developer.android.com/reference/android/os/Build.VERSION_CODES#R`, for apps targeting API level `https://developer.android.com/reference/android/os/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

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

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

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

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

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

```
public void setView (View view)
```

<br />

**This method was deprecated in API level 30.**   

Custom toast views are deprecated. Apps can create a standard text toast with the `https://developer.android.com/reference/android/widget/Toast#makeText(android.content.Context,%20java.lang.CharSequence,%20int)` method, or use a [Snackbar](https://developer.android.com/reference/com/google/android/material/snackbar/Snackbar) when in the foreground. Starting from Android `https://developer.android.com/reference/android/os/Build.VERSION_CODES#R`, apps targeting API level `https://developer.android.com/reference/android/os/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:**

- `https://developer.android.com/reference/android/widget/Toast#getView()`

### show

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

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