Android APIs
public class

Toast

extends Object
java.lang.Object
   ↳ android.widget.Toast

Class Overview

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.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int LENGTH_LONG

Added in API level 1

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

See Also
Constant Value: 1 (0x00000001)

public static final int LENGTH_SHORT

Added in API level 1

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

public Toast (Context context)

Added in API level 1

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

Parameters
context The context to use. Usually your Application or Activity object.

Public Methods

public void cancel ()

Added in API level 1

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.

public int getDuration ()

Added in API level 1

Return the duration.

See Also

public int getGravity ()

Added in API level 1

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

public float getHorizontalMargin ()

Added in API level 1

Return the horizontal margin.

public float getVerticalMargin ()

Added in API level 1

Return the vertical margin.

public View getView ()

Added in API level 1

Return the view.

See Also

public int getXOffset ()

Added in API level 1

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

public int getYOffset ()

Added in API level 1

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

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

Added in API level 1

Make a standard toast that just contains a text view with the text from a resource.

Parameters
context The context to use. Usually your Application or Activity object.
resId The resource id of the string resource to use. Can be formatted text.
duration How long to display the message. Either LENGTH_SHORT or LENGTH_LONG
Throws
if the resource can't be found.
Resources.NotFoundException

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

Added in API level 1

Make a standard toast that just contains a text view.

Parameters
context The context to use. Usually your Application or Activity object.
text The text to show. Can be formatted text.
duration How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

public void setDuration (int duration)

Added in API level 1

Set how long to show the view for.

public void setGravity (int gravity, int xOffset, int yOffset)

Added in API level 1

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

public void setMargin (float horizontalMargin, float verticalMargin)

Added in API level 1

Set the margins of the view.

Parameters
horizontalMargin The horizontal margin, in percentage of the container width, between the container's edges and the notification
verticalMargin The vertical margin, in percentage of the container height, between the container's edges and the notification

public void setText (int resId)

Added in API level 1

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

Parameters
resId The new text for the Toast.

public void setText (CharSequence s)

Added in API level 1

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

Parameters
s The new text for the Toast.

public void setView (View view)

Added in API level 1

Set the view to show.

See Also

public void show ()

Added in API level 1

Show the view for the specified duration.