Android : Creating notification using Toast

What is toast notification ?

Toast notification is a message popup in android application which popup on application window and automatically fades in. It fills only required area on screen and user’s activity and interaction are remains enable, it mean during popup of toast message user can do interaction’s like entering text, selection of controls ect.

For this android lib has one class android.widget.Toast, this class has methods to create toast, set their location , duration and message.

For example, simple text message base toast.

package bimbim.in;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Toast;
 
public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //call to show toast
        ShowToast("Test Toast");
    }
    
    private void ShowToast(String msg )
    {
        Toast toast = Toast.makeText(Main.this, msg,
                 Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 0, 0);
        toast.show();
    }
}

Here is out putandroid-toast-example

In above example we have created a toast object using makeText static method of class which returns toast object.

makeText has two overloads as given below

//Make a standard toast that just contains a text 
view with the text from a resource.
static Toast makeText(Context context, 
int resId, int duration)
 
//Make a standard toast that just contains a text from string.
static Toast makeText(Context context, 
CharSequence text, int duration)
 

For setting location of toast notification there is method setGravity. it Set the location at which the notification should appear on the screen.

public void setGravity (int gravity, int xOffset, int yOffset) 

First parameter is gravity and different gravity constants are define in class Gravity. You can see different  Gravity constants and their description using Eclipse IDE.

Other useful methods are

setDuration(int duration) and setMargin(float horizontalMargin, float verticalMargin)

setDuration is used to set duration of toast display and it can be short or long while setMargin is used to specify vertical and horizontal margin.

show() method is used to show the toast.

Custom View Toast:

There is an important method setView(View view) and this is used to show custom view as toast.

This is used when you want to show custom view as toast notification like some colorful text and image and other.

Here is one example

package bimbim.in;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //call to show toast
        //ShowToast("Test Toast");
        ShowCustomeViewToast();
    }
    
    private void ShowToast(String msg )
    {
        Toast toast = Toast.makeText(Main.this, msg,
                 Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 0, 0);
        toast.show();
    }
    
    private void ShowCustomeViewToast()
    {
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.customeview,
            (ViewGroup) findViewById(R.id.root_layout));
 
        ImageView image = (ImageView) layout.findViewById(R.id.image1);
        image.setImageResource(R.drawable.icon);
        TextView text = (TextView) layout.findViewById(R.id.text1);
        text.setText("Image and Text toast");
 
        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.CENTER_VERTICAL|
                Gravity.CENTER_HORIZONTAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
    }
}
 

custom-android-toast

You can download sample from here Android.Toast.Test.zip (43.25 kb)

Leave a Reply

Your email address will not be published. Required fields are marked *