Use toast in Android to prompt

Source: Internet
Author: User

Toast may be something quite Android-specific and often used in reality. In this tutorial, we will make a comprehensive summary of the use of toast. There are four examples in total, this section describes the simplest usage, how to adjust the display position, and how to create a custom toast display.
This tutorial focuses on the Code and explains the usage in the annotations. It can be used as a fast query of toast usage.
For more information about the other tips frequently used in Android, see the Tutorial: Using notification in Android.

Activity: toastsample. Java

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
ImportNet. learningandroid. apitest. R;ImportAndroid. App. activity;ImportAndroid. OS. Bundle;ImportAndroid. View. gravity;ImportAndroid. View. layoutinflater;ImportAndroid. View. view;ImportAndroid. View. viewgroup;ImportAndroid. widget. textview;ImportAndroid. widget. Toast;/*** Toast usage overview. * @ author Bing **/public classToastsampleExtendsActivityImplementsView. onclicklistener {@ overrideProtected voidOncreate (bundle savedinstancestate ){Super. Oncreate (savedinstancestate); setcontentview (R. layout. toast_sample); findviewbyid (R. Id. btntoastnormal). setonclicklistener (This); Findviewbyid (R. Id. btntoastposition). setonclicklistener (This); Findviewbyid (R. Id. btntoastmargin). setonclicklistener (This); Findviewbyid (R. Id. btntoastcustomview). setonclicklistener (This) ;}@ OverridePublic voidOnclick (view v ){Switch(V. GETID ()){CaseR. Id. btntoastnormal:// The most common call method. The display duration is short. The value varies depending on the system. You can also specify the number of milliseconds. // The value of the 1st parameter is context, generally, you can specify the current activity instance directly. // 2nd parameters are the text to be displayed. Here, string is used directly, we recommend that you use the pre-defined string // 3rd parameters in XML to display the length, in milliseconds. the predefined toast is used here. length_short, // and toast. length_long can be used. These two values are slightly different depending on the system. // do not forget the last one. show ()Toast. maketext (This, "Toast text, normal", Toast. length_short). Show ();Break;CaseR. Id. btntoastposition:// The default gravity is gravity. center_horizontal | gravity. Bottom // adjust only the yoffset here, so that the text display position is closer to the nextToast t2 = toast. maketext (This, "Toast text with specific position", Toast. length_long); t2.setgravity (gravity. center_horizontal | gravity. Bottom, 0, 10); t2.show ();Break;CaseR. Id. btntoastmargin:// If You Want To adjust the display position significantly, we recommend that you use the setmargin method. // The parameters accepted by setmargin are the percentage of the horizontal and vertical values, respectively, in this way, the adaptability is better under different resolutions. // This is changed to display above the portrait center of the screenToast T3 = toast. maketext (This, "Toast text with specific margin and position", toast. length_short); t3.setgravity (gravity. center_horizontal | gravity. bottom, 0, 0); t3.setmargin (0f, 0.5f); t3.show ();Break;CaseR. Id. btntoastcustomview:// Use a custom view to display toast. You must first compile a layout definition file // In fact, the toast. maketext method is also called in this way.Layoutinflater Inflater = getlayoutinflater (); view layout = Inflater. Inflate (R. layout. toast_view_sample, (viewgroup) findviewbyid (R. Id. toastsamplelayout ),False); Textview text = (textview) layout. findviewbyid (R. id. toast_text); text. settext ("toast with custom view, it's a long text: "+" Manuka honey interferes with bacteria infecting a wound "+" by keeping the microbes from attaching to tissue "+" and even by making antibiotics more effective. "+" Cynthia Graber reports. "); toast t4 =NewToast (This); T4.setgravity (gravity. center_horizontal | gravity. Bottom, 0, 50); t4.setduration (toast. length_long); t4.setview (layout); t4.show ();Break;Default:Break;}}}

Definition file of the custom view used in the 4th example: toast_view_sample.xml

123456789101112131415161718
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"android:id="@+id/toastViewLayout" android:orientation="horizontal"android:padding="20dp" android:background="@android:drawable/toast_frame">    <ImageView android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:layout_marginRight="10dp"               android:src="@drawable/toast_icon"               />    <TextView android:id="@+id/toast_text"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:textColor="#FFFFFFFF"              /></LinearLayout>

Here, @ Android: drawable/toast_frame is actually the background used by the default toast of Android, while @ drawable/toast_icon is used by me by finding one icon.
Final running effect:

2011,
Bing. All rights reserved. For all reposts, please use links.

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.