Android API--Toast

Source: Internet
Author: User

Android's toast is a simple message prompt box, the toast prompt box cannot be clicked by the user, toast will automatically disappear according to the user set time after display.


Create Toast
Two methods to create a toast
Java code

Maketext (context context, int resid, int duration) 


Parameters: The context is the toast display in which contexts, usually the current activity;resid refers to the display content reference resouce that data, that is, from the R class to specify the content of the message displayed; duration Specifies the display time toast default Length_ The short and Length_long two constants, which represent a small time display and a long time display, respectively.

Java code

Maketext (Context context, charsequence text, int duration) 


Parameter context and duration are the same as the first method, and the parameter text can write the message itself.

The method Show () can be displayed after creating the Toast object with any of the above methods.

Java code

Toast Toast = Toast.maketext (Toastdemoactivity.this, "This is an ordinary toast!", toast.length_short);
 Toast.show (); 


Set Toast Display Location
Two methods to set the display position:
Method One: setgravity (int gravity, int xoffset, int yoffset) three parameters respectively (starting position, horizontal right displacement, vertical downward displacement)
Method Two: SetMargin (float horizontalmargin, float verticalmargin)
Sets the display position as a horizontal and vertical percentage, with the parameters being float (horizontal displacement is right minus left, vertical displacement is negative)

Java code

Set Toast display position (starting position, horizontal right shift, vertical downward shift) 
toast.setgravity (Gravity.top | Gravity.left, 0); 
Toast display position, measured in horizontal and vertical percentages, parameters are float type (horizontal displacement is right negative left, vertical displacement positive upper negative) 
Toast.setmargin ( -0.5f, 0f); 




Custom Toast
The following code shows a toast effect with a picture:


Java code

Toast Button btn2 = (button) Findviewbyid with picture (R.ID.TOAST2); 
Btn2.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {//define a toast
                 Toast Toast = Toast.maketext (Toastdemoactivity.this, "This is the toast! of a generation of pictures", Toast.length_long); 
                Define a imageview ImageView ImageView = new ImageView (toastdemoactivity.this); 
                Imageview.setimageresource (R.drawable.icon); 
                Get toast View View Toastview = Toast.getview ();
                 Define a layout, here is layout linearlayout linearlayout = new LinearLayout (toastdemoactivity.this); 
                Linearlayout.setorientation (linearlayout.horizontal); 
                Merging ImageView and Toastview into layout linearlayout.addview (ImageView); 
                Linearlayout.addview (Toastview); 
               Replace the original Toastview Toast.setview (linearlayout); Toast.show (); 
 } 
        });


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.