The Basics
First, instantiate a
Toast
object with one of themakeText()
methods. This method takes three parameters: the applicationContext
, the text message, and the duration for the toast. It returns a properly initialized Toast object. You can display the toast notification withshow()
, 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 ();This example demonstrates everything you need for most toast notifications. You should rarely need anything else. You may, however, want to position the toast differently or even use your own layout instead of a simple text message. The following sections describe how you can do these things.
You can also chain your methods and avoid holding on to the Toast object, like this: