Home > Mobile >  Custom Toast in Jetpack Compose
Custom Toast in Jetpack Compose

Time:11-03

I want an custom Toast in Jetpack Compose but didn't found any helpful documentation regarding this any help will be appreciated

CodePudding user response:

Until version 30(R), setView method could be used. As stated here now either use the standard Toast or go to Snackbar

Toast -> setView

/**
 * Set the view to show.
 *
 * @see #getView
 * @deprecated Custom toast views are deprecated. Apps can create a standard text toast with the
 *      {@link #makeText(Context, CharSequence, int)} method, or use a
 *      <a href="{@docRoot}reference/com/google/android/material/snackbar/Snackbar">Snackbar</a>
 *      when in the foreground. Starting from Android {@link Build.VERSION_CODES#R}, apps
 *      targeting API level {@link Build.VERSION_CODES#R} or higher that are in the background
 *      will not have custom toast views displayed.
 */
@Deprecated
public void setView(View view) {
    mNextView = view;
}

CodePudding user response:

Custom Toasts are deprecated in favor of SnackBar.
So they mostly won't have support in Jetpack Compose.

References.
StackOverflow Post - Custom Toasts are deprecated.
Android code change deprecating Custom Toast with explaination

Deprecating custom toasts as discussed, the reasons are:

  • We're blocking background custom toasts for security reasons (go/toast-abuse).
  • This means custom toasts are only possible if the app is in the foreground. In the foreground the app has control over its own view hierarchy and is capable of creating any visual elements it would otherwise use custom toasts for.
  • If we were to declare ongoing support for foreground toasts the developer would be in a situation where they either check for foreground status of the app before posting a custom toast or they accept that the information they want to display may not be shown at all. This is not great.
  • There is also a desire to avoid custom toasts altogether since they hurt UX consistency (https://docs.google.com/presentation/d/1r5WEofZ_G3B9M65nS37uD4RqA4iV9HUmngyE6ZpBSsw/edit#slide=id.g7b69852da2_0_0).

Also added a recommendation to use Snackbars while the app is in the foreground.

Toast Abuse - Attack using Android Toast

  • Related