Toasts in Android are used to show Notification within an Activity. You may be knowing what alert messages are in HTML. Using JavaScript alert() function we can Alert the user about something using a popup message, the user sees the message and clicks the OK button to dismiss the dialog.
Toast messages in Android Programming are similar but they are terminated/dismissed by itself (we do not have any buttons). We need to set a time period for which the message has to be displayed when the time is reached the message fades away, it is usually shown at the bottom of the Activity page.

Some examples where you can an Android Toast Message be helpful:
Example 1: We have an Email application and the user deletes an email, then when that email is been deleted we can display a Toast message saying "Email has been deleted"
Example 2: At login, if the email id or password is incorrect we can display a toast message saying "Invalid id or password"
Example 3: When a message has been sent, we can notify the user "Message sent" using toast messages.
Let's see how Toasts works:
The above code snippet is the structure of creating a Toast object and setting a "Text" message and its "Duration"
makeText() is the method we have to use which takes in three parameters, Let's see each of these parameters one-by-one,
- Context:
It is the Application context.
You can get the Application context using, Context context = getApplicationContext();
Another way of getting Context is referring to the Activity class that you are displaying the Toast message. I suppose the Activity class is MainActivity where we want to display the message,
Toast toast = Toast.makeText(MainActivity.this, text , Toast.LENGTH_SHORT); toast.show();
You can also refer to the class by simply referring "this"
Toast toast = Toast.makeText(this, text , duration); toast.show();
Text :
It is the string message that you want the toast to display on the Android Activity screen.
String toastTextMsg = "Hello, welcome to Code2care!"; Toast toast = Toast.makeText(MainActivity.this, toastTextMsg , Toast.LENGTH_SHORT); toast.show();
Note: If any of the resources is not found then you will get a Resources.NotFoundException exception.
Duration :
It is the time period in milliseconds for which the toast message will be displayed on the screen. The toast message be visible as a fade-in effect and will be dismissed automatically when this time duration has elapsed with a fade-out effect.
There are two constants for duration time you can use from the Toast class
- Toast.LENGTH_SHORT : This will display the toast for a short period of time.
- Toast.LENGTH_LONG : This will display the toast for a long period of time.
Note that you can only have these two values for the duration of toast message, If you define a custom duration as Integer value you will get a warning message in the gutter area saying,
Toast.makeText(this, "Hello", 5000); toast.show();
⛏️ "Expected duration Toast.LENGTH_SHORT or Toast.LENGTH_LONG, a custom duration value is not supported"
Actual duration of these constants are 3.5 seconds for LONG_DELAY and 2 seconds for SHORT_DELAY,
private static final int LONG_DELAY = 3500; // 3.5 seconds private static final int SHORT_DELAY = 2000; // 2 seconds
You can display toast as one statement too,
Toast.makeText(this, "Hello, this is a android toast message!", Toast.LENGTH_LONG).show();
Changing the Position of Toast message :
By default the toast message is displayed at the bottom of an Activity screen aligned vertically.
If you want to display the toast message at the different position then you can use setGravity() method.
setGravity(int, int, int) :
To get a location at which the notification should appear on the screen it has 3 parameters,
-
Gravity constants :
You can set the position of toast message using gravity constants,
Gravity.TOP Gravity.BOTTOM Gravity.LEFT Gravity.RIGHT
x-position offset
If you want to move the toast message towards the right side, increase this value.
y-position offset
If you want to move the toast message towards the left side, increase this value.
Example : toast.setGravity(Gravity.TOP, 0,0);
Toast Example : Java Code
package com.example.toastexample;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Toast Example
Toast toast = Toast.makeText(this,
"Hello, this is a android toast message!", Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 4, 5);
toast.show();
}
}

- Android : Execute some code after back button is pressed
- Android is starting optimizing... app 1 of 1
- How to change Android Button Color using xml attribute and programatically using java
- SQLite with Android Easy to Understand Tutorial that covers Select, Insert, Update and Delete
- How to make Android EditText not editable
- Make Android TextView Clickable like Buttons
- Unable to establish connection to adb : Android Studio Error
- Android Constant and Resource Type Mismatches Lint
- Android Shared Preferences API tutorial
- Fixing Android unknown error 961 while downloading app
- DDMS files not found hprof-conv.exe
- How to reset eclipse layout
- Android Developers Bluetooth Tutorial
- Android Studio Native typeface cannot be made error
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Multiline EditText in Android Example
- How to screenshot on Android?
- JavaScript : redirect page to other url
- Unable to load VM from snapshot. The snapshot has been saved for a different hardware configuration
- How To Disable Landscape Mode in Android Application
- Change Title text for Android Activity using java code
- Android : Class file collision: A resource exists with a different case
- Android Emulator Soft Back button action using Computer keyboard
- Device not compatible error Android Google Play Store
- ERROR x86 emulation currently requires hardware acceleration. Intel HAXM is not installed on this machine
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager
- Disable Fading Edges Scroll Effect Android Views
- Android : Unable to load VM from snapshot : Mac OS X Error
- Change Android EditText Cursor Height
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- How to add border to Android TextView
- Remove ActionBar from Activity that extends appcompat-v7
- How to send SMS on Android Emulator
- Android RatingBar Example
- This Toast was not created with Toast.makeText() : Android RuntimeException
- Android Studio Ctrl Shift o auto import not working
- Android : Duplicate registration for activity com.example.abc
- ADT Installation Error: requires plug-in org.eclipse.wst.sse.ui
- Running Android Lint has encountered a problem NullPointerException Error
- Android Emulator] ##KBD: Full queue, lose event Error Logs
- Export aborted because fatal lint error were found
- Android Parsing Data for android-L failed Unsupported major.minor version 51.0 Error
- Android : Accidental Octal Lint Warning
- Android ListView turns Black or Flickers while Scrolling
- How to make a dummy phone call from Android Emulator device
- How to know if someone has read your WhatsApp message - WhatsApp
- Android Studio NoClassDefFoundError: java.awt.Toolkit - Android-Studio
- WhatsApp launches WhatsApp Web to Access Messages over web browser - WhatsApp
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations - Android
- Check Wifi Connection static Android Programming - Android
- Android Constant and Resource Type Mismatches Lint - Android
- How to Word wrap eclipse console logs width - Eclipse
- How to hide Navigation bar from Android Screen Activity - Android
- Share image and text Twitter using your Android Application Programatically - Twitter
- WhatsApp Web escanner - WhatsApp
- Programmatically check if Facebook is installed on Android device - Android
- Install Apache Tomcat ver 8 on Mac OS X Yosemite 10.10 - Mac-OS-X
- Disable Control Scroll Zoom-in and Zoom-out in Notepad++ - NotepadPlusPlus
- Can we move apps like WhatsApp, Facebook to external MicroSD card - WhatsApp
- Java: Check Internet connection on Android Device (Wifi or Mobile) - Android