Home | History | Annotate | Download | only in notifiers
      1 page.title=Creating Toast Notifications
      2 parent.title=Notifying the User
      3 parent.link=index.html
      4 @jd:body
      5 
      6 <div id="qv-wrapper">
      7   <div id="qv">
      8     <h2>Key classes</h2>
      9     <ol>
     10       <li>{@link android.widget.Toast}</li>
     11     </ol>
     12     <h2>In this document</h2>
     13     <ol>
     14       <li><a href="#Basics">The Basics</a></li>
     15       <li><a href="#Position">Positioning your Toast</a></li>
     16       <li><a href="#CustomToastView">Creating a Custom Toast View</a></li>
     17     </ol>
     18   </div>
     19 </div>
     20 
     21 <p>A toast notification is a message that pops up on the surface of the window.
     22 It only fills the amount of space required for the message and the user's current
     23 activity remains visible and interactive. The notification automatically fades in and 
     24 out, and does not accept interaction events.</p>
     25 
     26 <p>The screenshot below shows an example toast notification from the Alarm application.
     27 Once an alarm is turned on, a toast is displayed to assure you that the 
     28 alarm was set.</p>
     29 <img src="{@docRoot}images/toast.png" alt="" />
     30 
     31 <p>A toast can be created and displayed from an {@link android.app.Activity} or 
     32 {@link android.app.Service}. If you create a toast notification from a Service, it
     33 appears in front of the Activity currently in focus.</p>
     34 
     35 <p>If user response to the notification is required, consider using a 
     36 <a href="notifications.html">Status Bar Notification</a>.</p>
     37 
     38 
     39 <h2 id="Basics">The Basics</h2>
     40 
     41 <p>First, instantiate a {@link android.widget.Toast}
     42 object with one of the {@link android.widget.Toast#makeText(Context,int,int) makeText()} methods.
     43 This method takes three parameters: the application {@link android.content.Context},
     44 the text message, and the duration for the toast. It returns a properly initialized Toast
     45 object. You can display the toast notification with {@link android.widget.Toast#show()},
     46 as shown in the following example:</p>
     47 
     48 <pre>
     49 Context context = getApplicationContext();
     50 CharSequence text = "Hello toast!";
     51 int duration = Toast.LENGTH_SHORT;
     52 
     53 Toast toast = Toast.makeText(context, text, duration);
     54 toast.show();
     55 </pre>
     56 
     57 <p>This example demonstrates everything you need for most toast notifications.
     58 You should rarely need anything else. You may, however, want to position the 
     59 toast differently or even use your own layout instead of a simple text message. 
     60 The following sections describe how you can do these things.</p>
     61 
     62 <p>You can also chain your methods and avoid holding on to the Toast object, like this:</p>
     63 <pre>Toast.makeText(context, text, duration).show();</pre>
     64 
     65 
     66 <h2 id="Positioning">Positioning your Toast</h2>
     67 
     68 <p>A standard toast notification appears near the bottom of the screen, centered horizontally.
     69 You can change this position with the {@link android.widget.Toast#setGravity(int,int,int)}
     70 method. This accepts three parameters: a {@link android.view.Gravity} constant, 
     71 an x-position offset, and a y-position offset.</p>
     72 
     73 <p>For example, if you decide that the toast should appear in the top-left corner, you can set the
     74 gravity like this:</p>
     75 <pre>
     76 toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
     77 </pre>
     78 
     79 <p>If you want to nudge the position to the right, increase the value of the second parameter. 
     80 To nudge it down, increase the value of the last parameter.
     81 
     82 
     83 <h2 id="CustomToastView">Creating a Custom Toast View</h2>
     84 
     85 <img src="{@docRoot}images/custom_toast.png" alt="" style="float:right" />
     86 
     87 <p>If a simple text message isn't enough, you can create a customized layout for your
     88 toast notification. To create a custom layout, define a View layout,
     89 in XML or in your application code, and pass the root {@link android.view.View} object
     90 to the {@link android.widget.Toast#setView(View)} method.</p>
     91 
     92 <p>For example, you can create the layout for the toast visible in the screenshot to the right
     93 with the following XML (saved as <em>toast_layout.xml</em>):</p>
     94 <pre>
     95 &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     96               android:id="@+id/toast_layout_root"
     97               android:orientation="horizontal"
     98               android:layout_width="fill_parent"
     99               android:layout_height="fill_parent"
    100               android:padding="10dp"
    101               android:background="#DAAA"
    102               >
    103     &lt;ImageView android:id="@+id/image"
    104                android:layout_width="wrap_content"
    105                android:layout_height="fill_parent"
    106                android:layout_marginRight="10dp"
    107                />
    108     &lt;TextView android:id="@+id/text"
    109               android:layout_width="wrap_content"
    110               android:layout_height="fill_parent"
    111               android:textColor="#FFF"
    112               />
    113 &lt;/LinearLayout>
    114 </pre> 
    115 
    116 <p>Notice that the ID of the LinearLayout element is "toast_layout". You must use this
    117 ID to inflate the layout from the XML, as shown here:</p>
    118 
    119 <pre>
    120 LayoutInflater inflater = getLayoutInflater();
    121 View layout = inflater.inflate(R.layout.toast_layout,
    122                                (ViewGroup) findViewById(R.id.toast_layout_root));
    123 
    124 ImageView image = (ImageView) layout.findViewById(R.id.image);
    125 image.setImageResource(R.drawable.android);
    126 TextView text = (TextView) layout.findViewById(R.id.text);
    127 text.setText("Hello! This is a custom toast!");
    128 
    129 Toast toast = new Toast(getApplicationContext());
    130 toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    131 toast.setDuration(Toast.LENGTH_LONG);
    132 toast.setView(layout);
    133 toast.show();
    134 </pre>
    135 
    136 <p>First, retrieve the {@link android.view.LayoutInflater} with 
    137 {@link android.app.Activity#getLayoutInflater()} 
    138 (or {@link android.content.Context#getSystemService(String) getSystemService()}),
    139 and then inflate the layout from XML using 
    140 {@link android.view.LayoutInflater#inflate(int, ViewGroup)}. The first parameter
    141 is the layout resource ID and the second is the root View. You can use
    142 this inflated layout to find more View objects in the layout, so now capture and 
    143 define the content for the ImageView and TextView elements. Finally, create
    144 a new Toast with {@link android.widget.Toast#Toast(Context)} and set some properties
    145 of the toast, such as the gravity and duration. Then call
    146 {@link android.widget.Toast#setView(View)} and pass it the inflated layout.
    147 You can now display the toast with your custom layout by calling 
    148 {@link android.widget.Toast#show()}.</p>
    149 
    150 <p class="note"><strong>Note:</strong> Do not use the public constructor for a Toast 
    151 unless you are going to define the layout with {@link android.widget.Toast#setView(View)}.
    152 If you do not have a custom layout to use, you must use
    153 {@link android.widget.Toast#makeText(Context,int,int)} to create the Toast.</p>
    154 
    155