Most visited

Recently visited

Added in API level 1

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.

Developer Guides

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

Summary

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 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()

Return the view.

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 a text view with the text from a resource.

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

Make a standard toast that just contains a text view.

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)

Set the view to show.

void show()

Show the view for the specified duration.

Inherited methods

From class java.lang.Object

Constants

LENGTH_LONG

Added in API level 1
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
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
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

cancel

Added in API level 1
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
int getDuration ()

Return the duration.

Returns
int

See also:

getGravity

Added in API level 1
int getGravity ()

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

Returns
int

See also:

getHorizontalMargin

Added in API level 1
float getHorizontalMargin ()

Return the horizontal margin.

Returns
float

getVerticalMargin

Added in API level 1
float getVerticalMargin ()

Return the vertical margin.

Returns
float

getView

Added in API level 1
View getView ()

Return the view.

Returns
View

See also:

getXOffset

Added in API level 1
int getXOffset ()

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

Returns
int

getYOffset

Added in API level 1
int getYOffset ()

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

Returns
int

makeText

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

Make a standard toast that just contains a text view with the 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
Returns
Toast
Throws
if the resource can't be found.
Resources.NotFoundException

makeText

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

Make a standard toast that just contains a text view.

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
Returns
Toast

setDuration

Added in API level 1
void setDuration (int duration)

Set how long to show the view for.

Parameters
duration int

See also:

setGravity

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

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

Parameters
gravity int
xOffset int
yOffset int

See also:

setMargin

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

Set the margins of the view.

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
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
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
void setView (View view)

Set the view to show.

Parameters
view View

See also:

show

Added in API level 1
void show ()

Show the view for the specified duration.

Hooray!