Why do toasts get truncated when app is installed on Android 12 APi 31
Asked Answered
T

2

6

The documentation says that Toasts are truncated to two lines on applications targeting Android 12 or later. The behaviour that I observe is that Toasts are truncated to two lines on applications installed on a device running Android 12 or later.

Specifically, one of my apps which was installed before my phone was updated to Android 12 does not get its toasts truncated, but if I install it on the emulator running Android 12 it gets its toasts truncated. Another app which I rebuilt and installed after my phone was updated to Android 12 gets its toasts truncated.

Update:

The situation appears actually to be more complicated: the behaviour also depends on the device and apparently also on whether it's a debug or a release build. The same app which was getting its toasts truncated displays them correctly with a release build on my phone, but truncates them with the same release build on the emulator.

Note that this is not the same question as 70307699 where the OP updated his targetSdk to 31. Both my apps have targetSdk set to less than 31.

How can I get the documented behaviour and get my toasts back?

Terrance answered 15/12, 2021 at 20:55 Comment(0)
G
8

Rather than outright deprecating toasts, the Android Platform Team is instead making them progressively less useful, which is a form of deprecation.

https://developer.android.com/reference/android/widget/Toast

The last sentence states: Starting with Android 12 (API level 31), apps targeting Android 12 or newer will have their toasts limited to two lines.

Going forward I'll personally be using Snackbars if I need a multi-line message.

val SNACKBAR_MAX_LINES = 8 // Increase maximum SnackBar line limit above 2
val snackbar = Snackbar.make(bottomNavigationView, "message", Snackbar.LENGTH_SHORT)
            (snackbar.view.findViewById(com.google.android.material.R.id.snackbar_text) as TextView)
                .run {
                    maxLines = SNACKBAR_MAX_LINES 
                }

snackbar.show()
Gable answered 27/7, 2022 at 6:8 Comment(1)
My app doesn't target API 31 or later, so its behaviour shouldn't be changed by installing it on a device with API 31. I use a lot of toasts, so it would be major surgery to change them to snackbars, and would probably make my app not work on older versions of Android.Terrance
C
0

May be this can be a work around. Try creating a custom toast message. I tried creating one with PopupWindow. The setContentView method of this class can be used to set custom layout and thereby can support multiple lines of text.

Custom toast on Android: a simple example

And if you want to make it a generic one by creating a method in Application Class of your app. Here is the code.

class ApplicationClass() : Application(), Application.ActivityLifecycleCallbacks {
    private var activityReference: Activity? = null

    override fun onCreate() {
        super.onCreate()
        registerActivityLifecycleCallbacks(this)
    }

    fun showCustomToast(toast: String) {
        try {
            activityReference?.window?.decorView?.let {
                val popupWindow = PopupWindow(it.width - 200, ViewGroup.LayoutParams.WRAP_CONTENT)
                val binding = GenericToastPopupBinding.inflate(LayoutInflater.from(this))
                binding.toastMessage.text = toast
                popupWindow.contentView = binding.root
                binding.close.setOnClickListener {
                    popupWindow.dismiss()
                }
                popupWindow.setBackgroundDrawable(
                    ContextCompat.getDrawable(
                        this,
                        R.drawable.bg_rectangle_white
                    )
                )
                popupWindow.animationStyle = android.R.style.Animation_Toast
                popupWindow.showAtLocation(it, Gravity.BOTTOM, 0, 500)
                MainScope().launch {
                    delay(5000)
                    popupWindow.dismiss()
                }
            }

        } catch (e: Exception) {
            Log.d("TAG", "showCustomToast: ")
        }
    }

    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
        activityReference = activity
    }

    override fun onActivityStarted(activity: Activity) {
        activityReference = activity
    }

    override fun onActivityResumed(activity: Activity) {
        activityReference = activity
    }

    override fun onActivityPaused(activity: Activity) {
       
    }

    override fun onActivityStopped(activity: Activity) {
       
    }

    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
     
    }

    override fun onActivityDestroyed(activity: Activity) {
      
    }
}
Conceptacle answered 2/3, 2023 at 6:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.