Android Question Beta 3.8 JavaObject CreateEvent / CreateEventFromUI

stevel05

Expert
Licensed User
Longtime User
I am trying to play with the JavaObject CreateEvent, it seems that there is a parameter missing from the signature. The help has an extra parameter: TargetMethod which can't be entered.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The description is incorrect. It will be fixed. There is no TargetMethod parameter.

Example of RatingBar with OnRatingBarChangeListener:
B4X:
Sub Globals
   Dim rbar As View
End Sub

Sub Activity_Create(FirstTime As Boolean)
   rbar = CreateRatingBar("rbar")
   Activity.AddView(rbar, 10dip, 10dip, -2, 100dip) 'width must be set to -2 (WRAP_CONTENT)
End Sub

Sub CreateRatingBar (EventName As String) As Object
   Dim context As Object = GetBA.GetField("context")
   Dim jo As JavaObject
   jo.InitializeNewInstance("android.widget.RatingBar", Array (context))
   jo.RunMethod("setNumStars", Array(5))
   Dim event As Object = jo.CreateEventFromUI("android.widget.RatingBar.OnRatingBarChangeListener", _
     EventName, Null)
   jo.RunMethod("setOnRatingBarChangeListener", Array(event))
   Return jo
End Sub

Sub rbar_Event (MethodName As String, Args() As Object) As Object
   Dim rating As Float = args(1)
   Dim FromUser As Boolean = Args(2) 'ignore
   Activity.Title = rating
   Return Null
End Sub

Sub GetBA As JavaObject
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetFieldJO("processBA")
End Sub

upload_2014-5-11_8-58-4.png
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
How can I change the color of the star in the above example? Currently it is based on the Android OS version (which is blue most of the time). I would like to set it to yellow, which is similar to the gold star ratings we normally see on sites.

There is an explanation here

http://stackoverflow.com/questions/2446270/android-ratingbar-change-star-colors/6008722#6008722

I created the xml and other asset files mentioned above and then I tried to implement it using this code
B4X:
jo.RunMethod("setProgressDrawable",Array As Object(xml.GetDrawable("ratingbar_yellow")))
It runs without errors and I can see the new star color as well, but instead of showing five 1 stars, I am getting a single star (which is the full star) that is stretched across the ratingbar. It feels like background property is getting set with the value of the progress star.

What could be wrong?
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
A test project with red stars, is attached.

And here is the output I got.

L790Oly.png
 

Attachments

  • ratingbartest.zip
    15.8 KB · Views: 294
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Seems like it is not simple to change the rating bar drawable programmatically. See this issue: https://code.google.com/p/android/issues/detail?id=70324#makechanges (the workaround in the second post doesn't work).

A more complex solution, which I haven't tried, is posted here: http://stackoverflow.com/questions/...-a-ratingbar-using-images-loaded-from-the-web

You can however use a different method to create the rating bar. Define it in a XML file and then load it with XmlLayoutBuilder. You can then use similar code to add the event and access its methods.
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
Thanks Erel. I tried to follow your suggestion. I created the XML file and could load it correctly into an Activity using LoadXmlLayout. But I want to add it to a panel that already has other views. How can I do that? One way I can think of is to load it into an empty panel and then add that panel to the main panel.

And when I tried to position the ratingbar via code using SetLayout, I got an error
B4X:
java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to anywheresoftware.b4a.BALayout$LayoutParams

I suppose if I load it to an empty panel, I can position that panel instead of the ratinbar. Is this the only possible way?

Basically I would like to load the ratingbar via XML, just to change the star color. The rest of the stuff, like adding to a panel, positioning, setting rating value etc must be done via code only. I believe I can do most of the stuff via code using JavaObjects. I just want to "import" a ratingbar with a custom star color into B4A, that's all.
 
Upvote 0
Top