I just installed the Android R (API 30) image in my emulator to try my app, and it crashed when trying to set the Background color of a Toast.
Toast toast = Toast.makeText(ctxt, msg, duration); View view = toast.getView(); view.setBackgroundColor(0xFF303030); TextView tview = view.findViewById(android.R.id.message); tview.setTextColor(Color.WHITE); toast.show();
This is really strange as in Android Q (API 29) works perfectly.
My build.gradle updated for Android R (API 30)
compileSdkVersion 30 buildToolsVersion "30.0.1"
Is there a new way to do it??
If a toast message is not getting displayed when you run your android Application, 1. Check that you have used . show() method on the toast object as an initial troubleshooting step.
Display the created Toast Message using the show() method of the Toast class. The code to show the Toast message: Toast. makeText(getApplicationContext(), "This a toast message", Toast.
Custom toast views are deprecated. Apps can create a standard text toast with the makeText(android.
If a simple text message isn't enough, you can create a customized layout for your toast notification. To create a custom layout, define a View layout, in XML or in your application code, and pass the root View object to the setView (View) method. Notice that the ID of the LinearLayout element is "toast_layout".
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With