public class Toast extends Object
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.
Modifier and Type | Class and Description |
---|---|
static interface |
Toast.Duration |
Modifier and Type | Field and Description |
---|---|
static int |
LENGTH_LONG
Show the view or text notification for a long period of time.
|
static int |
LENGTH_SHORT
Show the view or text notification for a short period of time.
|
Constructor and Description |
---|
Toast(Context context)
Construct an empty Toast object.
|
Modifier and Type | Method and Description |
---|---|
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.
|
WindowManager.LayoutParams |
getWindowParams()
Gets the LayoutParams for the Toast window.
|
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,
CharSequence text,
int duration)
Make a standard toast that just contains a text view.
|
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.
|
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(CharSequence s)
Update the text in a Toast that was previously created using one of the makeText() methods.
|
void |
setText(int resId)
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.
|
public static final int LENGTH_SHORT
setDuration(int)
,
Constant Field Valuespublic static final int LENGTH_LONG
setDuration(int)
,
Constant Field Valuespublic Toast(Context context)
setView(android.view.View)
before you
can call show()
.context
- The context to use. Usually your Application
or Activity
object.public void show()
public void cancel()
public View getView()
setView(android.view.View)
public void setDuration(int duration)
LENGTH_SHORT
,
LENGTH_LONG
public int getDuration()
setDuration(int)
public void setMargin(float horizontalMargin, float verticalMargin)
horizontalMargin
- The horizontal margin, in percentage of the
container width, between the container's edges and the
notificationverticalMargin
- The vertical margin, in percentage of the
container height, between the container's edges and the
notificationpublic float getHorizontalMargin()
public float getVerticalMargin()
public void setGravity(int gravity, int xOffset, int yOffset)
Gravity
,
getGravity()
public int getGravity()
Gravity
,
getGravity()
public int getXOffset()
public int getYOffset()
public WindowManager.LayoutParams getWindowParams()
public static Toast makeText(Context context, CharSequence text, int duration)
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 static Toast makeText(Context context, @StringRes int resId, int duration) throws Resources.NotFoundException
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
Resources.NotFoundException
- if the resource can't be found.public void setText(@StringRes int resId)
resId
- The new text for the Toast.public void setText(CharSequence s)
s
- The new text for the Toast.