Toast
Have you ever seen the following format of message as it shown in the image below in any app you use?
It is called a Toast in Android app development. It is used to display short and temporary messages in android apps and let’s see what its features are and then we will get our hands dirty and learn how to make such toasts.
Features of Toast
- It is an Android widget that can used to show a message for a short duration of time.
- It disappears after some time.
- It doesn’t block the Activity or Fragment when it shows.
- It can be used to give feedback to the user regarding any action, like form submission etc.
How to Create a Toast?
The Toast can be created using android.widget.Toast class, which extends the java.lang.Object class.as you might know this java.lang.Object is super class in java all the classes by default extends this class .
Constants of Toast class
Constant | Description |
public static final int LENGTH_LONG | This is used to display the Toast for a longer duration of time. |
public static final int LENGTH_SHORT | This is used to display the Toast for a longer duration of time. |
The constant LENGTH_LONG sets display duration of 3.5 sec in the constant LENGTH_SHORT sets display duration of 2 sec for the Toast.
Methods of Toast class
Following are the methods available in the Toast class, which can be used to create a Toast.
Method | Description |
public static Toast makeText(Context context, CharSequence text, int duration) | This particular method is used to make the Toast widget with the specified text and for the specified duration. |
public void show() | This method runs the Toast. |
public void setMargin(float horizontal, float vertical) | This particular method is used to set horizontal and vertical margin |
Now let us see how to create a Toast:
Make an object of the Toast class.
Toast t = new Toast(this);
Call makeText(Context c, CharSequence text, int duration) this method which required three parameters.
Context c: : Context is nothing but just an interface for all global information about an app environment and this can be an abstract class whose implementation got provided by an Android system also It allows access to application-specific classes and resources, along with up-calls for application-level operations such as broadcasting and receiving intents and launching activities, etc and we can get this Context object by using the method getApplicationContext()
CharSequence text: This is the message which is displayed in the toast and It can be any text.
int duration:This is the time duration for which you want your message to appear on the screen and There are two values: Toast.LENGTH_SHORT and Toast.LENGTH_LONG
Using our Toast instance/object and we need to call maketext() method in the following way:
t.makeText(getApplicationContext(),"Hello Toast",Toast.LENGTH_SHORT);
Then call show() this method to sjow the toast on the screen.
t.show();
The complete code can be:
Toast t = new Toast(this); t.makeText(getApplicationContext(),"Hello Toast",Toast.LENGTH_SHORT); t.show();
Positioning Toast on the Screen
By default, a Toast message can appear at the center on the bottom of the screen and If you want to display it at other positions then you can use the method setGravity(int gravity, int x, int y) which has the given below parameters:
- int gravity: You can also use the Gravity class for using the predefined values like Gravity.RIGHT, Gravity.TOP or you can use more than one values by using pipe( | ) symbol. For example, Gravity.LEFT|Gravity.BOTTOM
- int x: You can also use this to set the horizontal distance and from where this distance will be measured depends upon the int gravity parameter you have set based on the requirement.
- int y: You can use this to set the vertical distance and from where this distance will be measured depends upon the int gravity parameter you have set.
For example, if we have chosen Gravity.CENTER, and your x=100 and y=200 and then it will place the toast in the following position:
Complete Code for Activity Class displaying Toast
package com.studytonight.toast; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Displaying Toast with 'Studytonight is Best' message Toast.makeText(getApplicationContext(),"Hello Toast",Toast.LENGTH_LONG).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
Output: