Toast is a mechanism used to display information in Android, unlike dialog, where toast is not focused, and toast is displayed with limited time, and will automatically disappear after a certain amount of time. Here's an example to see how to use toast.
1. Default effect
Code
Toast.maketext (Getapplicationcontext (), default Toast style, toast.length_short). Show ();
2. Customize the display position effect
Code
Toast = Toast.maketext (Getapplicationcontext (), "Custom location Toast", Toast.length_long); Toast.setgravity (gravity.center, 0, 0); Toast.show ();
3. With picture effect
Code
Toast = Toast.maketext (Getapplicationcontext (), "toast with pictures", toast.length_long);
Toast.setgravity (gravity.center, 0, 0);
LinearLayout Toastview = (linearlayout) Toast.getview ();
New ImageView (Getapplicationcontext ());
Imagecodeproject.setimageresource (R.drawable.icon);
Toastview.addview (imagecodeproject, 0);
Toast.show ();
4. Fully customizable effects
Code
Layoutinflater inflater = Getlayoutinflater ();
View layout = Inflater.inflate (R.layout.custom,
(ViewGroup) Findviewbyid (r.id.lltoast));
ImageView image = (ImageView) Layout.findviewbyid (r.id.tvimagetoast);
Image.setimageresource (R.drawable.icon);
TextView title = (TextView) Layout.findviewbyid (r.id.tvtitletoast);
Title.settext ("Attention");
Layout.findviewbyid (R.id.tvtexttoast);
Text.settext ("Fully customizable toast");
New Toast (Getapplicationcontext ());
Toast.setgravity (Gravity.right | Gravity.top, 12, 40);
Toast.setduration (Toast.length_long);
Toast.setview (layout);
Toast.show ();
5. Other Threads
Code
New Thread (new Runnable () {public void run () { showtoast ()} }). Start ();
Custom.xml
<?XML version= "1.0" encoding= "Utf-8"?>
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_height= "Wrap_content"
Android:layout_width= "Wrap_content"
Android:background= "#ffffffff"
Android:orientation= "vertical"
Android:id= "@+id/lltoast" >
<TextView
Android:layout_height= "Wrap_content"
Android:layout_margin= "1dip"
Android:textcolor= "#ffffffff"
Android:layout_width= "Fill_parent"
Android:gravity= "Center"
Android:background= "#bb000000"
Android:id= "@+id/tvtitletoast" />
<LinearLayout
Android:layout_height= "Wrap_content"
Android:orientation= "vertical"
Android:id= "@+id/lltoastcontent"
Android:layout_marginleft= "1dip"
Android:layout_marginright= "1dip"
Android:layout_marginbottom= "1dip"
Android:layout_width= "Wrap_content"
Android:padding= "15dip"
Android:background= "#44000000" >
<ImageView
Android:layout_height= "Wrap_content"
Android:layout_gravity= "Center"
Android:layout_width= "Wrap_content"
Android:id= "@+id/tvimagetoast" />
<TextView
Android:layout_height= "Wrap_content"
Android:paddingright= "10dip"
Android:paddingleft= "10dip"
Android:layout_width= "Wrap_content"
Android:gravity= "Center"
Android:textcolor= "#ff000000"
Android:id= "@+id/tvtexttoast" />
</LinearLayout>
</LinearLayout>
Android Toast.maketext Usage