B4J Question [SOLVED] Access CustomView Properties Recursive

rwblinn

Well-Known Member
Licensed User
Longtime User
Seeking for a way to set customview properties for a number of the same customviews in a loop via code.
Example: Defined the 3 customviews
B4X:
Private ThermostatMakeLab As xInstrumentationController
Private ThermostatWohnzimmer1 As xInstrumentationController
Private ThermostatWohnzimmer2 As xInstrumentationController
Instead of setting for each
B4X:
Dim TEXTSIZE as Int = 12
ThermostatMakeLab.TextSize =TEXTSIZE
ThermostatWohnzimmer1.TextSize = TEXTSIZE
ThermostatWohnzimmer2.TextSize = TEXTSIZE
want to loop over the customviews (with property Tag set) and set the textsize.
Each customview is a pane with properties.
Tried like below snippet but cast error (basically expected but do not know how to access the customview; note mBase is public)
B4X:
    For Each v As B4XView In MainPage.Root.GetAllViewsRecursive
        If v Is Pane and v.Tag > 0 Then
          'GetView(0) is Canvas, GetView(1) is Pane
          Dim ic as xInstrumentationController = v.GetView(1).As(xInstrumentationController)
          ic.TextSize = TEXTSIZE
        End If
    Next
 

stevel05

Expert
Licensed User
Longtime User
The convention is to store the instance of the customview in the base tag property. The code is already part of the Customview template for XUI Customviews.

B4X:
'Base type must be Object
Public Sub DesignerCreateView (Base As Object, Lbl As Label, Props As Map)
    mBase = Base
    Tag = mBase.Tag
    mBase.Tag = Me  '<--- Instance assigned to Base.Tag

Changing the line to
B4X:
 Dim ic as xInstrumentationController = v.GetView(1).Tag

should then do the trick.

If you are not using an XUI custom view, you can do the same yourself saving the Tag passed from the designer in a new property. See the XUI Customview template for full code.

Alternatively, you could just create a list or temporary array of the Customviews and iterate over that to change the properties.
 
Last edited:
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Alternatively, you could just create a list or temporary array of the Customviews and iterate over that to change the properties.

Thanks for the options. Decided to go for the list solution as easier to manage:
B4X:
Private devices As List
devices.Initialize
devices.AddAll(Array(ThermostatMakeLab,ThermostatWohnzimmer1,ThermostatWohnzimmer2,ThermostatFlur,ThermostatBad,ThermostatDusche,ThermostatEsszimmer))
For Each Instrument As xInstrumentationController In devices
    Instrument.TextSize = 10
    Instrument.TextColor = xui.Color_Blue
Next

Applied to an example of the new B4XCustom View xInstrumentController embedded in B4X Library xInstrumentControls.
Live data from my home HomematicIP thermostats. Green bar=present value, black indicator=setpoint. Red dot=Turn thermostat off, Green dot=Thermostat 23°C.
1637241739253.png
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You could also have done it that way:

B4X:
Private Instruments() As xInstrumentationController
Instruments = Array(ThermostatMakeLab,ThermostatWohnzimmer1,ThermostatWohnzimmer2,ThermostatFlur,ThermostatBad,ThermostatDusche,ThermostatEsszimmer)
For i = 0 To Instruments.Size - 1
    Instrument.TextSize = 10
    Instrument.TextColor = xui.Color_Blue
Next
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
You could also have done it that way:
Thanks for Array example.

Tested this code:
B4X:
Private Instruments() As xInstrumentationController
Instruments = Array As xInstrumentationController(ThermostatMakeLab,ThermostatWohnzimmer1,ThermostatWohnzimmer2,ThermostatFlur,ThermostatBad,ThermostatDusche,ThermostatEsszimmer)
For i = 0 To Instruments.Length - 1
    Instruments(i).TextSize = 10
    Instruments(i).TextColor = xui.Color_Blue
Next

OR with For Each

For Each Instrument As xInstrumentationController In Instruments
    Instrument.TextSize = 10
    Instrument.TextColor = xui.Color_Blue
Next
 
Upvote 0
Top