Android Dev Guide Series 15: user notification (2) Creation of toast notification

Source: Internet
Author: User

Android Dev Guide Series 15: user notification (2) Creation of toast notification

Lazy bones (http://blog.csdn.com/iamlazybone)

Create a toast notification

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:

Context context = getapplicationcontext (); <br/> charsequence text = "Hello toast! "; <Br/> int duration = toast. length_short; <br/> Toast = toast. maketext (context, text, duration); <br/> toast. show (); 

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:

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

 

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:

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

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

 

Create custom toast Layout

Sometimes 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:

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Id = "@ + ID/toast_layout_root" <br/> Android: orientation = "horizontal" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: padding = "10dp" <br/> Android: Background = "# daaa" <br/> <imageview Android: id = "@ + ID/image" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "fill_parent" <br/> Android: layout_marginright = "10dp" <br/> <textview Android: Id = "@ + ID/text" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "fill_parent" <br/> Android: textcolor = "# fff" <br/> </linearlayout> 

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

Layoutinflater Inflater = getlayoutinflater (); <br/> View layout = Inflater. inflate (R. layout. toast_layout, <br/> (viewgroup) findviewbyid (R. id. toast_layout_root); <br/> imageview image = (imageview) layout. findviewbyid (R. id. image); <br/> image. setimageresource (R. drawable. android); <br/> textview text = (textview) layout. findviewbyid (R. id. text); <br/> text. settext ("Hello! This is a custom toast! "); <Br/> Toast = new toast (getapplicationcontext (); <br/> toast. setgravity (gravity. center_vertical, 0, 0); <br/> toast. setduration (toast. length_long); <br/> toast. setview (layout); <br/> toast. show (); 

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 methods 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.