Toast Notifications in Android Application
Submitted by pavel7_7_7 on Saturday, September 13, 2014 - 22:34.
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:
Where
And after this you need to call method show() of the Toast class:
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:
For example, you can do it in the following way:
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:
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:
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.
- context - context of the application
- text - a string with the message.
- duration - time to display message
- Toast toast = Toast.makeText(getApplicationContext(),
- "Welcome to sourcecodester.com", Toast.LENGTH_SHORT);
- toast.show();
- setGravity(int gravity, int xOffset, int yOffset)
- toast.setGravity(Gravity.TOP, 0, 0);
- Toast.makeText(context, msg, duration).show();
- }
- Toast.makeText(context, stringID, duration).show();
- }
- Toast toast = Toast.makeText(context, msg, duration);
- toast.setGravity(gravity, 0, 0);
- toast.show();
- }
- Toast toast = Toast.makeText(context, text, duration);
- toast.setGravity(gravity, offset[0], offset[1]);
- toast.show();
- }
- Toast toastWithImage = Toast.makeText(getApplicationContext(),R.string.Hello, Toast.LENGTH_LONG);
- toastWithImage.setGravity(Gravity.CENTER, 0, 0);
- LinearLayout toastlayout = (LinearLayout) toastWithImage.getView();
- ImageView toastImage = new ImageView(getApplicationContext());
- toastImage.setImageResource(R.drawable.toastImage);
- toastlayout.addView(toastImage, 0);
- toastWithImage.show();
Add new comment
- 31 views