Toast in Android

Source: Internet
Author: User

Toast notification is a short small message popped up on the window surface. It only fills in the space required by the message, and the user's current activity remains visible and interactive. This notification automatically fades in and out and does not accept user interaction events. The toast notification can be created and displayed by activity or service. If you create a toast notification from the service, it is displayed at the top of the current activity.

The following describes a common method in toast:

1. maketext () method

public static Toast makeText (Context context, CharSequence text, int duration)public static Toast makeText (Context context, int resId, int duration)

You can use the maketext () method to obtain a toast instance. The first maketext method has three parameters: 1. context of the application, 2. text message to be displayed; 3. the time when the toast notification is continuously displayed. Use the show () method to display the toast notification. You can also use strings in the resource file to display text.

Context context = getApplicationContext();CharSequence text = "Hello toast!";int duration = Toast.LENGTH_SHORT;Toast toast = Toast.makeText(context, text, duration);toast.show();

Of course, you can directly use the combination method and avoid creating a toast object:

Toast.makeText(context, text, duration).show();

2. setgravity ()

This method is simple. Set the toast display position:

Toast. setgravity (gravity. Center, 0, 0); // set the toast display position to center

3. setview ()

By viewing the toast source code, we found that this class contains a view variable mnextview, so we can use setview () to implement our custom toast requirements. This method allows us to use custom toast, as shown in the following example:

// Custom toast_layout.xml layout file <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Id = "@ + ID/toast_layout_root" Android: Orientation = "horizontal" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: padding = "8dp" Android: Background = "# daaa"> <imageview Android: src = "@ drawable/droid" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_marginright = "8dp"/> <textview Android: id = "@ + ID/text" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: textcolor = "# fff"/> </linearlayout> // In the activity, layoutinflater Inflater = getlayoutinflater (); view layout = Inflater. inflate (R. layout. custom_toast, (viewgroup) findviewbyid (R. id. toast_layout_root); textview text = (textview) layout. findviewbyid (R. id. text); text. settext ("this is a custom toast"); Toast = new toast (getapplicationcontext (); toast. setgravity (gravity. center_vertical, 0, 0); toast. setduration (toast. length_long); toast. setview (layout); toast. show ();

Note: Do not use a public toast class constructor unless you use the setview (View) method to define layout. If you do not use custom layout, you must use the maketext (context, Int, INT) method to create toast.

For instance demonstration, you can refer to this example, which contains five different toast types and provides download:

Http://www.cnblogs.com/salam/archive/2010/11/10/1873654.html#2390550

Note the following:
When toast is used as the prompt message, toast is displayed at the bottom of the screen, which is generally used to prompt users for incorrect operations. In some cases, when a user tries to operate multiple times consecutively, many toast entries may appear, which are displayed in sequence and remain on the page for a long time, this will seriously affect the user affinity of the software. We can use the following method to show that the toast does not accumulate the time when the toast is not finished, but interrupts the current toast to display the new toast. In this way, toast will not stay on the interface for a long time. A maximum of one toast prompt time is displayed.

 

  private Toast toast = null;        private void showTextToast(String msg) {        if (toast == null) {            toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT);        } else {            toast.setText(msg);        }        toast.show();    }

 

 

 

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.