Android Question Query and use a class variable in Designer?

Filippo

Expert
Licensed User
Longtime User
Hi,

how can I query and use a variable from a class in Designer? Is that even possible? If so, how?

Specifically, it concerns this code here:
B4X:
Private Sub StatusBarHeight As Int
    Dim sHeight As Int
    Dim jo As JavaObject
    jo.InitializeContext
    
    Try
        Dim res As JavaObject = jo.RunMethod("getResources", Null)
        Dim resourceId As Int = res.RunMethod("getIdentifier", Array("status_bar_height", "dimen", "android"))
        
        If resourceId > 0 Then
            sHeight = res.RunMethod("getDimensionPixelSize", Array(resourceId))           
        End If
    Catch
        Log("Fehler beim Verschieben: " & LastException.Message)
    End Try
    Return sHeight
End Sub

I want to use the height of “StatusBarHeight” in the designer so that my toolbar adjusts accordingly.
 
Sounds like a job for.....
 
Upvote 0
I rarely use B4A, but certainly in B4J I've found it to be far more straight forward than it first appeared.

You'd basically just create a class with your StatusBarHeight sub and a sub with whatever you want to do with it :
[CODE lang="b4x" title="Desinger Script Claa"]'Class named DSE_SBH'

'Parameters: panel that you want to adjust
Private Sub SetPanelBasedOnSHeight(DesignerArgs As DesignerArgs)

Dim Panel As B4XView = DesignerArgs.GetViewFromArgs(0)
Panel.Top = StatusBarHeight 'for example

End Sub

Private Sub StatusBarHeight As Int

Dim sHeight As Int
Dim jo As JavaObject
jo.InitializeContext

Try

Dim res As JavaObject = jo.RunMethod("getResources", Null)
Dim resourceId As Int = res.RunMethod("getIdentifier", Array("status_bar_height", "dimen", "android"))

If resourceId > 0 Then

sHeight = res.RunMethod("getDimensionPixelSize", Array(resourceId))

End If

Catch

Log("Fehler beim Verschieben: " & LastException.Message)

End Try

Return sHeight

End Sub[/CODE]

Then call it from the layout's script:
[CODE lang="b4x" title="In layout script"]DSE_SBH.SetPanelBasedOnSHeight(Panel1)[/CODE]
1773054403253.png


Or something like that (code written on the fly)!
 
Upvote 0
I am receiving this error:
B4A Version: 13.40
Parsing code. (0.40s)
Java Version: 19
Building folders structure. (0.09s)
Compiling code. (0.39s)
Compiling layouts code. Error
Unassigned variable: 'clsstatusbar'
Error File: layToolBar.bal

Go to the designer script page to debug this error.

Designer-script:
B4X:
If ActivitySize > 6.5 Then
    pnlToolBar.Height = 64dip
Else
    If Portrait Then
        pnlToolBar.Height = 56dip
    Else
        pnlToolBar.Height = 48dip
    End If
End If

DDD.AddClass(fsToolBar)
fsToolBar.SetStatusBarHeight(pnlToolBar, pnlToolBar.Height)

Classe fsToolBar:
B4X:
Private Sub SetStatusBarHeight(DesignerArgs As DesignerArgs)
    Dim Panel As B4XView = DesignerArgs.GetViewFromArgs(0)
    Panel.Height = StatusBarHeight + DesignerArgs.GetViewFromArgs(1)
End Sub

Private Sub StatusBarHeight As Int
    Dim sHeight As Int
    Dim jo As JavaObject
    jo.InitializeContext
    
    Try
        Dim res As JavaObject = jo.RunMethod("getResources", Null)
        Dim resourceId As Int = res.RunMethod("getIdentifier", Array("status_bar_height", "dimen", "android"))
        
        If resourceId > 0 Then
            sHeight = res.RunMethod("getDimensionPixelSize", Array(resourceId))           
        End If
    Catch
        Log("Fehler beim Verschieben: " & LastException.Message)
    End Try
    Return sHeight
End Sub
 
Upvote 0
Change:
B4X:
Private Sub SetStatusBarHeight(DesignerArgs As DesignerArgs)
    Dim Panel As B4XView = DesignerArgs.GetViewFromArgs(0)
    Panel.Height = StatusBarHeight + DesignerArgs.GetViewFromArgs(1)
End Sub
To:
B4X:
Private Sub SetStatusBarHeight(DesignerArgs As DesignerArgs)
    Dim Panel As B4XView = DesignerArgs.GetViewFromArgs(0)
    Dim pnlToolBarHeight As Int = DesignerArgs.Arguments.Get(1)
    Panel.Height = StatusBarHeight + pnlToolBarHeight
End Sub
 
Upvote 0
I may be wrong, but in this case I also don't think you need the line DDD.AddClass(fsToolBar).
 
Upvote 0
If I don't use this line, I get this error when compiling:
java.lang.Exception: Sub initialize signature does not match expected signature.
public static anywheresoftware.b4a.pc.RemoteObject fg.cronomillemiglia.fstoolbar_subs_0._initialize(anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject) throws java.lang.Exception
class anywheresoftware.b4a.pc.RemoteObject, class anywheresoftware.b4a.pc.RemoteObject,
 
Upvote 0
java.lang.Exception: Sub initialize signature does not match expected signature.
What does the Initialize sub in your class look like?
Remember that the Initialize sub of Designer Script Extension classes must not include any parameter.
 
Upvote 0
Remember that the Initialize sub of Designer Script Extension classes must not include any parameter.
Now I understand that there should be no parameters in “Initialize” in the class.
Now there are no errors, but the height of the “pnlToolBar” panel is not changed.
 
Upvote 0
I'd suggets you test the code in SetStatusBarHeight , calling it from a button or something to make sure that it works separately from the Designer Script.
Maybe try calling Pane1.SetLayoutAnimated instead of directly setting the height only with Panel.Height = StatusBarHeight + pnlToolBarHeight.
 
Upvote 0
I don't think the designer script is the solution to my problem.
I need to continue editing the new panel-height in Designer, and it looks like it won't work with that.
 
Upvote 0
Fair enough. Maybe someone else who has more experience with using Designer Scripts in B4A can step in and help better than I can, or provide an alternative to DSE.
Having said that....
I need to continue editing the new panel-height in Designer, and it looks like it won't work with that.
If you're talking about this bit:
B4X:
If ActivitySize > 6.5 Then
    pnlToolBar.Height = 64dip
Else
    If Portrait Then
        pnlToolBar.Height = 56dip
    Else
        pnlToolBar.Height = 48dip
    End If
End If
... then I would think that you can do that in the SetStatusBarHeight sub too:
B4X:
'Parameters: Panel, ActivitySize, Portrait'
Private Sub SetStatusBarHeight(DesignerArgs As DesignerArgs)
    Dim pnlToolBar As B4XView = DesignerArgs.GetViewFromArgs(0)
    Dim ActivitySize As Double = DesignerArgs.Arguments.Get(1)
    Dim Portrait As Boolean = DesignerArgs.Arguments.Get(2)
   
    If ActivitySize > 6.5 Then
        pnlToolBar.Height = 64dip
    Else
           If Portrait Then
            pnlToolBar.Height = 56dip
        Else
                pnlToolBar.Height = 48dip
        End If
    End If
   
    pnlToolBar.Height = StatusBarHeight + pnlToolBar.Height
End Sub
[CODE lang="b4x" title="designer script"]DDD.AddClass(fsToolBar)
fsToolBar.SetStatusBarHeight(pnlToolBar, ActivitySize, Portrait)[/CODE]

[Again, this is untested 'guesstimate' code. Someone with a greater knowledge of B4A may need to correct it]
 
Upvote 0
Thank you very much for your efforts, but unfortunately that doesn't work either.
The variable “pnlToolBar.Height” is not modified in the Designer.
 
Upvote 0
It works! :)

My mistake was that I called the “SetStatusBarHeight” procedure from Designer in the fsTootlBar class, but you should call the procedure from Designer in the main layout.

designer script:
clsStatusBar.SetStatusBarHeight(ToolBar1)

B4X:
'Parameters: Panel
Private Sub SetStatusBarHeight(DesignerArgs As DesignerArgs)
    Dim ToolBar As B4XView = DesignerArgs.GetViewFromArgs(0)
  
    ToolBar.Height = StatusBarHeight + ToolBar.Height
    Log("ToolBar.Height=" & ToolBar.Height)
End Sub
 
Upvote 0
Back
Top