Android Basics: HelloWorld Toast Usage

Source: Internet
Author: User

One: Look at the program


Second: Change the layout:

1: Under res resources, find layout, open Activity_main.xml

Below the graphical layout view, you can customize the drag interface elements to adjust the layout. For example, add a button



2: Open activity_main.xml View, edit source code, add Trigger function for button



3: Adding functions to activity

public void Test (view view) {System.out.println ("------test----"); Toast toast = Toast.maketext (Mainactivity.this, "clicked Button", Toast.length_short); The screen is centered, and the x-axis and y-axis offsets are 0  toast.setgravity (gravity.center, 0, 0); Toast.show ();}



Three: Start the simulator

When you click the button, an informational prompt appears.


Four: Usage of toast

A Toast is a view that quickly displays a small amount of information to the user. A Toast floats on the application to display information to the user, it never gets the focus, does not affect the user's input, and so on, mainly for some help/hints.


The most common way to create a Toast is to use a static method Toast.maketext


1. Default display mode

Java code
    1. //First parameter: the current context. Available Getapplicationcontext () or this
    2. ///second parameter: the string to display. It's also a string ID in r.string.
    3. ///Third parameter: The length of time displayed. Toast defaults to two length_long (long) and Length_short (short), or milliseconds, such as 2000ms
    4. Toast Toast=toast.maketext (Getapplicationcontext (), "Default Toast", Toast.length_short);
    5. //Show toast Information
    6. Toast.show ();

2. Customizing the display location

Java code
  1. Toast Toast=toast.maketext (Getapplicationcontext (), "toast with custom display location", Toast.length_short);
  2. //First parameter: Sets the location where the toast appears on the screen. My current settings are centered on the top
  3. ///second parameter: The offset of the horizontal x-axis of the toast position relative to the first parameter, positive offset to the right, negative to left offset
  4. ///third parameter: Same as the second argument
  5. //If you set an offset that exceeds the range of the screen, the toast will appear in the screen near the outside border
  6. Toast.setgravity (gravity.top| Gravity.center,--- -);
  7. //center display, x-axis and y-axis offset are 0
  8. //toast.setgravity (gravity.center, 0, 0);
  9. Toast.show ();

3. With the picture

Java code
  1. Toast Toast=toast.maketext (Getapplicationcontext (), "show toast with pictures", + );
  2. Toast.setgravity (Gravity.center, 0, 0);
  3. //Create picture View Object
  4. ImageView imageview= New ImageView (Getapplicationcontext ());
  5. //Set Picture
  6. Imageview.setimageresource (R.drawable.ic_launcher);
  7. //Get the toast layout
  8. LinearLayout Toastview = (linearlayout) Toast.getview ();
  9. //Set this layout to landscape
  10. Toastview.setorientation (linearlayout.horizontal);
  11. //ImageView is added to the first position in this layout
  12. Toastview.addview (ImageView, 0);
  13. Toast.show ();

4. Fully customizable display mode

Java code
  1. //inflater means inflatable .
  2. //layoutinflater This class is used to instantiate the layout of an XML file to its corresponding view object
  3. Layoutinflater inflater = Getlayoutinflater ();
  4. //Populate a View object by developing an XML file and layout ID
  5. View layout = Inflater.inflate (R.layout.custom2, (ViewGroup) Findviewbyid (r.id.lltoast));
  6. ImageView image = (ImageView) Layout.findviewbyid (r.id.tvimagetoast);
  7. //Set picture in Picture view in Layout
  8. Image.setimageresource (R.drawable.ic_launcher);
  9. TextView title = (TextView) Layout.findviewbyid (r.id.tvtitletoast);
  10. //Set title
  11. Title.settext ("title bar");
  12. TextView Text = (TextView) Layout.findviewbyid (r.id.tvtexttoast);
  13. //Set content
  14. Text.settext ("fully customizable toast");
  15. Toast toast= New Toast (Getapplicationcontext ());
  16. Toast.setgravity (Gravity.center, 0, 0);
  17. Toast.setduration (Toast.length_long);
  18. Toast.setview (layout);
  19. Toast.show ();

5. Calls from other threads through Handler

Java code
  1. //Call Method 1
  2. //thread th=new Thread (this);
  3. //th.start ();
  4. //Call Method 2
  5. Handler.post (new Runnable () {
  6. @Override
  7. Public void Run () {
  8. Showtoast ();
  9. }
  10. });

Java code
    1. Public void Showtoast () {
    2. Toast Toast=toast.maketext (Getapplicationcontext (), "toast calls in other threads to display", toast.length_short);
    3. Toast.show ();
    4. }

Java code
  1. Handler handler=New Handler () {
  2. @Override
  3. Public void handlemessage (Message msg) {
  4. int what=msg.what;
  5. Switch (what) {
  6. Case 1:
  7. Showtoast ();
  8. break;
  9. default:
  10. break;
  11. }
  12. Super. Handlemessage (msg);
  13. }
  14. };

Java code
    1. @Override
    2. Public void Run () {
    3. Handler.sendemptymessage (1);
    4. }


Android Basics: HelloWorld Toast Usage

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.