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.
Summary
Constants
|
|
|
Value |
|
int |
LENGTH_LONG |
Show the view or text notification for a long period of time. |
1 |
0x00000001 |
int |
LENGTH_SHORT |
Show the view or text notification for a short period of time. |
0 |
0x00000000 |
Public Constructors
Public Methods
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait
Details
Constants
public
static
final
int
LENGTH_LONG
Show the view or text notification for a long period of time. This time
could be user-definable.
Constant Value:
1
(0x00000001)
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.
Constant Value:
0
(0x00000000)
Public Constructors
Public Methods
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.
public
int
getGravity()
Get the location at which the notification should appear on the screen.
public
float
getHorizontalMargin()
Return the horizontal margin.
public
float
getVerticalMargin()
Return the vertical margin.
public
int
getXOffset()
Return the X offset in pixels to apply to the gravity's location.
public
int
getYOffset()
Return the Y offset in pixels to apply to the gravity's location.
public
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.
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 |
public
static
Toast
makeText(Context context, CharSequence text, int duration)
Make a standard toast that just contains a text view.
public
void
setDuration(int duration)
Set how long to show the view for.
public
void
setGravity(int gravity, int xOffset, int yOffset)
Set the location at which the notification should appear on the screen.
public
void
setMargin(float horizontalMargin, float verticalMargin)
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)
Update the text in a Toast that was previously created using one of the makeText() methods.
Parameters
resId
| The new text for the Toast.
|
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)
public
void
show()
Show the view for the specified duration.