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.
For information about creating Toast notifications, read the Toast Notifications developer guide.
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
|
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)
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)
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.
|
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.
int getGravity ()
Get the location at which the notification should appear on the screen.
Returns | |
---|---|
int |
See also:
float getHorizontalMargin ()
Return the horizontal margin.
Returns | |
---|---|
float |
float getVerticalMargin ()
Return the vertical margin.
Returns | |
---|---|
float |
int getXOffset ()
Return the X offset in pixels to apply to the gravity's location.
Returns | |
---|---|
int |
int getYOffset ()
Return the Y offset in pixels to apply to the gravity's location.
Returns | |
---|---|
int |
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 |
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 |
void setDuration (int duration)
Set how long to show the view for.
Parameters | |
---|---|
duration |
int
|
See also:
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:
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
|
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.
|
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.
|
void setView (View view)
Set the view to show.
Parameters | |
---|---|
view |
View
|
See also: