Toast.getView() returns null on Android 11 (API 30)

You can check before to custumized toast

    Toast toast = Toast.makeText(ctxt, msg, duration);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
        View view = toast.getView();
        view.setBackgroundColor(0xFF303030);
        TextView tview = view.findViewById(android.R.id.message);
        tview.setTextColor(Color.WHITE);
    }
    toast.show();

Since Android 11, custom toasts/ toast modifications are deprecated, according to Google to "protect users". Hence why your app in Android 30 is not able to display custom toasts.

From Android Developers documentation:

Custom toast views are deprecated. Apps can create a standard text toast with the makeText(android.content.Context, java.lang.CharSequence, int)


@pvalle & @Aayush Panda, It works for me in Android 11. Please check below code

public static void showCenterToastMessage(Context context, String msg) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View layout = inflater.inflate(R.layout.custom_toast,null);
        TextView text = (TextView) layout.findViewById(R.id.text);
        text.setText(msg);
        text.setPadding(20,0,20,0);
        text.setTextSize(18);
        text.setTextColor(Color.WHITE);
        Toast toast = new Toast(context);
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        layout.setBackgroundColor(Color.DKGRAY);
        toast.setView(layout);
        toast.show();
    }

layout is as follows

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/text"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

The only way I have found of showing custom toasts from API 30 onwards is by creating them ad hoc.

XML LAYOUT
Customize as needed

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".main_activity">



        <!--Ad hoc toast Textview-->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="12dp"
            android:layout_margin="18dp"
            android:background="@drawable/ad_hoc_toast_background"
            android:textColor="#1e1e1e"
            android:gravity="center"
            android:visibility="gone"
            android:layout_alignParentBottom="true"
            android:id="@+id/ad_hoc_toast_textview"
            tools:text="Temporary message bla bla bla ..."/>



</RelativeLayout>

TOAST BACKGROUND (ad_hoc_toast_background.xml)
Customize as needed

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape
             xmlns:android="http://schemas.android.com/apk/res/android"
             android:shape="rectangle">

        <size
                 android:width="220dp"
                 android:height="100dp"/>
        <corners
                 android:radius="25dp"
            />

        <solid
                 android:color="#e6ffffff"
            />
    </shape>
</item>
</selector>

Define the show_ad_hoc_toast() method

private void show_ad_hoc_toast(final TextView ad_hoc_toast_textview, String text){


    //Set the text
    ad_hoc_toast_textview.setText(text);


    //Create alpha animation
    AlphaAnimation animation1 = new AlphaAnimation(0f, 1f);

    //Set duration
    animation1.setDuration(300);

    //Set that the animation changes persist once the animation finishes
    animation1.setFillAfter(true);


    //Set on AnimationEnd Listner
    animation1.setAnimationListener(new Animation.AnimationListener() {

        @Override public void onAnimationStart(Animation animation){}
        @Override public void onAnimationRepeat(Animation animation){}
        @Override public void onAnimationEnd(Animation animation){

            //After 2250 millis -> hide the toast
            new CountDownTimer(2250, 1) {
                public void onTick(long millisUntilFinished){}
                public void onFinish() {hide_ad_hoc_toast(ad_hoc_toast_textview);}
            }.start();


        }

    });


    //Make the view visible
    ad_hoc_toast_textview.setVisibility(View.VISIBLE);


    //Start animation
    ad_hoc_toast_textview.startAnimation(animation1);


}

Define the hide_ad_hoc_toast() method

private void hide_ad_hoc_toast(final TextView ad_hoc_toast_textview){


    //Create alpha animation
    AlphaAnimation animation1 = new AlphaAnimation(1f, 0f);

    //Set duration
    animation1.setDuration(300);

    //Set that the animation changes persist once the animation finishes
    animation1.setFillAfter(true);


    //Set on AnimationEnd Listner
    animation1.setAnimationListener(new Animation.AnimationListener() {

        @Override public void onAnimationStart(Animation animation) { }
        @Override public void onAnimationRepeat(Animation animation) { }
        @Override public void onAnimationEnd(Animation animation) {

            //Make the view gone
            ad_hoc_toast_textview.setVisibility(View.GONE);

        }

    });



    //Start animation
    ad_hoc_toast_textview.startAnimation(animation1);



}

Call the method from your code when needed

//Find ad_hoc_toast textview
TextView ad_hoc_toast_textview = findViewById(R.id.ad_hoc_toast_textview);

//Define the text to be shown
String text = "This is the custom toast message"

//Show the ad_hoc toast
show_ad_hoc_toast(ad_hoc_toast_textview, text);  

RESULT
RESULT

Tags:

Android

Toast