Dev Guide/framework topics/User Interface/notifications/toast notifications

Source: Internet
Author: User

Create a toast notification

 

20:08:23 upload

Download Attachment
(6.39 KB)

A toast notification is a message popped up from the current window. It only occupies an area that meets the reality of text information, and the following activity is visible and operable. Information will automatically fade away, and user operations will not be accepted.

The example of a toast in the alarm program is displayed. Once the alarm program is enabled, a toast will be displayed.

Basic knowledge

First, use the maketext () method to instantiate a toast object. The method has three parameters: application context, prompt text, and display time. The method returns a correctly initialized toast object, and then calls the show () method to display it:


  1. Context context = getapplicationcontext ();
  2. Charsequence text = "Hello toast! ";
  3. Int duration = toast. length_short;
  4. Toast = toast. maketext (context, text, duration );
  5. Toast. Show ();

Copy code

This example demonstrates in detail every step of toast display, which is very complete. You can also use your own layout to display it. It is not just a text, but will be mentioned later in the chapter. You can also display toast as follows:

  1. Toast. maketext (context, text, duration). Show ();

Copy code

Toast display position

The standard toast is displayed in the left-right corner of the screen. You can also use the setgravity (INT, Int, INT) method to customize the location, for example:

  1. Toast. setgravity (gravity. Top | gravity. Left, 0, 0 );

Copy code

If you want to offset toast to the right, add the second parameter, and then add the second parameter.

Create custom toast LayoutSometimes a simple text is not enough. You can customize a layout. Create a custom XML layout file or code and pass the Root View to the setview () method:

  1. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  2. Android: Id = "@ + ID/toast_layout_root"
  3. Android: Orientation = "horizontal"
  4. Android: layout_width = "fill_parent"
  5. Android: layout_height = "fill_parent"
  6. Android: padding = "10dp"
  7. Android: Background = "# daaa"
  8. >
  9. <Imageview Android: Id = "@ + ID/image"
  10. Android: layout_width = "wrap_content"
  11. Android: layout_height = "fill_parent"
  12. Android: layout_marginright = "10dp"
  13. />
  14. <Textview Android: Id = "@ + ID/text"
  15. Android: layout_width = "wrap_content"
  16. Android: layout_height = "fill_parent"
  17. Android: textcolor = "# fff"
  18. />
  19. </Linearlayout>

Copy code

Note that the layout ID is toast_layout. You must use this ID to find the XML, for example:

  1. Layoutinflater Inflater = getlayoutinflater ();
  2. View layout = Inflater. Inflate (R. layout. toast_layout,
  3. (Viewgroup) findviewbyid (R. Id. toast_layout_root ));
  4. Imageview image = (imageview) layout. findviewbyid (R. Id. Image );
  5. Image. setimageresource (R. drawable. Android );
  6. Textview text = (textview) layout. findviewbyid (R. Id. Text );
  7. Text. settext ("Hello! This is a custom toast! ");
  8. Toast = new toast (getapplicationcontext ());
  9. Toast. setgravity (gravity. center_vertical, 0, 0 );
  10. Toast. setduration (toast. length_long );
  11. Toast. setview (layout );
  12. Toast. Show ();

Copy code

First, use the getlayoutflater () method to retrieve layoutinflater, and then use the inflate (INT, viewgroup) method to find the XML file. The first parameter is the resource ID of the layout file, the second parameter is the root view. You can use the inflated layout to find more view and layout objects. Now you can find the textview and imageview elements. Finally, use the toast (context) method to create a new toast and set its parameters. For example, location and time. Call setview (View) and pass it to inflated.
Layout. Call the show () method to display the custom toast. Note: Public constructor cannot be used unless you call the setview (View) method. If you do not use a custom layout, you can only use the maketext (context, Int, INT) method.

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.