How to configure Toast icon in Android 12?

22k Views Asked by At

Starting with Android 12, Google show a toast message with an app icon.

My application have launcher icon. Android 12 splash screen show app icon correctly.

Show toast by code

Toast.makeText(this, "Show simple toast", Toast.LENGTH_LONG).show()

compileSdkVersion/targetSdkVersion 31

android emulator Google play Intel x86 Atom_64 System Image API Level 31, Revision 8.

How can i change this default toast icon?

enter image description here

7

There are 7 best solutions below

0
On

Update: it stopped working the next day :(

In all likelihood, this is an Android 12 bug because immediately after reboot everything works. But as soon as you change the build variants (from debug to release, for example), everything stops working.

And in the logcat I get the following error messages (while I'm trying to show only one Toast, I specifically double-checked):

2023-10-23 16:12:26.166  3055-3055  ndroid.systemu          com.android.systemui                 E  Failed to open APK '/data/app/~~ydafWiMxymoRue6E25Ic_Q==/my.app.id-o52oNN4A3UebVQPR4X_ZMQ==/base.apk': I/O error
2023-10-23 16:12:26.167  3055-3055  ndroid.systemu          com.android.systemui                 E  Failed to open APK '/data/app/~~ydafWiMxymoRue6E25Ic_Q==/my.app.id-o52oNN4A3UebVQPR4X_ZMQ==/base.apk': I/O error
2023-10-23 16:12:26.168  3055-3055  ResourcesManager        com.android.systemui                 E  failed to add asset path '/data/app/~~ydafWiMxymoRue6E25Ic_Q==/my.app.id-o52oNN4A3UebVQPR4X_ZMQ==/base.apk'
                                                                                                    java.io.IOException: Failed to load asset path /data/app/~~ydafWiMxymoRue6E25Ic_Q==/my.app.id-o52oNN4A3UebVQPR4X_ZMQ==/base.apk
                                                                                                        at android.content.res.ApkAssets.nativeLoad(Native Method)
                                                                                                        at android.content.res.ApkAssets.<init>(ApkAssets.java:295)
                                                                                                        at android.content.res.ApkAssets.loadFromPath(ApkAssets.java:144)
                                                                                                        at android.app.ResourcesManager.loadApkAssets(ResourcesManager.java:472)
                                                                                                        at android.app.ResourcesManager.access$000(ResourcesManager.java:84)
                                                                                                        at android.app.ResourcesManager$ApkAssetsSupplier.load(ResourcesManager.java:186)
                                                                                                        at android.app.ResourcesManager.createAssetManager(ResourcesManager.java:559)
                                                                                                        at android.app.ResourcesManager.createResourcesImpl(ResourcesManager.java:641)
                                                                                                        at android.app.ResourcesManager.findOrCreateResourcesImplForKeyLocked(ResourcesManager.java:696)
                                                                                                        at android.app.ResourcesManager.createResources(ResourcesManager.java:1049)
                                                                                                        at android.app.ResourcesManager.getResources(ResourcesManager.java:1152)
                                                                                                        at android.app.ActivityThread.getTopLevelResources(ActivityThread.java:2453)
                                                                                                        at android.app.ApplicationPackageManager.getResourcesForApplication(ApplicationPackageManager.java:1798)
                                                                                                        at android.app.ApplicationPackageManager.getResourcesForApplication(ApplicationPackageManager.java:1784)
                                                                                                        at android.app.ApplicationPackageManager.getDrawable(ApplicationPackageManager.java:1545)
                                                                                                        at miui.content.res.IconCustomizer.getCustomizedNormalIcon(IconCustomizer.java:804)
                                                                                                        at miui.content.res.IconCustomizer.getIcon(IconCustomizer.java:780)
                                                                                                        at android.app.MiuiThemeHelper.getDrawable(MiuiThemeHelper.java:101)
                                                                                                        at android.content.res.IMiuiThemeHelperImpl.getDrawable(IMiuiThemeHelperImpl.java:39)
                                                                                                        at android.content.res.ThemeManagerStub.getDrawable(ThemeManagerStub.java:159)
                                                                                                        at android.app.ApplicationPackageManager.loadUnbadgedItemIcon(ApplicationPackageManager.java:3073)
                                                                                                        at android.content.pm.PackageItemInfo.loadUnbadgedIcon(PackageItemInfo.java:290)
                                                                                                        at com.android.systemui.toast.SystemUIToast.getBadgedIcon(SystemUIToast.java:286)
                                                                                                        at com.android.systemui.toast.SystemUIToast.inflateToastView(SystemUIToast.java:198)
                                                                                                        at com.android.systemui.toast.SystemUIToast.<init>(SystemUIToast.java:90)
                                                                                                        at com.android.systemui.toast.SystemUIToast.<init>(SystemUIToast.java:77)
                                                                                                        at com.android.systemui.toast.ToastFactory.createToast(ToastFactory.java:78)
                                                                                                        at com.android.systemui.toast.ToastUI.lambda$showToast$0(ToastUI.java:131)
                                                                                                        at com.android.systemui.toast.ToastUI.$r8$lambda$w_gPCh3F8Xxn1jN4lkQZoUci71c(Unknown Source:0)
                                                                                                        at com.android.systemui.toast.ToastUI$$ExternalSyntheticLambda0.run(Unknown Source:16)
                                                                                                        at com.android.systemui.toast.ToastUI.showToast(ToastUI.java:169)
                                                                                                        at com.android.systemui.statusbar.CommandQueue$H.handleMessage(CommandQueue.java:1494)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:210)
                                                                                                        at android.os.Looper.loop(Looper.java:299)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8319)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1038)
2023-10-23 16:12:26.168  3055-3055  ndroid.systemu          com.android.systemui                 E  Failed to open APK '/data/app/~~ydafWiMxymoRue6E25Ic_Q==/my.app.id-o52oNN4A3UebVQPR4X_ZMQ==/base.apk': I/O error
2023-10-23 16:12:26.169  3055-3055  ndroid.systemu          com.android.systemui                 E  Failed to open APK '/data/app/~~ydafWiMxymoRue6E25Ic_Q==/my.app.id-o52oNN4A3UebVQPR4X_ZMQ==/base.apk': I/O error
2023-10-23 16:12:26.170  3055-3055  ResourcesManager        com.android.systemui                 E  failed to add asset path '/data/app/~~ydafWiMxymoRue6E25Ic_Q==/my.app.id-o52oNN4A3UebVQPR4X_ZMQ==/base.apk'
                                                                                                    java.io.IOException: Failed to load asset path /data/app/~~ydafWiMxymoRue6E25Ic_Q==/my.app.id-o52oNN4A3UebVQPR4X_ZMQ==/base.apk
                                                                                                        at android.content.res.ApkAssets.nativeLoad(Native Method)
                                                                                                        at android.content.res.ApkAssets.<init>(ApkAssets.java:295)
                                                                                                        at android.content.res.ApkAssets.loadFromPath(ApkAssets.java:144)
                                                                                                        at android.app.ResourcesManager.loadApkAssets(ResourcesManager.java:472)
                                                                                                        at android.app.ResourcesManager.access$000(ResourcesManager.java:84)
                                                                                                        at android.app.ResourcesManager$ApkAssetsSupplier.load(ResourcesManager.java:186)
                                                                                                        at android.app.ResourcesManager.createAssetManager(ResourcesManager.java:559)
                                                                                                        at android.app.ResourcesManager.createResourcesImpl(ResourcesManager.java:641)
                                                                                                        at android.app.ResourcesManager.findOrCreateResourcesImplForKeyLocked(ResourcesManager.java:696)
                                                                                                        at android.app.ResourcesManager.createResources(ResourcesManager.java:1049)
                                                                                                        at android.app.ResourcesManager.getResources(ResourcesManager.java:1152)
                                                                                                        at android.app.ActivityThread.getTopLevelResources(ActivityThread.java:2453)
                                                                                                        at android.app.ApplicationPackageManager.getResourcesForApplication(ApplicationPackageManager.java:1798)
                                                                                                        at android.app.ApplicationPackageManager.getResourcesForApplication(ApplicationPackageManager.java:1784)
                                                                                                        at android.app.ApplicationPackageManager.getDrawable(ApplicationPackageManager.java:1545)
                                                                                                        at android.app.ApplicationPackageManager.loadUnbadgedItemIcon(ApplicationPackageManager.java:3088)
                                                                                                        at android.content.pm.PackageItemInfo.loadUnbadgedIcon(PackageItemInfo.java:290)
                                                                                                        at com.android.systemui.toast.SystemUIToast.getBadgedIcon(SystemUIToast.java:286)
                                                                                                        at com.android.systemui.toast.SystemUIToast.inflateToastView(SystemUIToast.java:198)
                                                                                                        at com.android.systemui.toast.SystemUIToast.<init>(SystemUIToast.java:90)
                                                                                                        at com.android.systemui.toast.SystemUIToast.<init>(SystemUIToast.java:77)
                                                                                                        at com.android.systemui.toast.ToastFactory.createToast(ToastFactory.java:78)
                                                                                                        at com.android.systemui.toast.ToastUI.lambda$showToast$0(ToastUI.java:131)
                                                                                                        at com.android.systemui.toast.ToastUI.$r8$lambda$w_gPCh3F8Xxn1jN4lkQZoUci71c(Unknown Source:0)
                                                                                                        at com.android.systemui.toast.ToastUI$$ExternalSyntheticLambda0.run(Unknown Source:16)
                                                                                                        at com.android.systemui.toast.ToastUI.showToast(ToastUI.java:169)
                                                                                                        at com.android.systemui.statusbar.CommandQueue$H.handleMessage(CommandQueue.java:1494)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:210)
                                                                                                        at android.os.Looper.loop(Looper.java:299)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8319)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)                           

Original answer

This combination worked for me:

1. Add android:roundIcon to manifest.

<application
...
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round">

Round icon can be generated in Android Studio: Image Asset -> Launcher Icons (Adaptive and Legacy) -> Options -> Round Icon (API = 25) -> Generate: Yes

Also, if it's important, my icon asset is in PNG format.

2. Reboot the device.

After this, my app icon began to appear in Toast messages.

POCO X3 NFC, MIUI Global 14.0.2, Android 12.

0
On

The icon displayed in the toast message is the icon of the app. To change it you need to change the icon of your app. There is no documented way to have the text only without the icon.

0
On
public void CustomToast(Context context,String text,int ico,int duration)
{
    //define new Toast Object
    Toast toast = new Toast(context.getApplicationContext());
    //create TextView Object to set icon for it
    TextView tv_toast=new TextView(context);
    //get text from parameter for toast
    tv_toast.setText(text);
    //set background color and radius for my toast
    GradientDrawable gd = new GradientDrawable();
    gd.setColor(Color.GRAY);
    gd.setCornerRadius(20);
    gd.setStroke(1, 0xFF000000);
    //applying setting to tv
    tv_toast.setBackground(gd);
    //set icon
    tv_toast.setCompoundDrawablesWithIntrinsicBounds(ico, 0, 0, 0);
    //set duration
    toast.setDuration(duration);
    //ending- set my custom style for toast object
    toast.setView(tv_toast);
    // then show it
    toast.show();
}
0
On

After running a few tests, I found that setting <application android:icon="@drawable/ic_stat_name" ... with a drawable generated with Image Asset > Notification Icons is overriding the default toast icon.

However, this doesn't work with a drawable generated as Image Asset > Launcher Icons. So at this point, I guess something is missing in Android Studio's generation tool.

5
On

For me a simple restart of the device did the trick.

I had not restarted my test phone after the update to Android 12 at all. I experienced the same odd bug that a generic app icon was shown in Toasts issued by my app. I tried changing the Manifest as Mickaël‘s answer suggested. No luck. I ended up with the exact same Manifest the app had before I started debugging … and then I restarted the device. My app now displays the correct icon in Toasts reliably. What happened? Unsure. I suspect that a restart may trigger an icon cache refresh that crawls all installed apps in search for new/updated app icons.

0
On

From me the restart of the phone work as well. The toasts now show the proper App Icon, configured in my apps. If it does not work for you please review this statement and check if everything is properly set for you app icon. I hope that this will help :) https://developer.android.com/studio/write/image-asset-studio

PS: There is one more place from which you can build your app icons https://romannurik.github.io/AndroidAssetStudio/index.html

1
On

Seems like this issue was reported to Google, and assigned to a Google engineer: https://issuetracker.google.com/issues/202863198