Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method setView is deprecated

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);
like image 297
HasanToufiqAhamed Avatar asked Aug 08 '20 05:08

HasanToufiqAhamed


People also ask

How do I change the toast time on my Android?

There is no way to directly change the duration for which the toast is shown using the show() method without reimplementing the whole Toast class in your application, but there is a workaround. You can use a android. os. CountDownTimer to count down the time for which to display a toast.

How do you toast messages on android?

Therefore the code to make a Toast message is: Toast. makeText(getApplicationContext(), "This a toast message", Toast.


3 Answers

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.

like image 121
Xenolion Avatar answered Oct 10 '22 22:10

Xenolion


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.

like image 44
Panagiotis Bougioukos Avatar answered Oct 10 '22 21:10

Panagiotis Bougioukos


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

like image 1
Hrudhay Avatar answered Oct 10 '22 23:10

Hrudhay