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.
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 ();
ImageView imagecodeproject = 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");
TextView Text = (TextView) Layout.findviewbyid (r.id.tvtexttoast);
Text.settext ("fully customizable Toast");
toast = new Toast (Getapplicationcontext ());
toast.setgravity (Gravity.right | Gravity.top, (a);
toast.setduration (toast.length_long);
Toast.setview (layout);
toast.show ();
5. Other Threads:
Code:
New Thread (New Runnable () {
public void Run () {
Showtoast ();
}
}). Start ();
Five kinds of toast for Android effects