Class Toast

java.lang.Object
icyllis.modernui.widget.Toast

public final 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.

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. Example is the brief message saying that your settings have been saved.

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

Note that toasts being sent from the background are rate limited, so avoid sending such toasts in quick succession.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Show the view or text notification for a long period of time.
    static final int
    Show the view or text notification for a short period of time.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Close the view if it's showing, or don't show it if it isn't showing yet.
    int
    Return the duration.
    static Toast
    makeText(Context context, CharSequence text, int duration)
    Make a standard toast that just contains text.
    static Toast
    makeText(CharSequence text, int duration)
    Deprecated, for removal: This API element is subject to removal in a future version.
    void
    setDuration(int duration)
    Set how long to show the view for.
    void
    Update the text in a Toast that was previously created using one of the makeText() methods.
    void
    Show the view for the specified duration.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • LENGTH_SHORT

      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.
      See Also:
    • LENGTH_LONG

      public static final int LENGTH_LONG
      Show the view or text notification for a long period of time. This time could be user-definable.
      See Also:
  • Method Details

    • makeText

      @Deprecated(forRemoval=true) public static Toast makeText(@NonNull CharSequence text, int duration)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • makeText

      @NonNull public static Toast makeText(@NonNull Context context, @NonNull CharSequence text, int duration)
      Make a standard toast that just contains text.
      Parameters:
      text - The text to show. Can be formatted text.
      duration - How long to display the message. Either LENGTH_SHORT or LENGTH_LONG
    • show

      public void show()
      Show the view for the specified duration.
    • cancel

      public void cancel()
      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.
    • setDuration

      public void setDuration(int duration)
      Set how long to show the view for.
      See Also:
    • getDuration

      public int getDuration()
      Return the duration.
      See Also:
    • setText

      public void setText(@NonNull CharSequence s)
      Update the text in a Toast that was previously created using one of the makeText() methods.
      Parameters:
      s - The new text for the Toast.