Toast in Android
Have you ever encountered the following format of message as shown in the image below in any app you use?
This is called a Toast in Android. It is used to display short and temporary messages in android apps. So let's first see what are its features and then we will get our hands dirty and learn how to make such toasts.
Features of Toast
- It is an Android widget that is used to show a message for a short duration of time.
- It disappears after a short time.
- It doesn't block the Activity or Fragment when it runs.
- It can be used to give feedback to the user regarding any operations, like form submission etc.'
To learn android visit:android training
How to Create a Toast?
A Toast can be created using the
android.widget.Toast
class, which extends the java.lang.Object
class.
Before, we proceed with learning how to create a Toast, let's spend some time in exploring the
android.widget.Toast
class.
Contants of Toast class
public static final int LENGTH_LONG
This can be used to display the Toast for a longer duration
.public static final int LENGTH_SHORT
This can be used to display the Toast for a longer duration. The constant
LENGTH_LONG
sets a display duration of 3.5 sec while the constant LENGTH_SHORT
sets a display duration of 2 sec for the Toast.Methods of Toast class
Following are the methods available in the
Toast
class, which are used to create a Toast.
public static Toast makeText(Context context, CharSequence text, int duration)
This method makes the Toast widget with the specified text and for the specified duration.
public void show()
This method shows the Toast.
public void setMargin(float horizontal, float vertical)
This method can be used to set horizontal and vertical margin.
Now let's see how to we create a Toast:
- Make an object of the
Toast
class.
Toast t = new Toast(this);
- Call
makeText(Context c, CharSequence text, int duration)
method which needs three parameters. - Context c: Context is an interface for global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc. We can get this Context object by using the method
getApplicationContext()
- CharSequence text: This is the message which is shown in the toast. It can be any text.
- int duration: This is the time duration for which you want your message to appear on the screen. There are two values:
Toast.LENGTH_SHORT
andToast.LENGTH_LONG
- Using our Toast instance/object, we need to call
maketext()
method in the following way:
t.makeText(getApplicationContext(),"StudyTonight Toast",Toast.LENGTH_SHORT);
- Then call
show()
method to display the toast on the screen
t.show();
The complete code will be:
Toast t = new Toast(this); t.makeText(getApplicationContext(),"StudyTonight Toast",Toast.LENGTH_SHORT); t.show();
Positioning Toast on the Screen
By default, Toast message appears at the center on the bottom of the screen. If you want to display it at other positions, you can use the method
setGravity(int gravity, int x, int y)
which has the following parameters:- int gravity: You can use the Gravity class to use the predefined values like
Gravity.RIGHT
,Gravity.TOP
or you can also use more than one values by using pipe( | ) symbol. For example,Gravity.LEFT|Gravity.BOTTOM
- int x: You can use this to set the horizontal distance. From where this distance will be measured depends upon the
int gravity
parameter you have set. - int y: You can use this to set the vertical distance. Again, from where this distance will be measured depends upon the
int gravity
parameter you have set.
For example, if you have chosen
Gravity.CENTER
, and your x=100
and y=200
, 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(),"Studytonight is Best",Toast.LENGTH_LONG).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
Output Screen
To more information visit:android app development course
Comments
Post a Comment