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
 

ferya

Member
Licensed User
Longtime User
formated code

B4X:
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
 
Upvote 0

ferya

Member
Licensed User
Longtime User
hi thedesolatesoul
this is an android resource drawable and Erel sample is for 9patch png files.
I don't know how handle this.
 
Upvote 0

ferya

Member
Licensed User
Longtime User
sorry!
please help I don't know how
because the graphic drawabel is come from android R.drawabel and I don't know how work?
and I want set that for bitmap not background. background image converts to stretched.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
sorry!
please help I don't know how
because the graphic drawabel is come from android R.drawabel and I don't know how work?
and I want set that for bitmap not background. background image converts to stretched.
That is the whole point of 9patch, that the image can stretch to the views size.
Why do you want to display a 9patch?
 
Upvote 0

ferya

Member
Licensed User
Longtime User
sorry but they are not from file.
they are R drawables from this link:
Android Drawables
I changed
B4X:
rv.SetImage("img1",BitmapDrawable1.Bitmap)
by this
B4X:
rv.SetImage("img1",object1)
as Erel said.
but got same error
(java.lang.ClassCastException: android.graphics.drawable.NinePatchDrawable cannot be cast to android.graphics.drawable.BitmapDrawable)

please see my real code bellow:

B4X:
Sub btn_Test
   Dim BitmapDrawable1 As BitmapDrawable
   Dim AndroidResources1 As AndroidResources
   Dim object1 As Object
   Dim dname As String
   dname="sym_action_call"
   object1=AndroidResources1.GetAndroidDrawable(dname)
   If object1=Null Then
      Log("Drawable NOT FOUND: "&dname)
   Else
      BitmapDrawable1=object1
   End If
   If object1 Is BitmapDrawable Then
      Log("is drawable")
      rv.SetImage("img1",BitmapDrawable1.Bitmap)
      rv.SetText("lblreport",lblr.Get(index))
      rv.UpdateWidget
   End If
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
The system drawables are files. Use this code and see by yourself:
B4X:
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("list_pressed_holo_dark", "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")

EDIT: You can see the drawables and their real name in C:\Android\android-sdk\platforms\android-nn\data\res\drawable-xxxx (replace nn by the platform version and xxxx by the screen size)
 
Last edited:
Upvote 0

corwin42

Expert
Licensed User
Longtime User
@ferya:

It's a bit confusing because your question changed. First you said that you wanted to set a NinePatchDrawable to an ImageView. Now you want to set it to a Widgets ImageView over a RemoteViews object. These are two totally different things.

At first you have to make clear from where your resource comes. For Widget ImageViews which are accessed over a RemoteView there are three possibilities to set the image:
1. as a Bitmap (directly supported by B4A)
2. as a ResourceId (possible with Reflection)
3. as a URI (possible with Reflection)

So what is exactly the source of your drawable? Do you have an id for it?
 
Last edited:
Upvote 0

ferya

Member
Licensed User
Longtime User
sorry I am newby.
I wrote a sample for my exact question here:
B4X:
#Region Module Attributes
   #FullScreen: False
   #IncludeTitle: false
   #ApplicationLabel: test AndroidDrawable
   #VersionCode: 1
   #VersionName: 
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region
Sub Process_Globals
End Sub

Sub Globals
   Dim AndroidResources1 As AndroidResources
   Dim ImageView1 As ImageView
   Dim Label1 As Label
   Dim ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Main")
   Dim AndroidDrawableNames As List
   AndroidDrawableNames=AndroidResources1.GetAndroidDrawableNames
   For i=0 To AndroidDrawableNames.Size-1
   ListView1.AddSingleLine(AndroidDrawableNames.Get(i))
   Next
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub ListView1_ItemClick(Posision As Int,Value As Object)
   Label1.Text=Value
   Dim BitmapDrawable1 As BitmapDrawable
   Dim Object1 As Object
   Object1=AndroidResources1.GetAndroidDrawable(Value)
   If Object1=Null Then
      ImageView1.Bitmap=Null
   Else
      BitmapDrawable1=Object1
      ImageView1.Bitmap=BitmapDrawable1.Bitmap
   End If
End Sub
clicking on ListView Items shows the graphic by ImageView1
But some of them shows
some of them cause this error:
java.lang.ClassCastException: android.graphics.drawable.NinePatchDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
and some of them cause this:
java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
View attachment androidR.zip
For example if you choose (arrow_down_float) from ListView it shows correctly
but (alert_dark_frame) causes error1 and
(btn_default) causes error2
 
Upvote 0

ferya

Member
Licensed User
Longtime User
Hi Erel
I did
but it is not working yet.
please correct my source file
it's attached in previous post.
I'm not perfect in programming.
sorry for wasting your time.
 
Upvote 0

ferya

Member
Licensed User
Longtime User
Hi Erel
thank you for your quick replay.
Just now I change my code to
B4X:
Sub ListView1_ItemClick(Posision As Int,Value As Object)
   Label1.Text=Value
   Dim BitmapDrawable1 As BitmapDrawable
   Dim Object1 As Object
   Object1=AndroidResources1.GetAndroidDrawable(Value)
   If Object1=Null Then
      ImageView1.Bitmap=Null
   Else
      BitmapDrawable1=Object1
      ImageView1.Background=Object1
   End If
End Sub
and run it.
when I click on first item of ListView (alert_dark_frame) I got this error:
java.lang.ClassCastException: android.graphics.drawable.NinePatchDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
What is wrong with me? http://www.b4x.com/forum/images/icons/icon9.gif
 
Upvote 0
Top