FileDocCategorySizeDatePackage
Toast.javaAPI DocAndroid 5.1 API15978Thu Mar 12 22:22:10 GMT 2015android.widget

Toast

public class Toast extends Object
A toast is a view containing a quick little message for the user. The toast class helps you create and show those. {@more}

When the view is shown to the user, appears as a floating view over the application. It will never receive focus. The user will probably be in the middle of typing something else. The idea is to be as unobtrusive as possible, while still showing the user the information you want them to see. Two examples are the volume control, and the brief message saying that your settings have been saved.

The easiest way to use this class is to call one of the static methods that constructs everything you need and returns a new Toast object.

Developer Guides

For information about creating Toast notifications, read the Toast Notifications developer guide.

Fields Summary
static final String
TAG
static final boolean
localLOGV
public static final int
LENGTH_SHORT
Show the view or text notification for a short period of time. This time could be user-definable. This is the default.
public static final int
LENGTH_LONG
Show the view or text notification for a long period of time. This time could be user-definable.
final android.content.Context
mContext
final TN
mTN
int
mDuration
android.view.View
mNextView
private static android.app.INotificationManager
sService
Constructors Summary
public Toast(android.content.Context context)
Construct an empty Toast object. You must call {@link #setView} before you can call {@link #show}.

param
context The context to use. Usually your {@link android.app.Application} or {@link android.app.Activity} object.


                                                          
       
        mContext = context;
        mTN = new TN();
        mTN.mY = context.getResources().getDimensionPixelSize(
                com.android.internal.R.dimen.toast_y_offset);
        mTN.mGravity = context.getResources().getInteger(
                com.android.internal.R.integer.config_toastDefaultGravity);
    
Methods Summary
public voidcancel()
Close the view if it's showing, or don't show it if it isn't showing yet. You do not normally have to call this. Normally view will disappear on its own after the appropriate duration.

        mTN.hide();

        try {
            getService().cancelToast(mContext.getPackageName(), mTN);
        } catch (RemoteException e) {
            // Empty
        }
    
public intgetDuration()
Return the duration.

see
#setDuration

        return mDuration;
    
public intgetGravity()
Get the location at which the notification should appear on the screen.

see
android.view.Gravity
see
#getGravity

        return mTN.mGravity;
    
public floatgetHorizontalMargin()
Return the horizontal margin.

        return mTN.mHorizontalMargin;
    
private static android.app.INotificationManagergetService()

        if (sService != null) {
            return sService;
        }
        sService = INotificationManager.Stub.asInterface(ServiceManager.getService("notification"));
        return sService;
    
public floatgetVerticalMargin()
Return the vertical margin.

        return mTN.mVerticalMargin;
    
public android.view.ViewgetView()
Return the view.

see
#setView

        return mNextView;
    
public WindowManager.LayoutParamsgetWindowParams()
Gets the LayoutParams for the Toast window.

hide

        return mTN.mParams;
    
public intgetXOffset()
Return the X offset in pixels to apply to the gravity's location.

        return mTN.mX;
    
public intgetYOffset()
Return the Y offset in pixels to apply to the gravity's location.

        return mTN.mY;
    
public static android.widget.ToastmakeText(android.content.Context context, java.lang.CharSequence text, int duration)
Make a standard toast that just contains a text view.

param
context The context to use. Usually your {@link android.app.Application} or {@link android.app.Activity} object.
param
text The text to show. Can be formatted text.
param
duration How long to display the message. Either {@link #LENGTH_SHORT} or {@link #LENGTH_LONG}

        Toast result = new Toast(context);

        LayoutInflater inflate = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
        TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
        tv.setText(text);
        
        result.mNextView = v;
        result.mDuration = duration;

        return result;
    
public static android.widget.ToastmakeText(android.content.Context context, int resId, int duration)
Make a standard toast that just contains a text view with the text from a resource.

param
context The context to use. Usually your {@link android.app.Application} or {@link android.app.Activity} object.
param
resId The resource id of the string resource to use. Can be formatted text.
param
duration How long to display the message. Either {@link #LENGTH_SHORT} or {@link #LENGTH_LONG}
throws
Resources.NotFoundException if the resource can't be found.

        return makeText(context, context.getResources().getText(resId), duration);
    
public voidsetDuration(int duration)
Set how long to show the view for.

see
#LENGTH_SHORT
see
#LENGTH_LONG

        mDuration = duration;
    
public voidsetGravity(int gravity, int xOffset, int yOffset)
Set the location at which the notification should appear on the screen.

see
android.view.Gravity
see
#getGravity

        mTN.mGravity = gravity;
        mTN.mX = xOffset;
        mTN.mY = yOffset;
    
public voidsetMargin(float horizontalMargin, float verticalMargin)
Set the margins of the view.

param
horizontalMargin The horizontal margin, in percentage of the container width, between the container's edges and the notification
param
verticalMargin The vertical margin, in percentage of the container height, between the container's edges and the notification

        mTN.mHorizontalMargin = horizontalMargin;
        mTN.mVerticalMargin = verticalMargin;
    
public voidsetText(int resId)
Update the text in a Toast that was previously created using one of the makeText() methods.

param
resId The new text for the Toast.

        setText(mContext.getText(resId));
    
public voidsetText(java.lang.CharSequence s)
Update the text in a Toast that was previously created using one of the makeText() methods.

param
s The new text for the Toast.

        if (mNextView == null) {
            throw new RuntimeException("This Toast was not created with Toast.makeText()");
        }
        TextView tv = (TextView) mNextView.findViewById(com.android.internal.R.id.message);
        if (tv == null) {
            throw new RuntimeException("This Toast was not created with Toast.makeText()");
        }
        tv.setText(s);
    
public voidsetView(android.view.View view)
Set the view to show.

see
#getView

        mNextView = view;
    
public voidshow()
Show the view for the specified duration.

        if (mNextView == null) {
            throw new RuntimeException("setView must have been called");
        }

        INotificationManager service = getService();
        String pkg = mContext.getOpPackageName();
        TN tn = mTN;
        tn.mNextView = mNextView;

        try {
            service.enqueueToast(pkg, tn, mDuration);
        } catch (RemoteException e) {
            // Empty
        }