“This is the second day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

🔥 Application Scenarios

Use toast.setgravity () to display the Toast prompt in the middle, as follows:

        Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0.0);
        toast.show();
Copy the code

It cannot be displayed when running on Android 12. Check Logcat as follows:

Toast: setGravity() shouldn't be called on text toasts, the values won't be used
Copy the code

You cannot call setGravity with toast. The call is invalid. Oh, look to the bull, we look at the source to find the reason

🔥 source

💥 Toast. SetGravity ()

    /** * Sets the position on the screen where Toast appears. * * Warning: Starting with Android R, this method does not work when text toast is raised for API level R or higher applications. * /
    public void setGravity(int gravity, int xOffset, int yOffset) {
        if (isSystemRenderedTextToast()) {
            Log.e(TAG, "setGravity() shouldn't be called on text toasts, the values won't be used");
        }
        mTN.mGravity = gravity;
        mTN.mX = xOffset;
        mTN.mY = yOffset;
    }
Copy the code

>=Android R(30); Invalid is invalid bai, still don’t give show, excessive.

Logcat tooltips was here, to have come, let’s take a look at isSystemRenderedTextToast () method.

💥 Toast. IsSystemRenderedTextToast ()

    /** * The Text Toast will be rendered by SystemUI, not within the application, so the application cannot bypass the background custom Toast limit. * /
    @ChangeId
    @EnabledAfter(targetSdkVersion = Build.VERSION_CODES.Q)
    private static final long CHANGE_TEXT_TOASTS_IN_THE_SYSTEM = 147798919L;
    
    private boolean isSystemRenderedTextToast(a) {
        return Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM) && mNextView == null;
    }
Copy the code

The point. Text Toast will be rendered by SystemUI, not within the application.

Clear and clear, can play like this, but your level is not enough, do not give you to play.

When things are clear, think about solutions. He said that Text Toast will be presented by SystemUI, so I will not use Text.

🔥 Toast offers methods

Take a look at the method Tast provides:

There are several ways. Let’s try it out. Look at the source code just to be safe

💥 Toast. SetView () the source code

    /** * sets the View to be displayed@deprecatedThe custom Toast view is deprecated. An application can create a standard text toast using the makeText method, * or using Snackbar */
    @Deprecated
    public void setView(View view) {
        mNextView = view;
    }
Copy the code

This one’s even worse. Get rid of it.

  • Or stick to the default Toast.

  • Or use Snackbar.

🔥 Snackbar

Snackbar is a quick pop-up message prompt similar to Toast (I just found out, haha).

Compared to Toast:

  • Only one can be displayed at a time

  • Interaction with the user

    • Set buttons on the right side to add events. According to the Design principle of Material Design, only one button will be displayed (add more, whichever comes last).
  • Provides Snackbar to display and close listening events

    • BaseTransientBottomBar.addCallback(BaseCallback)

💥 code implementation

    showMessage(findViewById(android.R.id.content), str, Snackbar.LENGTH_INDEFINITE);
  
    public static void showMessage(View view, String str, int length) {
        Snackbar snackbar = Snackbar.make(view, str, length);

        View snackbarView = snackbar.getView();
        // Center the layout
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(snackbarView.getLayoutParams().width, snackbarView.getLayoutParams().height);
        params.gravity = Gravity.CENTER;
        snackbarView.setLayoutParams(params);
        // The text is centered
        TextView message = (TextView) snackbarView.findViewById(R.id.snackbar_text);
        / / the setTextAlignment need SDK > = 17
        message.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
        message.setGravity(Gravity.CENTER);
        message.setMaxLines(1);
        snackbar.addCallback(new BaseTransientBottomBar.BaseCallback<Snackbar>() {
            @Override
            public void onDismissed(Snackbar transientBottomBar, int event) {
                super.onDismissed(transientBottomBar, event);
                / / Snackbar shut down
            }

            @Override
            public void onShown(Snackbar transientBottomBar) {
                super.onShown(transientBottomBar);
                / / display Snackbar}}); snackbar.setAction("Cancel".new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Displays a default Snackbar.
                Snackbar.make(view, "I'll go first.", BaseTransientBottomBar.LENGTH_LONG).show(); }}); snackbar.show(); }Copy the code

Three arguments to snackbar. make:

  • View: Finds the outermost View of the current window from the View and displays it at the bottom.
  • The second argument (text):
    • CharSequence
    • StringRes
  • Duration (display duration)
    • Snackbar.length_indefinite is displayed from show() until it is turned off or another Snackbar is displayed.
    • Snackbar. LENGTH_SHORT for short periods of time
    • Snackbar. LENGTH_LONG long time
    • Custom duration is in milliseconds

💥 effect

Android 12

The Android 5.1

💥 tools

If you feel setup trouble can look at the following article, and then integrate a set of suitable for their own.

One line of code to Snackbar