AndroidResource 9patch problem

ferya

Member
Licensed User
Longtime User
I try to use android drawable resource.
the code bellow works with all resources except 9patch drawables
how could solve this problem?

Dim BitmapDrawable1 As BitmapDrawable
Dim DrawableName As String
Dim Object1 As Object
DrawableName="gallery_selected_default"
Object1=AndroidResources1.GetAndroidDrawable(DrawableName)
If Object1=Null Then
Log("Drawable NOT FOUND: "&DrawableName)
ImageView1.Bitmap=Null
Else
BitmapDrawable1=Object1
ImageView1.Bitmap=BitmapDrawable1.Bitmap
End If
the error code is
java.lang.ClassCastException: android.graphics.drawable.NinePatchDrawable cannot be cast to android.graphics.drawable.BitmapDrawable

also I checkd with this (ImageView1.Background=Object1) but got same error
 

corwin42

Expert
Licensed User
Longtime User
Just remove all lines that contain BitmapDrawable from your code and you are fine.

AndroidResources1.GetAndroidDrawable() does not return a BitmapDrawable but its superclass Drawable. Sometimes you have luck that this is a BitmapDrawable, sometimes it is not. So you cannot assign the result of it (Object1) to a BitmapDrawable.
 
Upvote 0

ferya

Member
Licensed User
Longtime User

Thank you all guys trying help me.
I used Informatix code and now my program works:
B4X:
Sub ListView1_ItemClick(Posision As Int,Value As Object)
   Label1.Text=Value
   Dim r As Reflector
   r.Target = r.GetContext
   r.Target = r.RunMethod("getResources")
   r.Target = r.RunMethod("getSystem")
   Dim ID_PressedDrawable As Int
   ID_PressedDrawable = r.RunMethod4("getIdentifier", Array As Object(Value, "drawable", "android"), _
   Array As String("java.lang.String", "java.lang.String", "java.lang.String"))
   r.Target = r.GetContext
   r.Target = r.RunMethod("getResources")
   Dim PressedDrawable As Object = r.RunMethod2("getDrawable", ID_PressedDrawable, "java.lang.int")
   ImageView1.Background=PressedDrawable
End Sub
please advise me if I want to use it for ImageView1.bitmap instead of ImageView1.background.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…