Method Toast.setView is deprecated
Asked Answered
M

4

19

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);
Madore answered 8/8, 2020 at 5:57 Comment(2)
I just did some digging on the AOSP source code (well, the Android Code Search website) and found the change which marked Toast#setView as deprecated (and even explained the rationale behind the change): cs.android.com/android/_/android/platform/frameworks/base/+/…Helldiver
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.Nikko
P
13

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.

Physiognomy answered 29/8, 2020 at 8:59 Comment(4)
Yeah but it is still deprecated, meaning it will be removed sometime in the future. So what's the alternative? I don't understand why we shouldn't be able to create custom toasts when our app in in the foreground.Aetiology
Use SnackBars for that @AetiologyPhysiognomy
Snackbars and Toasts are very different things imhoKokanee
This is not a solution. Snackbars don't display the View that you construct them with unlike Toasts and they can only be shown at the bottom of the screen unlike Toasts.Mumbletypeg
I
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.

Integrity answered 3/3, 2021 at 16:55 Comment(0)
K
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.)

Kussell answered 7/4, 2021 at 22:30 Comment(0)
B
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)
}
Brim answered 8/2, 2023 at 18:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.