![]() |
VOOZH | about |
Pre-requisites:
A Toast is a feedback message. It takes a very little space for displaying while the overall activity is interactive and visible to the user. It disappears after a few seconds. It disappears automatically. If the user wants a permanently visible message, a Notification can be used. Another type of Toast is custom Toast, in which images can be used instead of a simple message.
Example: Toast class provides a simple popup message that is displayed on the current activity UI screen (e.g. Main Activity).
| Constants | Description |
|---|---|
| public static final int LENGTH_LONG | displays for a long time |
| public static final int LENGTH_SHORT | displays for a short time |
| Methods | Description |
|---|---|
| public static Toast makeText(Context context, CharSequence text, int duration) | makes the toast message consist of text and time duration |
| public void show() | displays a toast message |
| public void setMargin (float horizontalMargin, float verticalMargin) | changes the horizontal and vertical differences |
In this example "This a simple toast message" is a Toast message which is displayed by clicking on the 'CLICK' button. Every time when you click your toast message appears.
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.
Open the “activity_main.xml” file and add a Button to show the Toast message in a Constraint Layout. Also, Assign an ID to the button component as shown in the image and the code below. The assigned ID to the button helps to identify and to use files.
android:id="@+id/id_name"
Now, after the UI, this step will create the Backend of the App. For this, Open the “MainActivity” file and instantiate the component (Button) created in the XML file using the findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID.
ComponentType object = (ComponentType)findViewById(R.id.IdOfTheComponent);
Button btn = (Button)findViewById(R.id.Button01);
Add the listener on the Button and this Button will show a toast message.
btn.setOnClickListener(new View.OnClickListener() {});
Now, Create a toast message. The Toast.makeText() method is a pre-defined method that creates a Toast object. Syntax:
public static Toast makeText (Context context, CharSequence text, int duration)
Parameters: This method accepts three parameters:
A. context: A first parameter is a Context object which is obtained by calling getApplicationContext().
Context context = getApplicationContext();
B. text: The second parameter is your text message to be displayed.
CharSequence text=”Your text message here”
C. duration: The last parameter is the time duration for the message.
int duration=Toast.LENGTH_LONG;
Display the created Toast Message using the show() method of the Toast class.
Syntax:
public void show ()
Toast.makeText(getApplicationContext(), "This a toast message", Toast.LENGTH_LONG).show();
Operate it as follows:
If there is a need to set the position of a Toast message, then setGravity() method can be used.
public void setGravity (int gravity, int xOffset, int yOffset)
Parameters: This method accepts three parameters:
gravity: This sets the position of the Toast message. The following constants can be used to specify the position of a Toast:
1.TOP 2.BOTTOM 3.LEFT 4.RIGHT 5.CENTER 6.CENTER_HORIZONTAL 7.CENTER_VERTICAL
- To display the Toast at the center: toast.setGravity(Gravity.CENTER, 0, 0);
- To display the Toast at the top, centered horizontally: toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTALLY, 0, 0);
- To display the Toast at the top, centered horizontally, but 30 pixels down from the top: toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTALLY, 0, 30);
- To display the Toast at the bottom, rightmost horizontally: toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 0, 0);
Example: Here, in the below example, the Toast is displayed at the Bottom-Right position.
Toast t = Toast.makeText(getApplicationContext(), "This a positioned toast message", Toast.LENGTH_LONG); t.setGravity(Gravity.BOTTOM | Gravity.RIGHT, 0, 0); t.show();