A summary of Toast display messages in Android development _android

Source: Internet
Author: User
Tags sqlite database

The example of this article summarizes the method of toast display message in Android development. Share to everyone for your reference, specific as follows:

Android provides a simple toast message box mechanism, you can click on the user after some buttons, prompts the user some information, the message can not be clicked by the user, toast the message according to the user set the display time after the automatic disappearance. Toast hints can easily display something that you want to show when you debug a program.

Two ways to create a toast

Java code for the first method:

Maketext (context context, int resid, int duration)

Parameters: The context is the toast display in which contexts, usually the current activity;resid refers to the display content reference resouce that data, that is, from the R class to specify the content of the message displayed; duration Specifies the display time toast default Length_ The short and Length_long two constants, which represent a small time display and a long time display, respectively.

Java code for the second method:

Maketext (Context context, charsequence text, int duration)

Parameter context and duration are the same as the first method, and the parameter text can write the message itself.

The method Show () can be displayed after creating the Toast object with any of the above methods.

Toast Toast = Toast.maketext (Toastdemoactivity.this, "This is an ordinary toast!", toast.length_short);
Toast.show ();

Set Toast Display Location

There are two ways to set the location of the display:

Method One:

setgravity (int gravity, int xoffset, int yoffset)

Three parameters respectively (starting position, horizontal right displacement, vertical downward displacement)

Method Two:

SetMargin (float horizontalmargin, float verticalmargin)

Sets the display position as a horizontal and vertical percentage, with the parameters being float (horizontal displacement is right minus left, vertical displacement is negative)

Set Toast display position (starting position, horizontal right shift, vertical downward shift)
toast.setgravity (Gravity.top | Gravity.left, 0);
Toast display position, measured in horizontal and vertical percentages, parameters are float type (horizontal displacement is right negative left, vertical displacement positive upper negative)
Toast.setmargin ( -0.5f, 0f);

Custom Toast

The following code can display a toast effect with a picture:

//toast button with picture btn2 = (Button) Findviewbyid (R.ID.TOAST2); Btn2.setonclicklistener ( New Onclicklistener () {public void OnClick (View v) {//define a Toast Toast Toast = Toast.maketext (toastdemoactivit
    Y.this, "This is a generation of pictures of toast!", Toast.length_long);
    Define a imageview ImageView ImageView = new ImageView (toastdemoactivity.this);
    Imageview.setimageresource (R.drawable.icon);
    Get toast View View Toastview = Toast.getview ();
    Define a Layout, here is Layout linearlayoutlinear Layout = new LinearLayout (toastdemoactivity.this);
    Linearlayout.setorientation (linearlayout.horizontal);
    Merging ImageView and Toastview into layout linearlayout.addview (ImageView);
    Linearlayout.addview (Toastview);
    Replace the original Toastview Toast.setview (linearlayout);
  Toast.show ();

}
}); 

For more information on Android-related content readers can view the site: "The activity of Android programming skills Summary", "Android Resources Operating Skills Summary", "Android File Operating skills summary", " Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", The Android View view tips summary and the Android Control usage summary

I hope this article will help you with the Android program.

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.