A Toast provides simple feedback about an operation in a small popup and automatically disappears after a timeout. It only fills the amount of space required for the message and the current activity remains visible and interactive.
Toast makeText (Context context,
Toast makeText (Context context,
void setGravity(int gravity, int xOffset, int yOffset)
The context to display your Toast in. this is commonly used in an Activity and getActivity() is commonly used in a Fragment
A CharSequence that specifies what text will be shown in the Toast. Any object that implements CharSequence can be used, including a String
A resource ID that can be used to provide a resource String to display in the Toast
Integer flag representing how long the Toast will show. Options are Toast.LENGTH_SHORT and Toast.LENGTH_LONG
Integer specifying the position, or "gravity" of the Toast. See options here
Specifies the horizontal offset for the Toast position
Specifies the vertical offset for the Toast position
A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive.
A more recent alternative to Toast is SnackBar. SnackBar offers an updated visual style and allows the user to dismiss the message or take further action. See the SnackBar documentation for details.
If you don't want to use the default Toast view, you can provide your own using the setView(View) method on a Toast object.
First, create the XML layout you would like to use in your Toast.
Then, when creating your Toast, inflate your custom View from XML, and call setView
Set position of a Toast
A standard toast notification appears at the bottom of the screen aligned in horizontal centre. You can change this position with the setGravity(int, int, int). This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.
For example, if you decide that the toast should appear in the top-left corner, you can set the gravity like this:
Show Toast Message Above Soft Keyboard
By default, Android will display Toast messages at the bottom of the screen even if the keyboard is showing. This will show a Toast message just above the keyboard.
Showing a Toast Message
In Android, a Toast is a simple UI element that can be used to give contextual feedback to a user.
To display a simple Toast message, we can do the following.
Or, to show a Toast inline, without holding on to the Toast object you can:
IMPORTANT: Make sure that the show() method is called from the UI thread. If you're trying to show a Toast from a different thread you can e.g. use runOnUiThread method of an Activity.
Failing to do so, meaning trying to modify the UI by creating a Toast, will throw a RuntimeException which will look like this:
The simplest way of handling this exception is just by using runOnUiThread: syntax is shown below.
Thread safe way of displaying a Toast Message (For AsyncTask)
If you don't want to extend Application and keep your toast messages thread safe, make sure you show them in the post execute section of your AsyncTasks.
Thread safe way of displaying Toast (Application Wide)
Remember to add MainApplication in manifest.
Now call it from any thread to display a toast message.
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0