Easy Tutorial
❮ 15 Php Mind Mapping Verilog Dividend ❯

2.5.7 Basic Usage of Toast (Toast)

Category Android Basic Tutorial

Introduction to This Section:

>

Alright, we've finally finished learning about some of the controls related to the Adapter class. Of course, in addition to the ones explained, there are many other related controls, but we won't go through them slowly one by one. If needed, please consult the documentation for the relevant usage. What this section brings is a control used for notification messages in Android - Toast (Toast)! Toast is a very convenient message prompt box that will display a message prompt box in the middle of the screen, without any buttons, and will not gain focus and automatically disappear after a while! It is very commonly used! In this section, we will learn about the use of Toast!

1. Directly call the makeText() method of the Toast class to create

This is the most common form we use! For example, when you click a button, then pop up a Toast, the usage is: Toast.makeText(MainActivity.this, "Prompt content", Toast.LENGTH_LONG).show(); The first one is the context object! The second one is the content to be displayed! The third one is the display time, only LONG and SHORT will take effect, even if you define other values, the last one called will still be these two!

In addition, since Toast is very commonly used, we can extract these common parts and write them into a method! When you need to display Toast, you can directly call this method to display Toast, which is much more convenient! The example is as follows:

void midToast(String str, int showTime)
{
    Toast toast = Toast.makeText(global_context, str, showTime);            
    toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL , 0, 0);  //Set the display position
    TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
    v.setTextColor(Color.YELLOW);     //Set the font color
    toast.show();   
}

The extracted method above, we find that we can call setGravity to set the display position of Toast and obtain the displayed text by findViewById(android.R.id.message), and then set the color, or size, etc.! This is the second way to customize Toast through the constructor method!


2. Customize Toast through the Constructor Method:

The text and display position above have been customized, and below we will write two simple examples:

1. Define a Toast with a picture

Effect Picture :

Key Code :

private void midToast(String str, int showTime)
{
    Toast toast = Toast.makeText(mContext, str, showTime);
    toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM , 0, 0);  //Set the display position
    LinearLayout layout = (LinearLayout) toast.getView();
    layout.setBackgroundColor(Color.BLUE);
    ImageView image = new ImageView(this);
    image.setImageResource(R.mipmap.ic_icon_qitao);
    layout.addView(image, 0);
    TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
    v.setTextColor(Color.YELLOW);     //Set the font color
    toast.show();
}

2. Fully customize Toast

>

If the above method is still not satisfactory, you can completely write your own Toast layout and display it; But the time we still can't control!

Running Effect Picture :

Key Code :

private void midToast(String str, int showTime)
{
    LayoutInflater inflater = getLayoutInflater();
    View view = inflater.inflate(R.layout.view_toast_custom,
            (ViewGroup) findViewById(R.id.lly_toast));
    ImageView img_logo = (ImageView) view.findViewById(R.id.img_logo);
    TextView tv_msg = (TextView) view.findViewById(R.id.tv_msg);
    tv_msg.setText(str);
    Toast toast = new Toast(mContext);
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(view);
    toast.show();
}

There is also a custom Toast layout and rounded corner background:

Rounded corner background: bg_toast.xml :

``` <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Set transparent background color --> <solid android:color="#BADB66" /> <!-- Set a black border --> <stroke android:width="1px" android:color="#FFFFFF" /> <!-- Set the radius of the four corners --> <corners android:bottomLeftRadius="50px" android:bottomRightRadius="50px" android:topLeftRadius="50px" android:topRightRadius

❮ 15 Php Mind Mapping Verilog Dividend ❯