Android 12: As of targetSdkVersion 31, the toast messages are clipped
Asked Answered
S

0

8

Recently my smartphone (Google Pixel 3) got an update from Android 11 to 12. Afterwards, I wanted to adapt my app Sensor Recording accordingly. So I made some changes in "build.gradle":

compileSdkVersion 31    // 30 before
defaultConfig {
    targetSdkVersion 31    // 30 before
    …
}
dependencies {
    implementation 'androidx.appcompat:appcompat:1.4.0'         // 1.3.1 before
    …
}

Everything was working correctly. However, I detected that the Toast messages were different now: They include a useless icon, and – even worse – the text message was truncated.

Before – with SdkVersion 30

enter image description here

And after – with SdkVersion 31

enter image description here

Both Toast are generated with the same code. But in the new version, the important parts of the text are missing. How can I get back the old Toast behaviour?

Susquehanna answered 10/12, 2021 at 16:36 Comment(6)
From the docs: If your app targets Android 12 (API level 31) or higher, toast is limited to two lines of text and shows the application icon next to the text. Be aware that the line length of this text varies by screen size, so it's good to make the text as short as possible. You can use Snackbar.Major
I get a lot of stack traces on Toast messages in Android 12 especially when there are many. However, the Toast still displays (as described above).Liederkranz
Did you find solution? please share here.Damnation
Not really! But as a workaround, I have replaced the "Toast" calls with invocations of "Snackbar", as recommended above by DarShan.Susquehanna
You'll find a some code to make snackbars multi-line in my answer here #70370686Bedstraw
This is the expected behavior's of the native Toast message Android 12 onwards as @Darshan has mentioned. To get the previous Toast behaviour, you can use custom view for the Toast message, Please refer this example https://mcmap.net/q/167022/-custom-toast-on-android-a-simple-exampleGarofalo

© 2022 - 2024 — McMap. All rights reserved.