Toast is a mechanism used by Android to display information. Unlike dialog, Toast has no focus and the display time of toast is limited, after a certain period of time, it will automatically disappear.
1. Default effect:
Code:
Toast. maketext (getapplicationcontext (), "Default toast style ",
Toast. length_short). Show ();
2. Custom display position effect:
Code:
Toast = toast. maketext (getapplicationcontext (),
"Custom location toast", Toast. length_long );
Toast. setgravity (gravity. Center, 0, 0 );
Toast. Show ();
3. image effects
Code
Toast = toast. maketext (getapplicationcontext (),
"Toast with image", 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 customized 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 customized toast ");
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 ();
From: http://android.tgbus.com/Android/tutorial/201103/346236.shtml