Android toast settings to the middle of the screen, customizing the Toast implementation method, and its description

Source: Internet
Author: User

Android toast is used to display a message to the user on the phone screen, and the information will automatically disappear after a period of time. The information can be simple text or complex pictures and other content (display a view).

1. Simple usage

Toast.maketext (Midlet.getapplicationcontext (), "User name cannot be empty", Toast.length_long). Show ();

2. Customize the display position effect

Code

Toast = Toast.maketext (Getapplicationcontext (),
"Custom Location Toast", Toast.length_long);
Toast.setgravity (gravity.center, 0, 0);
Toast.show ();

3. With picture effect

Code

Toast = Toast.maketext (Getapplicationcontext (),
"Toast with pictures", toast.length_long);
Toast.setgravity (gravity.center, 0, 0);
LinearLayout Toastview = (linearlayout) Toast.getview ();
ImageView imagecodeproject = new ImageView (Getapplicationcontext ());
Imagecodeproject.setimageresource (R.drawable.icon);
Toastview.addview (imagecodeproject, 0);
Toast.show ();

Three, constant

int Length_long

Persistent display of views or text prompts for a longer period of time. The length of the time can be customized.

See

setduration (int)

int Length_short

Displays the view or text prompt continuously for a short time. The length of the time can be customized. The value is the default value.

See

setduration (int)

Four, the constructor function

Public Toast (Context context)

Constructs an empty Toast object. Before calling show (), you must first call Setview (View).

(Translator Note: Only use Setview (View) to get Toast objects using the new toast), or you must use the Maketext () method to create Toast objects. And this way getting toast objects cannot use the SetText () method. )

Parameters

Contexts used by the context. This is usually your application or Activity object.

V. Public methods

public int Cancel ()

If the view is already displayed, it is turned off and no longer displayed. You generally do not need to call this method. Under normal circumstances, the view disappears after the surviving period.

public int getduration ()

Return to the surviving period

Please see

setduration (int)

public int getgravity ()

Gets the location where the hint message appears on the screen.

Please see

Gravity

Setgravity ()

public float Gethorizontalmargin ()

Returns the blank space outside the horizontal bar.

public float Getverticalmargin ()

Returns the blank space outside the vertical column.

Public Viewgetview ()

Returns the View object.

Please see

Setview (View)

public int Getxoffset ()

Returns the amount of horizontal offset pixels relative to the reference position.

Toast msg = Toast.maketext (main.this, "Message", Toast.length_long);        Msg.setgravity (Gravity.center, Msg.getxoffset ()/2, Msg.getyoffset ()/2); Msg.show ();

public int Getyoffset ()

Returns the amount of vertical offset pixels relative to the reference position.

public static Toastmaketext (context context, int resId, int duration)

Generates a standard Toast object from a resource that contains a text view.

Parameters

Context

The context to use. This is usually your application or activity object.

ResId

The string resource ID to use, which can be formatted text.

Duration

The duration of the information. Value is Length_short or Length_lon

Abnormal

Throws an exception when the resource is not found resources.notfoundexception

public static Toastmaketext (context context, charsequence text, int duration)

Produces a standard Toast object that contains a text view.

Parameters

Context

The context to use. This is usually your application or activity object.

ResId

The text to display, which can be formatted text.

Duration

The duration of the information. Value is Length_short or Length_long

public void setduration (int duration)

Set the duration of the lifetime. Note: The setting is too large to work, the actual available maximum value of duration is 3500, which can only display up to 3.5s

Please see

Length_short

Length_long

public void setgravity (int gravity, int xoffset, int yoffset)

Sets the display position of the cue information on the screen.

(Translator Note: The display location of a custom toast, such as toast.setgravity (gravity.center_vertical, 0, 0), can be positioned in the upper left corner of the toast. Location of toast hint Xoffset: Move more than 0 to the right, less than 0 to the left)

Please see

Gravity

Getgravity ()

public void SetMargin (float horizontalmargin, float verticalmargin)

Sets the outer space of the view.

Parameters

HorizontalMargin the edge of the container and the horizontal blank of the hint information (ratio to the container width).

VerticalMargin the edge of the container and the vertical padding of the hint information (ratio to the container height).

public void SetText (int resId)

Updates the text content of the Toast object that was generated before the Maketext () method.

Parameters

ResId the new string resource ID specified for the Toast.

public void SetText (Charsequence s)

Updates the text content of the Toast object that was generated before the Maketext () method.

Parameters

s the new text specified for the Toast.

public void Setview (view view)

Sets the view to display.

(Translator Note: Note that this method can display a custom toast view that can contain images, text, and so on.) is a more commonly used method. )

Please see

GetView ()

public void Show ()

Displays the prompt information for the specified duration.

Android toast settings to the middle of the screen, customizing the Toast implementation method, and its description

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.