
Android toast is used for displaying message or information for short time, Toast has a message in form of string to display quickly. You can display the information of message to user for short time using any other method than the question is arise that why we use Toast? So the answer is simple Toast less space of diaplay and less memory to show the information faster and the remaining activity will remain interactive means we can use the app while toast is displaying.
Toast Object:
To make a toast it will require 3 parameters such as:
- Application Context
- String or Message
- Time Period
Application Context you can initialize like this way:
Context context = getApplicationContext();
And String you can Defined as :
private String message = "This is my message";
And for Toast Time Period you can use predefined time Constants which is:
LENGTH_LONG LENGTH_SHORT
So using all these parameters you can show toast like this way:
Toast.makeText(context, text, duration).show(); The final Toast Code will be:
Toast.makeText(context, message,Toast.LENGTH_SHORT).show();

If you want to show multiple toast then you can create a custom Toast Method to display the message like this way:
Create a method:
public void showToast(String info){ }
Now costomize toast :
public void showToast(String info){ Toast.makeText(context, info,Toast.LENGTH_SHORT).show(); }
Now when you want to show toast just call this method like this way:
showToast("This is my message");
Toast Background Color:
To change the color of Android Toast you have to set background color to toast view programmatically like the below code:
Toast toast = Toast.makeText(context, info,Toast.LENGTH_SHORT); View tv = toast.getView(); // get the toast view to set the color on view tv.setBackgroundColor(Color.GREEN); // set color here toast.show(); // now show toast with green background

You can also set padding to toast you noticed that it doesn’t has space from its start point and end point.
Toast with Gradient Background:
Toast toast = Toast.makeText(this, "This is my message", Toast.LENGTH_LONG); View tv = toast.getView(); // get toast view to set properties to toast tv.setPadding(20,15,20,15); // padding top, left, right, bottom if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { tv.setBackground(getDrawable(R.drawable.gradeint)); // gradient background using gradient drawable xml file } toast.show(); // show gradient toast


Toast with Gradient Background and Icon:
if you want o set icon to tast then you can do this using the below method of code:
First of all create a view for toast than call textview to set text and icon together after that set the drawable icon and text to it. Finally set gradient xml file to it and call show method to diaplay the customized toast. Its Done…
Toast toast = Toast.makeText(this, "This is my message", Toast.LENGTH_LONG); View tv = toast.getView(); // get toast view to set properties to toast tv.setPadding(20,15,20,15); // padding top, left, right, bottom TextView toastMessage = (TextView) tv.findViewById(android.R.id.message); // call textview to set icon and text together toastMessage.setTextSize(25); // text size toastMessage.setTextColor(Color.WHITE); // text color toastMessage.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_play, 0, 0, 0); // icon ic_play toastMessage.setGravity(Gravity.CENTER); // set the toast in center toastMessage.setCompoundDrawablePadding(16); //padding between text and icon if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { tv.setBackground(getDrawable(R.drawable.gradeint)); // toast gradient background using drawable xml file } toast.show(); // show toast

If you like this tutorial please share it with your friends and if you have any doubts comment below.
Leave a Comment
You must be logged in to post a comment.