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.