Added in API level 1

Toast

open class Toast
kotlin.Any
   ↳ 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. {@more}

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.

Summary

Nested classes
abstract

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

Constants
static Int

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

static Int

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

Public constructors
Toast(context: Context!)

Construct an empty Toast object.

Public methods
open Unit

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

open Unit

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

open Int

Return the duration.

open Int

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

open Float

Return the horizontal margin.

open Float

Return the vertical margin.

open View?

Return the view.

open Int

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

open Int

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

open static Toast!
makeText(context: Context!, text: CharSequence!, duration: Int)

Make a standard toast that just contains text.

open static Toast!
makeText(context: Context!, resId: Int, duration: Int)

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

open Unit

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

open Unit
setDuration(duration: Int)

Set how long to show the view for.

open Unit
setGravity(gravity: Int, xOffset: Int, yOffset: Int)

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

open Unit
setMargin(horizontalMargin: Float, verticalMargin: Float)

Set the margins of the view.

open Unit
setText(resId: Int)

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

open Unit

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

open Unit
setView(view: View!)

Set the view to show.

open Unit

Show the view for the specified duration.

Constants

LENGTH_LONG

Added in API level 1
static val LENGTH_LONG: Int

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

Value: 1

See Also

LENGTH_SHORT

Added in API level 1
static val LENGTH_SHORT: Int

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

Value: 0

See Also

Public constructors

Toast

Added in API level 1
Toast(context: Context!)

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

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

Public methods

addCallback

Added in API level 30
open fun addCallback(callback: Toast.Callback): Unit

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
open fun cancel(): Unit

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
open fun getDuration(): Int

Return the duration.

Return
Int Value is android.widget.Toast#LENGTH_SHORT, or android.widget.Toast#LENGTH_LONG

See Also

getGravity

Added in API level 1
open fun getGravity(): Int

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.

getHorizontalMargin

Added in API level 1
open fun getHorizontalMargin(): Float

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.

getVerticalMargin

Added in API level 1
open fun getVerticalMargin(): Float

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.

getView

Added in API level 1
Deprecated in API level 30
open fun getView(): View?

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 android.os.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).

See Also

getXOffset

Added in API level 1
open fun getXOffset(): Int

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.

getYOffset

Added in API level 1
open fun getYOffset(): Int

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.

makeText

Added in API level 1
open static fun makeText(
    context: Context!,
    text: CharSequence!,
    duration: Int
): Toast!

Make a standard toast that just contains text.

Parameters
context Context!: The context to use. Usually your android.app.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 android.widget.Toast#LENGTH_SHORT, or android.widget.Toast#LENGTH_LONG

makeText

Added in API level 1
open static fun makeText(
    context: Context!,
    resId: Int,
    duration: Int
): Toast!

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

Parameters
context Context!: The context to use. Usually your android.app.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 android.widget.Toast#LENGTH_SHORT, or android.widget.Toast#LENGTH_LONG
Exceptions
android.content.res.Resources.NotFoundException if the resource can't be found.

removeCallback

Added in API level 30
open fun removeCallback(callback: Toast.Callback): Unit

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
open fun setDuration(duration: Int): Unit

Set how long to show the view for.

Parameters
duration Int: Value is android.widget.Toast#LENGTH_SHORT, or android.widget.Toast#LENGTH_LONG

setGravity

Added in API level 1
open fun setGravity(
    gravity: Int,
    xOffset: Int,
    yOffset: Int
): Unit

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.

setMargin

Added in API level 1
open fun setMargin(
    horizontalMargin: Float,
    verticalMargin: Float
): Unit

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
open fun setText(resId: Int): Unit

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
open fun setText(s: CharSequence!): Unit

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
open fun setView(view: View!): Unit

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.

See Also

show

Added in API level 1
open fun show(): Unit

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.