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:
- Context context = getapplicationcontext ();
- Charsequence text = "Hello toast! ";
- Int duration = toast. length_short;
- Toast = toast. maketext (context, text, duration );
- 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:
- 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:
- 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:
- <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 = "10dp"
- Android: Background = "# daaa"
- >
- <Imageview Android: Id = "@ + ID/image"
- Android: layout_width = "wrap_content"
- Android: layout_height = "fill_parent"
- Android: layout_marginright = "10dp"
- />
- <Textview Android: Id = "@ + ID/text"
- Android: layout_width = "wrap_content"
- Android: layout_height = "fill_parent"
- Android: textcolor = "# fff"
- />
- </Linearlayout>
Copy code
Note that the layout ID is toast_layout. You must use this ID to find the XML, for example:
- Layoutinflater Inflater = getlayoutinflater ();
- View layout = Inflater. Inflate (R. layout. toast_layout,
- (Viewgroup) findviewbyid (R. Id. toast_layout_root ));
- Imageview image = (imageview) layout. findviewbyid (R. Id. Image );
- Image. setimageresource (R. drawable. Android );
- Textview text = (textview) layout. findviewbyid (R. Id. Text );
- Text. settext ("Hello! 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 ();
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.