Toast component doesn't work into Android 11

Paulo Silva 141 Reputation points
2021-07-05T11:56:03.93+00:00

When I changed my App from 10 to 11(Android) but it doen't work with Toast Component. This is my class

public class Toast_Android : IToast
 {
 public void Show(string message)
 {
 //Toast.MakeText(Android.App.Application.Context, message, ToastLength.Long).Show();

            Toast toast = Toast.MakeText(Android.App.Application.Context, message, ToastLength.Long);

            //if (toast.View == null) return;

            //toast.SetGravity(Android.Views.GravityFlags., 0, 0);
            LinearLayout layout = (LinearLayout)toast.View;
            if (layout.ChildCount > 0)
            {
                TextView tv = (TextView)layout.GetChildAt(0);
                tv.SetForegroundGravity(GravityFlags.CenterVertical | GravityFlags.CenterHorizontal);
                //tv.TextAlignment = Android.Views.TextAlignment.Center;
            }
           // toast.SetGravity(GravityFlags.FillHorizontal | GravityFlags.Bottom, 0, 0);
            toast.Show();
        }
 }

I put in this line(if (toast.View == null) return;) just to compile my code, but is wrong, of course.

How can i change this code for other and doesn't break my app?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,366 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 77,256 Reputation points Microsoft Vendor
    2021-07-06T02:53:26.18+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    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)

    Since Android 11, custom toasts/ toast modifications are deprecated, that is why your app in Android 30 is not able to display custom toasts, Toast component doesn't work into Android 11

    So, I edited your code.

       using Android.App;  
       using Android.Content;  
       using Android.OS;  
       using Android.Runtime;  
       using Android.Views;  
       using Android.Widget;  
       using System;  
       using System.Collections.Generic;  
       using System.Linq;  
       using System.Text;  
         
       namespace XAndroidAds  
       {  
         
           public interface IToast  
           {  
         
         
               void Show(string message);  
           }  
         
           public class Toast_Android : IToast  
           {  
               
         
               public void Show(string message)  
               {  
                   //Toast.MakeText(Android.App.Application.Context, message, ToastLength.Long).Show();  
         
         
         
                   //if (toast.View == null) return;  
         
                   //toast.SetGravity(Android.Views.GravityFlags., 0, 0);  
         
                   Toast toast = Toast.MakeText(Android.App.Application.Context, message, ToastLength.Long);  
         
                   if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.R)  
                   {  
         
                   }  
                   else  
                   {  
                        
                       LinearLayout layout = (LinearLayout)toast.View;  
                       if (layout.ChildCount > 0)  
                       {  
                           TextView tv = (TextView)layout.GetChildAt(0);  
                           tv.SetForegroundGravity(GravityFlags.CenterVertical | GravityFlags.CenterHorizontal);  
                           //tv.TextAlignment = Android.Views.TextAlignment.Center;  
                       }  
                        
                   }  
                   toast.Show();  
         
                   // toast.SetGravity(GravityFlags.FillHorizontal | GravityFlags.Bottom, 0, 0);  
         
               }  
           }  
       }  
    

    When I use Android 11 emulator, I met a known issue(Toast.makeText is not showing in Android 11 (with SDK 30 emulator) ), you can try to use cold boot of emulator.

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.