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.
The soon to be released versions of B4A, B4i and B4J include a new feature named: designer script extensions. The new feature allows calling B4X code from the visual designer scripts. Note that the B4X code is not executed at design time. I believe that over time this new feature, with a...
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 :
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
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
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
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,
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.
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.
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.
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.
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....
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
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.
'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