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 | |
---|---|
Constants
LENGTH_LONG
public static final 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
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.
See also:
Constant Value: 0 (0x00000000)
Public constructors
Toast
public 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
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.
getDuration
public int getDuration ()
Return the duration.
Returns | |
---|---|
int |
Value is |
See also:
getGravity
public int getGravity ()
Get the location at which the notification should appear on the screen.
Returns | |
---|---|
int |
See also:
getHorizontalMargin
public float getHorizontalMargin ()
Return the horizontal margin.
Returns | |
---|---|
float |
getVerticalMargin
public float getVerticalMargin ()
Return the vertical margin.
Returns | |
---|---|
float |
getXOffset
public int getXOffset ()
Return the X offset in pixels to apply to the gravity's location.
Returns | |
---|---|
int |
getYOffset
public int getYOffset ()
Return the Y offset in pixels to apply to the gravity's location.
Returns | |
---|---|
int |
makeText
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 |
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 Value is |
Returns | |
---|---|
Toast |
Throws | |
---|---|
|
if the resource can't be found. |
Resources.NotFoundException |
makeText
public static 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
Value is |
Returns | |
---|---|
Toast |
setDuration
public void setDuration (int duration)
Set how long to show the view for.
Parameters | |
---|---|
duration |
int Value is |
See also:
setGravity
public 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
public 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
public 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
public 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
public void setView (View view)
Set the view to show.
Parameters | |
---|---|
view |
View |
See also:
Annotations
Interfaces
- AbsListView.MultiChoiceModeListener
- AbsListView.OnScrollListener
- AbsListView.RecyclerListener
- AbsListView.SelectionBoundsAdjuster
- ActionMenuView.OnMenuItemClickListener
- Adapter
- AdapterView.OnItemClickListener
- AdapterView.OnItemLongClickListener
- AdapterView.OnItemSelectedListener
- Advanceable
- AutoCompleteTextView.OnDismissListener
- AutoCompleteTextView.Validator
- CalendarView.OnDateChangeListener
- Checkable
- Chronometer.OnChronometerTickListener
- CompoundButton.OnCheckedChangeListener
- DatePicker.OnDateChangedListener
- ExpandableListAdapter
- ExpandableListView.OnChildClickListener
- ExpandableListView.OnGroupClickListener
- ExpandableListView.OnGroupCollapseListener
- ExpandableListView.OnGroupExpandListener
- Filter.FilterListener
- Filterable
- FilterQueryProvider
- HeterogeneousExpandableList
- ListAdapter
- MediaController.MediaPlayerControl
- MultiAutoCompleteTextView.Tokenizer
- NumberPicker.Formatter
- NumberPicker.OnScrollListener
- NumberPicker.OnValueChangeListener
- PopupMenu.OnDismissListener
- PopupMenu.OnMenuItemClickListener
- PopupWindow.OnDismissListener
- RadioGroup.OnCheckedChangeListener
- RatingBar.OnRatingBarChangeListener
- RemoteViewsService.RemoteViewsFactory
- SearchView.OnCloseListener
- SearchView.OnQueryTextListener
- SearchView.OnSuggestionListener
- SectionIndexer
- SeekBar.OnSeekBarChangeListener
- ShareActionProvider.OnShareTargetSelectedListener
- SimpleAdapter.ViewBinder
- SimpleCursorAdapter.CursorToStringConverter
- SimpleCursorAdapter.ViewBinder
- SimpleCursorTreeAdapter.ViewBinder
- SlidingDrawer.OnDrawerCloseListener
- SlidingDrawer.OnDrawerOpenListener
- SlidingDrawer.OnDrawerScrollListener
- SpinnerAdapter
- TabHost.OnTabChangeListener
- TabHost.TabContentFactory
- TextView.OnEditorActionListener
- ThemedSpinnerAdapter
- TimePicker.OnTimeChangedListener
- Toolbar.OnMenuItemClickListener
- ViewSwitcher.ViewFactory
- WrapperListAdapter
- ZoomButtonsController.OnZoomListener
Classes
- AbsListView
- AbsListView.LayoutParams
- AbsoluteLayout
- AbsoluteLayout.LayoutParams
- AbsSeekBar
- AbsSpinner
- ActionMenuView
- ActionMenuView.LayoutParams
- AdapterView
- AdapterView.AdapterContextMenuInfo
- AdapterViewAnimator
- AdapterViewFlipper
- AlphabetIndexer
- AnalogClock
- ArrayAdapter
- AutoCompleteTextView
- BaseAdapter
- BaseExpandableListAdapter
- Button
- CalendarView
- CheckBox
- CheckedTextView
- Chronometer
- CompoundButton
- CursorAdapter
- CursorTreeAdapter
- DatePicker
- DialerFilter
- DigitalClock
- EdgeEffect
- EditText
- ExpandableListView
- ExpandableListView.ExpandableListContextMenuInfo
- Filter
- Filter.FilterResults
- FrameLayout
- FrameLayout.LayoutParams
- Gallery
- Gallery.LayoutParams
- GridLayout
- GridLayout.Alignment
- GridLayout.LayoutParams
- GridLayout.Spec
- GridView
- HeaderViewListAdapter
- HorizontalScrollView
- ImageButton
- ImageSwitcher
- ImageView
- LinearLayout
- LinearLayout.LayoutParams
- ListPopupWindow
- ListView
- ListView.FixedViewInfo
- Magnifier
- MediaController
- MultiAutoCompleteTextView
- MultiAutoCompleteTextView.CommaTokenizer
- NumberPicker
- OverScroller
- PopupMenu
- PopupWindow
- ProgressBar
- QuickContactBadge
- RadioButton
- RadioGroup
- RadioGroup.LayoutParams
- RatingBar
- RelativeLayout
- RelativeLayout.LayoutParams
- RemoteViews
- RemoteViewsService
- ResourceCursorAdapter
- ResourceCursorTreeAdapter
- Scroller
- ScrollView
- SearchView
- SeekBar
- ShareActionProvider
- SimpleAdapter
- SimpleCursorAdapter
- SimpleCursorTreeAdapter
- SimpleExpandableListAdapter
- SlidingDrawer
- Space
- Spinner
- StackView
- Switch
- TabHost
- TabHost.TabSpec
- TableLayout
- TableLayout.LayoutParams
- TableRow
- TableRow.LayoutParams
- TabWidget
- TextClock
- TextSwitcher
- TextView
- TextView.SavedState
- TimePicker
- Toast
- ToggleButton
- Toolbar
- Toolbar.LayoutParams
- TwoLineListItem
- VideoView
- ViewAnimator
- ViewFlipper
- ViewSwitcher
- ZoomButton
- ZoomButtonsController
- ZoomControls
Enums
Exceptions