Android Toast.maketext Usage

Source: Internet
Author: User

Toast is a mechanism used to display information in Android, unlike dialog, where toast is not focused, and toast is displayed with limited time, and will automatically disappear after a certain amount of time. Here's an example to see how to use toast.

1. Default effect

Code

Toast.maketext (Getapplicationcontext (), default Toast style,      toast.length_short). Show ();

2. Customize the display position effect

Code

Toast = Toast.maketext (Getapplicationcontext (),      "Custom location Toast", Toast.length_long);    Toast.setgravity (gravity.center, 0, 0);    Toast.show ();

3. With picture effect

Code

Toast = Toast.maketext (Getapplicationcontext (),      "toast with pictures", toast.length_long);    
Toast.setgravity (gravity.center, 0, 0);
LinearLayout Toastview = (linearlayout) Toast.getview ();
New ImageView (Getapplicationcontext ());
Imagecodeproject.setimageresource (R.drawable.icon);
Toastview.addview (imagecodeproject, 0);
Toast.show ();

4. Fully customizable 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");

Layout.findviewbyid (R.id.tvtexttoast);
Text.settext ("Fully customizable 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 ();

Custom.xml

<?XML version= "1.0" encoding= "Utf-8"?> 
<LinearLayoutxmlns: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"
Android:paddingleft= "10dip"
Android:layout_width= "Wrap_content"
Android:gravity= "Center"
Android:textcolor= "#ff000000"
Android:id= "@+id/tvtexttoast" />
</LinearLayout>
</LinearLayout>

Android Toast.maketext Usage

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.