[Common Android controls] Effects of Toast

Source: Internet
Author: User

The Toast in Android is a simple message prompt box. The toast prompt box cannot be clicked by users. The toast will automatically disappear after the display time you set. I. display the default Toast implementation code: 1Toast. makeText (getApplicationContext (), "display default Toast", Toast. LENGTH_LONG ). show (); there are two ways to create the default Toast. Let's take a look at the following: 1. displays resource 1Toast in resource. makeText (Context context, int resId, int duration ). show (); parameter: context indicates the context in which toast is displayed, usually the current Activity; resId indicates the Resouce data referenced in the display content, specify the displayed message content from the R class; duration specifies the display time. Toast has two constants by default: LENGTH_SHORT and LENGTH_LONG, respectively indicating the length of the display time; 2. display the custom content 1Toast. makeText (Context context, CharSequence text, int duration ). show (); parameters: context and duration are the same as those of the first method. The text parameter can be used to write the message content. code for displaying Toast with image: 1 Toast toast = Toast. makeText (getApplicationContext (), "display Toast with image", Toast. LENGTH_LONG); 2toast. setGravity (Gravity. CENTER_VERTICAL, 0, 0); 3 LinearLayout toastView = (LinearLayout) toast. getView (); 4 ImageView imageCodeProject = new ImageView (getApplicationContext (); 5imageCodeProject. setImageResource (R. drawable. wirelessqa); 6toastView. addView (imageCodeProject, 0); 7toast. show (); 3. display custom Toast implementation code: 01 LayoutInflater inflater = getLayoutInflater (); 02 03 View layout = inflater. inflate (R. layout. define, (ViewGroup) findViewById (R. id. define); 04 05 TextView title = (TextView) layout. findViewById (R. id. define_title); 06title. setText ("this is custom"); 07 ImageView img = (ImageView) layout. findViewById (R. id. define_img); 08img. setImageResource (R. drawable. wirelessqa); 09 TextView text = (TextView) layout. findViewById (R. id. define_text); 10text. setText ("www.wirelessqa.com"); 11 Toast toast = new Toast (getApplicationContext (); 12toast. setGravity (Gravity. LEFT | Gravity. CENTER, 0, 0); 13toast. setDuration (Toast. LENGTH_LONG); 14 // replace the original ToastView15toast. setView (layout); 16 toast. show (); 4. display the Toast implementation code of other threads: 01displayThread. setOnClickListener (new OnClickListener () {02 03 @ Override04 05 public void onClick (View v) {06 07 new Thread (new Runnable () {08 09 @ Override10 11 public void run () {12 13 otherThreadToast (); 14 15} 16 17 }). start (); 18 19} 20 21}); 22 23} 24 25 private void otherThreadToast () {26 27 handler. post (new Runnable () {28 29 @ Override30 31 public void run () {32 33 Toast. makeText (getApplicationContext (), "this is the Toast of other threads", Toast. LENGTH_LONG ). show (); 34 35} 36 37}); 38 39} 5. code for Displaying Custom positions: 1 Toast toast = Toast. makeText (getApplicationContext (), "Custom Toast display position (top)", Toast. LENGTH_LONG); 2 3 toast. setGravity (Gravity. TOP, 0, 0); 4 5toast. show (); two methods can be used to set the display position: Method 1: setGravity (int gravity, int xOffset, int yOffset). The three parameters respectively represent (start position, horizontal shift to the right, method 2: setMargin (float horizontalMargin, float verticalMargin) sets the display position based on the percentage of the horizontal and vertical directions. The parameters are of the float type (horizontal displacement is positive, right, negative, left, vertical shift positive and negative) 1 // set Toast display position (start position, horizontal to right displacement, vertical to downward displacement) toast. setGravity (Gravity. TOP | Gravity. LEFT, 0,200); 2 // Toast display position, calculated as the percentage of the horizontal and vertical values. The parameters are float type (horizontal displacement is positive, right, negative, LEFT, vertical displacement is positive, top, and bottom) 3toast. setMargin (-0.5f, 0f );

Related Article

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.