Toast can show the help/prompts to user. There have five effect of toast as bellow:
1.default Effect:
Code
Toast.maketext (Getapplicationcontext (), default Toast style, toast.length_short). Show ();
2.custom Position Effect:
Code
Toast = Toast.maketext (Getapplicationcontext (), "Custom location Toast", Toast.length_long); Toast.setgravity (gravity.center, 0, 0); Toast.show ();
3.with the 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.completely Custom Effect:
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.from Other Thread:
Code
New Thread (New Runnable () {public void run () { showtoast (); } }). Start ();
All of the code as bellow:
1.main.java
Package Com.wjq.toast;import Android.app.activity;import Android.os.bundle;import android.os.handler;import Android.view.gravity;import Android.view.layoutinflater;import Android.view.view;import Android.view.ViewGroup; Import Android.view.view.onclicklistener;import Android.widget.imageview;import android.widget.LinearLayout; Import Android.widget.textview;import Android.widget.toast;public class Main extends Activity implements Onclicklistener {Handler Handler = new Handler (); @Override public void OnCreate (Bundle savedinstancestate) {SUPER.ONCR Eate (savedinstancestate); Setcontentview (R.layout.main); Findviewbyid (R.id.btnsimpletoast). Setonclicklistener (this); Findviewbyid (r.id.btnsimpletoastwithcustomposition). Setonclicklistener (this); Findviewbyid (r.id.btnsimpletoastwithimage). Setonclicklistener (this); Findviewbyid (R.id.btncustomtoast). Setonclicklistener (this); Findviewbyid (R.id.btnruntoastfromotherthread). Setonclicklistener (this); } public void Showtoast () {Handler.posT (new Runnable () {@Override public void run () {Toast.maketext (Getapplicationcontext (), "I'm from another thread! ", Toast.length_short). Show (); } }); } @Override public void OnClick (View v) {toast toast = null; Switch (V.getid ()) {Case R.id.btnsimpletoast:toast.maketext (Getapplicationcontext (), "Default Toast style", toast.length_sh ORT). Show (); Break Case r.id.btnsimpletoastwithcustomposition:toast = Toast.maketext (Getapplicationcontext (), "Custom location toast", Toast.LENG Th_long); Toast.setgravity (gravity.center, 0, 0); Toast.show (); Break Case r.id.btnsimpletoastwithimage: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 (); Break Case R.id.btncustomtoast: 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, 12, 40); Toast.setduration (Toast.length_long); Toast.setview (layout); Toast.show (); Break Case R.id.btnruntoastfromotherthread:new Thread (new Runnable () {public void run () {showtoast (); }}). Start (); Break } }}
2.main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " android:padding= "5dip" android:gravity= "center" > <button android:layout_height= "wrap_content" android:layout _width= "Fill_parent" android:id= "@+id/btnsimpletoast" android:text= "Default" ></Button> <button android: layout_height= "Wrap_content" android:layout_width= "fill_parent" android:text= "Custom display location" Android:id= "@+id/ Btnsimpletoastwithcustomposition "></Button> <button android:layout_height=" Wrap_content "Android: Layout_width= "Fill_parent" android:id= "@+id/btnsimpletoastwithimage" android:text= "with pictures" ></Button> < Button android:layout_height= "wrap_content" android:layout_width= "fill_parent" android:text= "fully customizable" android:id= "@+" Id/btncustomtoast "></Button> <button android:layout_height=" Wrap_content "android:layout_width="Fill_parent" android:text= "Other Threads" android:id= "@+id/btnruntoastfromotherthread" ></button></ linearlayout> 3.custom.xml<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns: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" Androi d:paddingleft= "10dip" android:layout_width= "wrap_content" android:gravity= "center" android:textcolor= "#ff000000" a Ndroid:id= "@+id/tvtexttoast"/> </LinearLayout></LinearLayout>
Source code Download
If you also interest on Linux and Android embed system,please connection with us in QQ grounp:139761394