question

$$ANON_USER$$ avatar image
0 Votes"
$$ANON_USER$$ asked $$ANON_USER$$ commented

Android toast with rounded corners not working

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]: /answers/storage/attachments/98883-image.png

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered $$ANON_USER$$ commented

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://docs.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.







image.png (12.9 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @JessieZhang-2116

as per suggested by you , I am using Xamarin Community Toolkit Toast message but how to set corner for this

0 Votes 0 ·

Hi @JessieZhang-2116 I want Toast message like this. I to modify or implement same functionality on App. ![99514-image.png][1] [1]: /answers/storage/attachments/99514-image.png

0 Votes 0 ·