Toast Notifications in Android Application

In this tutorial you will find the information about how to use toast notifications in your Android Project. Toast notification is a message that appears in your application window. The size of this notification is exact as needed to show all the information of the message. After it is show, Toast Notification disappears. In the most cases is used to show a short text message. To show the Toast Notification you need to initialize the object of the Toast. It's done with method:
  1. Toast.makeText(Context context, CharSequence text, int duration)
Where
  • context - context of the application
  • text - a string with the message.
  • duration - time to display message
You can use instead of CharSequence text the id of the string resource. The initialization can be done in the following way:
  1. Toast toast = Toast.makeText(getApplicationContext(),
  2. "Welcome to sourcecodester.com", Toast.LENGTH_SHORT);
And after this you need to call method show() of the Toast class:
  1. toast.show();
The duration of the display time is defined in constants. There are 2 constants: LENGTH_SHORT — Default value. This shows message for short time. LENGTH_LONG — Shows message for a longer period of the time. I found the information that the duration of the short message is 2 seconds and for a long constant the value is 3.5 seconds. By default, the message appears in the bottom part of the screen. But you can change the location by using:
  1. setGravity(int gravity, int xOffset, int yOffset)
For example, you can do it in the following way:
  1. toast.setGravity(Gravity.TOP, 0, 0);
For the gravity you always have to use constants defined in the Gravity class. As you can see you can customize the position of the toast notification and you can use directly a string with the message or a string resource id. It's useful to have a list of methods in your program, if you are using toast notification:
  1. public static void showMsg(Context context, String msg, int duration) {
  2. Toast.makeText(context, msg, duration).show();
  3. }
  4.  
  5. public static void showMessage(Context context, int stringID, int duration) {
  6. Toast.makeText(context, stringID, duration).show();
  7. }
  8.  
  9. public static void showMessage(Context context, String msg, int duration, int gravity) {
  10. Toast toast = Toast.makeText(context, msg, duration);
  11. toast.setGravity(gravity, 0, 0);
  12. toast.show();
  13. }
  14.  
  15. public static void showMessage(Context context, String text, int duration, int gravity, int[] offset) {
  16. Toast toast = Toast.makeText(context, text, duration);
  17. toast.setGravity(gravity, offset[0], offset[1]);
  18. toast.show();
  19. }
You can call these methods with different parameters to set the gravity and the position of the toast notification. There is a way to add an image to the toast notification. It makes the notification more attractive. It can be done in the following way:
  1. Toast toastWithImage = Toast.makeText(getApplicationContext(),R.string.Hello, Toast.LENGTH_LONG);
  2. toastWithImage.setGravity(Gravity.CENTER, 0, 0);
  3. LinearLayout toastlayout = (LinearLayout) toastWithImage.getView();
  4. ImageView toastImage = new ImageView(getApplicationContext());
  5. toastImage.setImageResource(R.drawable.toastImage);
  6. toastlayout.addView(toastImage, 0);
  7. toastWithImage.show();
Now you can work with the toast images without any problems because you know all the information you need. Do not forget the fact that toast notifications are displayed for a short time. So,you message should be really to short.

Add new comment