Android Question [SOLVED] Application.Icon return nothing after conversion to adaptive icon

AscySoft

Active Member
Licensed User
Longtime User
Hello. I don't know if this issue was something to do with this thread https://www.b4x.com/android/forum/threads/adaptive-icons.95244/
but after I did everything as instructed in there I found out (later on) that
B4X:
Application.Icon
is always evaluated to nothing, thus app will give me an error as I try to use app icon further on as an Bitmap object, sayng that it is not initialized??!!
I don't have a small test app, I am asking how to get the app icon on oreo+ devices (in my case)?
How should I read Objects\res\mipmap\ic_launcher.png into a bitmap?
I must mention that in the res/drawable folder there is one file "icon.png" but I use that for nottifications. The two files are not the same. App icon is ofcourse "ic_launcher.png".
 

AscySoft

Active Member
Licensed User
Longtime User
I've done it using this code, thanks for the tip
B4X:
Dim pm As PackageManager
ToolbarHelper.Icon = pm.GetApplicationIcon(Application.PackageName ) 'ToolbarHelper is declare as appcompat.ACActionBar in my case
OR, if you want to resize icon (as in my case) use this
B4X:
Dim btm As Bitmap
btm.InitializeMutable(32dip,32dip)
Dim DestRect As Rect
DestRect.Initialize(0,0, 32dip,32dip)
Dim can As Canvas
can.Initialize2(btm)
Dim pm As PackageManager
can.DrawDrawable(pm.GetApplicationIcon(Application.PackageName),DestRect)
ToolbarHelper.Icon = Tools.BitmapToBitmapDrawable(btm)
'ToolbarHelper is declare as appcompat.ACActionBar in my case

Question: should I check for API and use another code under API <27?
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Application.Icon recognizes BitmapDrawable only. Meanwhile here is AdaptiveIconDrawable.
If you have simple adaptive icon (background - foreground) you can try following code (returns Bitmap)

B4X:
#If java
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.graphics.drawable.AdaptiveIconDrawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.Canvas;

public static Bitmap getAppIcon ()
    {
    try
        {               
        Drawable drawable = BA.applicationContext.getPackageManager ().getApplicationIcon (BA.packageName);
        if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap (); }
        if (Build.VERSION.SDK_INT < 26) { return null; }       
        if (drawable instanceof AdaptiveIconDrawable)
            {
            Drawable backgroundDrawable = ((AdaptiveIconDrawable) drawable).getBackground ();
            Drawable foregroundDrawable = ((AdaptiveIconDrawable) drawable).getForeground ();

            Drawable [] drawableArray = new Drawable [2];
            drawableArray [0] = backgroundDrawable;
            drawableArray [1] = foregroundDrawable;

            LayerDrawable layerDrawable = new LayerDrawable (drawableArray);

            int width = layerDrawable.getIntrinsicWidth ();
            int height = layerDrawable.getIntrinsicHeight ();
           
            Bitmap bitmap = Bitmap.createBitmap (width, height, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas (bitmap);
            layerDrawable.setBounds (0, 0, canvas.getWidth (), canvas.getHeight ());
            layerDrawable.draw (canvas);             
            return bitmap;
            }
        else
            {
            return null;
            }
        }
    catch (Exception e)
        {
        return null;
          }
    }
#End If
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
I just installed onto note3 device (android 5) an it works just fine (it bring's old icon style, non adaptive).
Just an offtopic question : Should this mean that method for Application.Icon is somehow obsolete or needs an update?

I you don't use adaptive icons just use your normal Application.Icon (icon.png). If you delete your old normal icon the from res/drawable folder you will see that it is automatically replaced by the default B4A icon and is called icon.png. If you use adaptive icons then the default B4A icon will not be used even though it had been copied to the res/drawable.

All my apps use adaptive icons, it's simple enough to use and looks good too...
 
Upvote 0
D

Deleted member 103

Guest
Application.Icon might be updated in the future to support adaptive icons.
Is there a schedule for this?
Yesterday I implemented the "adaptive icons" in an app, and now I have several crashes.
I assumed that the function Application.Icon has already been modified.
 
Upvote 0
D

Deleted member 103

Guest
Don't use this code if it gives you a crash, instead you should use code form post 3
I first found your code yesterday, thank you for that. :)
But I expected that in B4a 8.80 the bug, in the function "Application.Icon", would have been fixed.
Well, I think for Erel the mistake is not priority 1.
 
Upvote 0

AscySoft

Active Member
Licensed User
Longtime User
I think for Erel the mistake is not priority 1
This is not a mistake, but as android is evolving more and more some code that yesterday were "safe" now become somehow buggy. I do think that method for Application.Icon is somehow obsolete and needs an update. But this is not no 1 priority right now, but as android 8 become more and more distributed it will be!
 
Upvote 0
Top