Step-by-step _android Development Course [32]_ User interface Toast (toast)

Source: Internet
Author: User

Focus on technology, enjoy life! --qq:804212028
Browse Links: http://blog.csdn.net/y18334702058/article/details/44624305

    • Topic: User interface Toast (toast)
      -Some tips to use in your app

structure Analysis of toast:
Toast Toast=toast.maketext (mainactivity.this, "Default Toast", Toast.length_short). Show ();

First parameter: The current context environment. Available Getapplicationcontext () or this.
Second parameter: the string to display. It's also a string ID in r.string.
The third parameter: the length of time displayed. Toast defaults to two length_long (long) and Length_short (short), or milliseconds, such as 2000ms
Finally, the toast information is displayed: Call the Show () method.

Toast (instance):

Default Toast Effect:

Customizing the display location Toast effect:

Customize the toast effect with pictures:

Fully customizable toast effects:

Thread Start toast Effect:

Implementation code:
Activity_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">       <buttonandroid:layout_height="Wrap_content"android:layout_width= "Fill_parent" android:id="@+id/btnsimpletoast"android:text="Default">                                </Button> <buttonandroid:layout_height="Wrap_content"android:layout_width= "Fill_parent" Android:text="Custom display location"android:id="@+id/btnsimpletoastwithcustomposition" >                                </Button> <buttonandroid:layout_height="Wrap_content"android:layout_width= "Fill_parent" android:id="@+id/btnsimpletoastwithimage"android:text="with picture">                               </Button> <buttonandroid:layout_height="Wrap_content"android:layout_width= "Fill_parent" Android:text="fully customizable"android:id="@+id/btncustomtoast">                               </Button> <buttonandroid:layout_height="Wrap_content"android:layout_width= "Fill_parent" Android:text="Other threads"android:id="@+id/btnruntoastfromotherthread">                               </Button></linearlayout>

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  =;  <textview  android:layout _height  = "wrap_content"  android:layout_ Margin  = "1dip"  android:textcolor  =< Span class= "Hljs-value" > "#ffffffff"  android:layout_width  =" fill_parent " android:gravity  =" Center " android:background  =" #bb000000 "  android:id  = "@+id/tvtitletoast" />  <linearlayout  android: Layout_height  = "wrap_content"  android:o Rientation  = "vertical"  android:id  =< Span class= "Hljs-value" > "@+id/lltoastcontent"  android:layout_marginleft  = "1dip"  android:layout_marginright  =< Span class= "Hljs-value" > "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" />   <TextViewandroid: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>

Mainactivity.java:

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. View. Onclicklistener;Import Android. View. ViewGroup;Import Android. Widgets. ImageView;Import Android. Widgets. LinearLayout;Import Android. Widgets. TextView;Import Android. Widgets. Toast;public class Mainactivity extends Activity implements Onclicklistener {Handler Handler = new Handler ();@Override public void OnCreate (Bundle savedinstancestate) {Super. OnCreate(savedinstancestate);Setcontentview (R. Layout. Activity_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_short). Show();        Break;Case R. ID. Btnsimpletoastwithcustomposition: Toast = Toast. Maketext(Getapplicationcontext (),"Custom location Toast", Toast. LENGTH_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. MyImage);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. MyImage);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. CENTER,0,0);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;}     }}

Focus on technology, enjoy life! --qq:804212028
Browse Links: http://blog.csdn.net/y18334702058/article/details/44624305

Step-by-step _android Development Course [32]_ User interface Toast (toast)

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.