Displaying an AppIcon in an ImageVIew

Status
Not open for further replies.

manicmonkey

Member
Licensed User
Longtime User
Probably an elementary question but I spent a couple of hours trying to figure this out.

I'm using pm.GetApplicationIcon(Main.packageName) to get the application Icon but nothing I can do will render it in my ImageView. My latest try was:

imgAppIcon.Background = pm.GetApplicationIcon(Main.packageName)

I tried initializing the ImageView first but nothing I do works.

Any pointers will be much appreciated.

Thanks in advance
manicmonkey
:sign0104:
 

Informatix

Expert
Licensed User
Longtime User
Probably an elementary question but I spent a couple of hours trying to figure this out.

I'm using pm.GetApplicationIcon(Main.packageName) to get the application Icon but nothing I can do will render it in my ImageView. My latest try was:

imgAppIcon.Background = pm.GetApplicationIcon(Main.packageName)

Do you get a good result with this code?
B4X:
Sub GetPackageName As String
    Dim r As Reflector
    Return r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
End Sub
Sub Activity_Resume
    Activity.Background = pm.GetApplicationIcon(GetPackageName)
End Sub
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
GetApplicationIcon returns a drawable not an icon, it is safer to draw it than assign it.

I use this to get a bitmap from the drawable.

B4X:
Sub GetBmpFromDrawable(d As Object) As Bitmap 
   Dim bmp As Bitmap 
   bmp.InitializeMutable(48dip,48dip)
   Dim cnv As Canvas 
   Dim dr As Rect 
   dr.Initialize(0,0,48dip,48dip)
   cnv.Initialize2(bmp)
   cnv.DrawDrawable(d,dr)
   Return cnv.Bitmap 
End Sub

Also your question lacks what *error* you are getting? What is in the logs?
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
You can try this code too:
B4X:
Dim pm As PackageManager
Dim Icons As BitmapDrawable

pPack = "com.google.android.youtube"

Icons = pm.GetApplicationIcon(pPack)
            
ImageView1.Background = Icons
 
Upvote 0

manicmonkey

Member
Licensed User
Longtime User
Do you get a good result with this code?
B4X:
Sub GetPackageName As String
    Dim r As Reflector
    Return r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
End Sub
Sub Activity_Resume
    Activity.Background = pm.GetApplicationIcon(GetPackageName)
End Sub

Yep, setting the Icon to the background works but the icon is stretched to fit the whole screen.

Activity.Background = pm.GetApplicationIcon(Main.packageName)

I was hoping to just usa an imageview to contain it.

mm.
 
Upvote 0

manicmonkey

Member
Licensed User
Longtime User
You can try this code too:
B4X:
Dim pm As PackageManager
Dim Icons As BitmapDrawable

pPack = "com.google.android.youtube"

Icons = pm.GetApplicationIcon(pPack)
            
ImageView1.Background = Icons

Thanks for the ideas. This code doesn't give any errors but the imageView just remains as a white square on the page with apparently no icon loaded.

I thought this would be simple enough but seems it is a little more tricky,

Thanks
mm.
:sign0163:
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Yep, setting the Icon to the background works but the icon is stretched to fit the whole screen.

Activity.Background = pm.GetApplicationIcon(Main.packageName)

I was hoping to just usa an imageview to contain it.

mm.

If that works, then there's no reason for not working with your ImageView. Set the gravity of your ImageView to NO_GRAVITY or CENTER to avoid stretching.
 
Upvote 0

manicmonkey

Member
Licensed User
Longtime User
If that works, then there's no reason for not working with your ImageView. Set the gravity of your ImageView to NO_GRAVITY or CENTER to avoid stretching.

I absolutely accept what you are saying but it just doesn't seem to be the case.

' imageView named imgAppIcon
imgAppIcon.Initialize("")
imgAppIcon.Background = pm.GetApplicationIcon("com.google.android.youtube")

This results in a plain white square for my imageView

I just don't get it unless I'm dooing something really stupid. I know the pm is working because when you assign it to Activity.background it works.

I just don't know. :BangHead:
 
Upvote 0

hanyelmehy

Well-Known Member
Licensed User
Longtime User
try this

Dim Icons As BitmapDrawable
Icons= PM.GetApplicationIcon(packages.Get(i))

then use
Icons.Bitmap
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
B4X:
Dim pm As PackageManager
Dim Icons As BitmapDrawable

pPack = "com.google.android.youtube"

Icons = pm.GetApplicationIcon(pPack)
          
return Icons.Bitmap

One of my users is getting an odd error on this code now, even though I've been using this code forever...

Java.lang.ClassCastException:
android.graphics.drawable.VectorDrawable cannot be cast to Android.graphics.drawable.bitmapDrawable

And ideas?

I've switched the code to:

B4X:
Sub GetPackageIcon(PackageName As String) As Bitmap
   Dim PM As PackageManager, Data As Object = PM.GetApplicationIcon(PackageName)
   If Data Is BitmapDrawable Then
       Dim Icon As BitmapDrawable = Data
       Return Icon.Bitmap
   Else
       Return GetBmpFromDrawable(Data, 48dip)
   End If
End Sub

Sub GetBmpFromDrawable(Drawable As Object, Size As Int) As Bitmap
   Dim BMP As Bitmap, BG As Canvas, Drect As Rect
   BMP.InitializeMutable(Size,Size)
   Drect.Initialize(0,0,Size,Size)
   BG.Initialize2(BMP)
   BG.DrawDrawable(Drawable,Drect)
   Return BG.Bitmap
End Sub

In hopes that it'll fix the problem.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Some Apps have Vectordrawables.
Vectordrawables is a Android 6 Feature (if i remember correctly)
 
Upvote 0
Status
Not open for further replies.
Top