Android 11 toast error

Using toast with pictures on Android 11, toast.getView () is null. After checking, the API has banned some methods in Android 11.

The solution is as follows:

    public static void showCenter(Context context, String text) {
        Toast toast = Toast.makeText (context, text, Toast.LENGTH_ Short);
// in the upper left corner. If you want to move to the right, set the second parameter to & gt; 0; move down and increase the third parameter; the last two parameters only have pixels
            toast.setGravity ( Gravity.CENTER , 0, 0);
            LinearLayout layout = (LinearLayout) toast.getView ();
            ImageView image = new ImageView(context);
            image.setImageResource (R. mipmap.icon_ success);
            layout.addView (image, 0);
        } ();

Read More: