SoFunction
Updated on 2025-04-09

Summary of the method of displaying messages in Android development

This article summarizes the method of Toast displaying messages in Android development. Share it for your reference, as follows:

Android provides a simple Toast message prompt box mechanism, which can prompt the user to have some information after the user clicks certain buttons. The prompt information cannot be clicked by the user. The Toast prompt information will automatically disappear according to the display time set by the user. Toast's prompt message can easily display certain things you want to display when debugging the program.

Two ways to create a Toast

Java code for the first method:

makeText(Context context, int resId, int duration)

Parameters: context is the context in which toast is displayed, usually the current activity; resId refers to the data that the display content refers to Resouce, which is to specify the displayed message content from the R class; duration specifies the display time, Toast has two constants LENGTH_SHORT and LENGTH_LONG by default, representing short-term display and long-term display respectively.

Java code for the second method:

makeText(Context context, CharSequence text, int duration)

The parameters context and duration are the same as the first method, and the parameters text can write the message content by itself.

Use any of the above methods to create the Toast object and call the method show() to display it.

Toast toast = (, "This is a normal Toast!", Toast.LENGTH_SHORT);
();

Set the Toast display position

There are two methods to set the display position:

Method 1:

setGravity(int gravity, int xOffset, int yOffset)

The three parameters are respectively represented (starting point position, horizontally shift to the right, vertically shifted down)

Method 2:

setMargin(float horizontalMargin, float verticalMargin)

Set the display position as the percentages of horizontal and vertical directions. Both parameters are float type (horizontal displacement positive right and negative left, vertical displacement positive up and negative down)

// Set the Toast display position (start position, horizontally shift to the right, vertically shifted down)( | , 0, 200);
// Toast displays the position, calculated as the percentages of horizontal and vertical directions. Both parameters are float type (horizontal displacement positive right and negative left, vertical displacement positive up and negative down)(-0.5f, 0f);

Custom Toast

The following code can display a Toast effect with an image:

// Toast with picturesButton btn2 = (Button) findViewById(.toast2);
(new OnClickListener() {
  public void onClick(View v) {
    // Define a Toast    Toast toast = (, "This is a Toast of a generation of pictures!", Toast.LENGTH_LONG);
    // Define an ImageView    ImageView imageView = new ImageView();
    ();
    // Get the View of Toast    View toastView = ();
    // Define a Layout, here is Layout    LinearLayoutlinear Layout = new LinearLayout();
    ();
    // Merge ImageView and ToastView into Layout    (imageView);
    (toastView);
    // Replace the original ToastView    (linearLayout);
    ();
  }
});

For more information about Android related content, please check out the topic of this site:Android programming activity operation skills summary》、《Android resource operation skills summary》、《Android file operation skills summary》、《Summary of Android's SQLite database skills》、《Summary of Android operating json format data skills》、《Android database operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android development introduction and advanced tutorial》、《Android View View Tips Summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.