15

While I'm doing custom toast on my app, I noticed that setView is deprecated.

Code Screenshot

Does anyone have a solution for this?

toast.setView(customView);
Vall0n
  • 1,601
  • 2
  • 14
  • 19
HasanToufiqAhamed
  • 751
  • 1
  • 5
  • 17
  • I just did some digging on the AOSP source code (well, the [Android Code Search](https://cs.android.com) website) and found the change which marked `Toast#setView` as deprecated (and even explained the rationale behind the change): https://cs.android.com/android/_/android/platform/frameworks/base/+/7b843abc1c7cd53096557909f31216b93c3ca674 – Edric Feb 14 '21 at 17:33
  • I am pretty sure that Google has rationale reasons for deprecating stuff. The alternative of using a Snackbar is not a real option. The Snackbar requires a View to be created and shown but in some cases, the view is not easy to access rather the context. – MeLean Aug 18 '21 at 06:07

4 Answers4

12

Since setView is deprecated:

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.

This makes sense Toasts can be displayed on Top of other Apps, some Apps can trick users by creating custom Toasts on Top of other Apps for their advantage even if their App is on the Background. But if your App is in the Foreground your custom Toast will still be shown in all Android Versions.

Xenolion
  • 12,035
  • 7
  • 33
  • 48
2

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

Documentation says

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.

There is a walkaround for some cases though

Toast.makeText(applicationContext,
                HtmlCompat.fromHtml("<font color='red'>custom toast message</font>", HtmlCompat.FROM_HTML_MODE_LEGACY),
                Toast.LENGTH_LONG).show()

Html color tag can also be <font color='#ff6347'>

For every modification that has to do with the displayed text, the above solution would be enough. You can for example make the text bold by inserting <b>my text</b> or you maybe want to change the font-family with <font font-family='...'> my text </font> For all those changes that solution will be enough.

If you want to modify the container though with properties like background-color the only alternative is to use Snackbar. View can not be modified for Toast anymore.

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
1

As other answers already mentioned the reasons and to use snackbar/deafult toast, I will provide the alternative I use.

We may not able to customise the toast background but we can use Spannable string to customise the text displayed in the toast. The default toast background will be shown but using different span styles available under package: android.text.style, we can achieve custom text style in the toast message.

Example custom toast which shows toast with text color in green and text size of 200 pixels.

val spannableString = SpannableString("Custom toast")
spannableString.setSpan(
    ForegroundColorSpan(Color.GREEN), 0, spannableString.length, 0
)
spannableString.setSpan(
    AbsoluteSizeSpan(200), 0, spannableString.length, 0
)
val toast = Toast.makeText(context, spannableString, Toast.LENGTH_SHORT)
toast.show()

Spannable string reference: spantastic text styling with spans

(PS: We could always show custom dialogs when app is running or custom notifications to show important messages to the users.)

Hrudhay
  • 181
  • 3
  • 9
0

I wrote a short Kotlin extension function to do this using spannable. Note that it returns a toast that you need to "show".

fun Context.spToPix(sp: Int): Int =
    (sp * Resources.getSystem().displayMetrics.scaledDensity).toInt()
fun Context.fontSizeToast(
    fontSize: Int,
    mess: String,
    displayTime: Int = Toast.LENGTH_LONG
): Toast {
    val ssMess = SpannableString(mess)
    ssMess.setSpan(
        AbsoluteSizeSpan(spToPix(fontSize)),
        0, ssMess.length, 0
    )
    return Toast.makeText(this, ssMess, displayTime)
}
steven smith
  • 1,519
  • 15
  • 31