Android Question Tabstrip Labels with Badger possible?

fredo

Well-Known Member
Licensed User
Longtime User
Anything I can do to overcome the ClassCastException?
(Testproject attached)

B4X:
Sub Activity_Create(FirstTime As Boolean)
    
    '### '### '### '### '###
    badger1.Initialize
    '### '### '### '### '###
    
    Activity.LoadLayout("Main")
    TabStrip1.LoadLayout("Page1", "PAGE 1")
    TabStrip1.LoadLayout("Page2", "THIS IS PAGE 2")
    TabStrip1.LoadLayout("Page3", "AND PAGE 3")
    For i = 1 To 100
        Page3ListView1.AddSingleLine($"Item ${i}"$)
    Next
    Activity.AddMenuItem("Jump to page 1", "mnu1")
    Activity.AddMenuItem("Jump to page 2", "mnu2")
    Activity.AddMenuItem("Jump to page 3", "mnu3")
    
    
    
    '### '### '### '### '###
    For Each lbl As Label In GetAllTabLabels(TabStrip1)
        badger1.SetBadge(lbl, Rnd(1,20) )   ' <------- ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to anywheresoftware.b4a.BALayout$LayoutParams
    Next
    '### '### '### '### '###

throws this:

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
badger_createnewpanel (java line: 110)
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to anywheresoftware.b4a.BALayout$LayoutParams
    at anywheresoftware.b4a.objects.ViewWrapper.getLeft(ViewWrapper.java:162)
    at anywheresoftware.b4a.objects.B4XViewWrapper.getLeft(B4XViewWrapper.java:106)
    at b4a.example.badger._createnewpanel(badger.java:110)
    at b4a.example.badger._setbadge(badger.java:280)
    at b4a.example.main._activity_create(main.java:378)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
    at b4a.example.main.afterFirstLayout(main.java:104)
    at b4a.example.main.access$000(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:82)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 

Attachments

  • TabstripWithBadger.zip
    12.4 KB · Views: 92

Erel

B4X founder
Staff member
Licensed User
Longtime User
SS-2018-05-23_08.49.10.png


See the attached project.

The interesting code is:
B4X:
For Each lbl As B4XView In GetAllTabLabels(TabStrip1)
   Dim Width, Height As Int
   Dim jo As JavaObject = lbl
   Do While True
       Width = jo.RunMethod("getMeasuredWidth", Null)
       Height = jo.RunMethod("getMeasuredHeight", Null)
       If Width > 0 And Height > 0 Then
           Exit
       End If
       Sleep(50)
   Loop
   Dim p As Panel = xui.CreatePanel("")
   p.Tag = lbl
   lbl.Parent.AddView(p, 0, 0, Width, Height)
   lbl.RemoveViewFromParent
   p.AddView(lbl, 0, 0, Width, Height)
Next
It waits for the labels to be measured and then adds panels with the correct size as the labels parents. From this point you can treat the labels like any other views.
 

Attachments

  • TabstripWithBadger.zip
    12.5 KB · Views: 137
Upvote 0
Top