Thursday 18 July 2013

Custom Toast in Android

In android there are several way to information user by using pre-define classes/API. As for example Toast, Dialog, Notification these three are the main classes for user notification. Dialog has different types like Alert, Popup,Custom Dialog and more. Same way notification have type like local notification(based on services and AlertManager) and  server side notification like Urban Air Push and other third party notification.

  But Today I am going to deal Toast API in android and it uses in showing user information. By default We generally use simple Toast to show user information. But We can customize Toast message. So In this Post I am going to show all aspect of Toast message in Android.
At first Android official definition of Toast:- "A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive.Toasts automatically disappear after a timeout. "

The Basics
First, instantiate a Toast object with one of the makeText() methods. This method takes three parameters: the application Context, the text message, and the duration for the toast.You can display the toast notification with show(), as shown in the following example:

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
You can also call Toast message like this. 
Toast.makeText(context, text, duration).show();
Positioning  Toast on the Screen
A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.
For example, if you decide that the toast should appear in the top-left  corner or Center-horizontal, you can set the gravity like this:

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);//Top-left corner or Put Gravity.Center_horizental for center position 
toast.show();


Adding an Image to the Basic Toast
In order to add an image to the standard Toast, you first need to create an ImageView object, and set its image from the resources using the setImageResource method. You then need to get the standard view (which is actually a LinearLayout) and add the newly created ImageView object to it. The position, into which the image needs to be inserted, also should be set (in our case 0 is used, so that the image is displayed over the text). Code for this is shown below:
Toast toast = Toast.makeText(getApplicationContext(),"'saurabhsharma123k.blogspot.in'",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView image = new ImageView(getApplicationContext());
image.setImageResource(R.drawable.saurabh);
toastView.addView(image, 0);
toast.show();

Creating a Toast with Custom Layout
In order to create a Toast with custom layout, we first need to create that layout itself. Its code is shown below:
  LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast_activity,(ViewGroup) findViewById(R.id.llToast));
ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.saurabh);
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("'saurabhsharma123k.blogspot.in'");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

Calling a Toast from Another Thread
To call a Toast from another Thread, you need to define a property of Handler type in your class:
Handler handler = new Handler();
Now, let’s assume that we have a Thread, in which we need to call a method that displays a Toast:

new Thread(new Runnable() {
public void run() {
showToast();
}
}).start();

In this case, the method will be executed in a separate Thread. But because a Toast can only be displayed in the main Thread, So we will get an error when we try to run this.
If we need to execute in the main Thread, when calling from a separate Thread, you need a Handler, that was defined earlier. You can display a Toast from newly created Thread in this way:
public void showToast() {
handler.post(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),
"'saurabhsharma123k.blogspot.in'", Toast.LENGTH_SHORT).show();
}
});
}
                Happy Coding !!!

3 comments:

  1. Can a Toast be more than one line long? When you add a picture, as in your example above, the Toast is very large. I would think a Toast could be more than one line.

    ReplyDelete
  2. What to if i want to use more than one customized toast in android....should i create as many xml files????? pls reply

    ReplyDelete
  3. Congratulations guys, quality information you have given!!!..Its really useful blog. Thanks for sharing this useful information
    Android training in chennai with placement | Android Training in chennai |Android Training in Velachery

    ReplyDelete

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...