Android toast with rounded corners not working

Anonymous
2021-05-23T02:39:42.457+00:00

Hi I am implementing toast in android and iOS but on iOS devices it's working fine and display toast message with rounded corner and in Android devices everything thing is working fine but toast message rounded not display. I am attached my android code. using Android.Widget; using RecyclingApp.Droid.Services; using RecyclingApp.Services; using Android.Content; using Android.Views; using System; [assembly: Xamarin.Forms.Dependency(typeof(ToastService))] namespace RecyclingApp.Droid.Services { public class ToastService : IToastService { public void ShowToastMessage(string Message, MessageColor messageColor, ToastPosition toastPosition, ToastDuration toastDuration) { Context context = Android.App.Application.Context; Toast toast = Toast.MakeText(context, Message, toastDuration == ToastDuration.Long ? ToastLength.Long : ToastLength.Short); switch (toastPosition) { case ToastPosition.Top: toast.SetGravity(Android.Views.GravityFlags.Top, 0, 20); break; case ToastPosition.Center: toast.SetGravity(Android.Views.GravityFlags.CenterVertical, 0, 0); break; case ToastPosition.Bottom: toast.SetGravity(Android.Views.GravityFlags.Bottom, 0, 20); break; default: break; } if (MessageColor.ErrorColor == messageColor) toast.View.SetBackgroundColor(Android.Graphics.Color.Rgb(151, 51, 60)); else if (MessageColor.WarningColor == messageColor) toast.View.SetBackgroundColor(Android.Graphics.Color.Rgb(233, 161, 34)); else if (MessageColor.SucessColor == messageColor) toast.View.SetBackgroundColor(Android.Graphics.Color.Rgb(124, 188, 125)); toast.View.FindViewById<TextView>(Android.Resource.Id.Message).SetTextColor(new Android.Graphics.Color(255, 255, 255)); //Android.Graphics.Drawables.GradientDrawable drawable = new Android.Graphics.Drawables.GradientDrawable(); //drawable.SetCornerRadius(10); //toast.View.SetBackgroundDrawable(drawable); toast.Show(); } } } ![98883-image.png][1] [1]: /api/attachments/98883-image.png?platform=QnA

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. JessieZhang-MSFT 7,716 Reputation points Microsoft External Staff
    2021-05-24T05:47:18.677+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    From android document Toast, we can see:

    This method was deprecated in API level 30.
    Custom toast views are deprecated. Apps can create a standard text toast with the makeText(android.content.Context, java.lang.CharSequence, int) method, or use a Snackbar when in the foreground. Starting from Android Build.VERSION_CODES#R, apps targeting API level Build.VERSION_CODES#R or higher that are in the background will not have custom toast views displayed.

    So the solution with setting a custom view on Toast is deprecated for API 30 and forward.

    You can use SnackBar from Xamarin Community toolkit package, which uses native implementation in platforms where natively supported, because Toast is deprecated in API level 30, a SnackBar without an Action is equivalent to a Toast.

    Started with the Xamarin Community Toolkit

    1. Install the Package on all your projects
    2. include the namespace using Xamarin.CommunityToolkit.Extensions;
    3. In your page code-behind show a SnackBar upon an event await this.DisplayToastAsync("This is a Toast Message");
      await this.DisplayToastAsync("This is a Toast Message for 5 seconds", 5000);

    You may specify a duration for the SnackBar to disappear (in milliseconds) or leave the default one which equals 3 seconds.
    98995-image.png

    Note:

    Resources

    SnackBar Sample is here: https://github.com/xamarin/XamarinCommunityToolkit/blob/main/samples/XCT.Sample/Pages/Views/SnackBarPage.xaml.cs

    Official Repo: https://github.com/xamarin/XamarinCommunityToolkit

    Official Docs: https://learn.microsoft.com/en-us/xamarin/community-toolkit/

    Best Regards,

    Jessie Zhang

    ---
    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.


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.