A Toast is a view that quickly displays a small amount of information to the user. A Toast floats on the application to display information to the user, it never gets the focus, does not affect the user's input, and so on, mainly for some help / hints.
The most common way to create a Toast is to use a static method Toast.maketext
I'm using SDK 2.2.
1. Default display mode
Java code
First parameter: The current context environment. Available Getapplicationcontext () or this//second parameter: the string to display. It's also a r.string. String ID//Third parameter: The length of time displayed. The toast default has two Length_long (long) and Length_short (short), and can also use milliseconds such as 2000ms toast Toast=toast.maketext (Getapplicationcontext (), " Default Toast ", Toast.length_short); Show toast Information toast.show ();
2. Customizing the display location
Java code
toast toast=toast.maketext ( Getapplicationcontext (), "Toast with custom display location", toast.length_short); //First parameter: Sets the location where the toast appears on the screen. My current settings are centered on top //the second parameter: The offset of the horizontal x-axis of the toast position relative to the first parameter, Positive offset to the right and negative to the left //the third argument: the same as the second argument for the same reason //If you set an offset that exceeds the range of the screen, the toast will appear within the screen near the outside border toast.setgravity (gravity.top| gravity.center, -50, 100); //Screen Center display , the x-axis and y-axis offsets are 0 //toast.setgravity (Gravity.CENTER, 0, 0); toast.show ();
3. With the picture
Java code
Toast toast=toast.maketext (Getapplicationcontext (), "show toast with pictures", 3000); toast.setgravity (gravity.center, 0, 0); //Creating a Picture View object imageview imageview= new imageview (Getapplicationcontext ()); //Settings Picture Imageview.setimageresource (R.drawable.ic_launcher); //Get the toast layout linearlayout toastview = (LinearLayout) toast.getview (); / /set this layout to landscape toastview.setorientation ( Linearlayout.horizontal); //will imageview the first position in this layout toastview.addview (imageview, 0); Toast.show ();
4. Fully customizable display mode
Java code
Inflater means inflatable //layoutinflater. This class is used to instantiate the layout of an XML file to its corresponding view object LayoutInflater inflater = Getlayoutinflater (); //fills a View object by developing an XML file and layout ID view layout = inflater.inflate (R.layout.custom2 , (ViewGroup) Findviewbyid (r.id.lltoast)); ImageView image = (ImageView) Layout.findviewbyid (r.id.tvimagetoast); //set picture in Picture view in layout image.setimageresource (R.drawable.ic_launcher); textview title = (TextView) layout.findviewbyiD (r.id.tvtitletoast); //set title title.settext ("title bar"); TextView text = (TextView) Layout.findviewbyid (r.id.tvtexttoast); //Set Content text.settext ("Fully customizable toast"); toast toast= new toast ( Getapplicationcontext ()); toast.setgravity (Gravity.CENTER , 0, 0); toast.setduration (Toast.LENGTH_ LONG); toast.setview (layout); toast.shoW ();
5. Calls from other threads through Handler
Java code
//Call Method 1 //thread th=new thread (this); //th.start (); //Call Method 2 handler.post (new runnable () { @Override public void run () { showtoast (); } });
Handler handler=new handler () { @Override public void handlemessage (Message msg) { int what= msg.what; switch (What ) { case 1: Showtoast (); break; default: break; } super.handlemessage (msg); } };
@Override public void Run () {handler.sendemptymessage (1); }
Android Application Development Learning-toast use method Daquan