B4J Question [Solved] Enable/Disable controls from "Features" text file

bdunkleysmith

Active Member
Licensed User
Longtime User
I wish to have a text file named "Features" which will contain a list of the names of a variety of controls in my UI which I want to enable/disable or show/hide.

In this way I will customise the UI to suit the requirements of each user at runtime.

I thought this would be simple, but I've hit a roadblock and so how do I use the string I read from the Features file to enable or disable the control with that ID?

For example I have a comboBox:

B4X:
Private cmbPort As ComboBox

and so if I read "cmbPort" as a string from the Features file, how do I use that to disable the ComboBox control which in code would be:

B4X:
cmbPort.Enabled = False

Thanks in anticipation of any assistance.
 

DonManfred

Expert
Licensed User
Longtime User
the name of the variable is not relevant.

if you read a "cmbPort" from the featuresfile YOU know what object is needed to use. in this case it is cmbPort..
Do such relation to all entries in the featuresfile for any object you want.

You can create a map and put all "names" (cmbPort, ect) as key and the object as the value.

So you can get the right combox from the map if you get the key cmbPort.
 
Upvote 0

Chris2

Active Member
Licensed User
A couple of untested thoughts in addition to @DonManfred suggestion:

a) Use GetViewByName from the DesignerUtils library's DDD class:
B4X:
Dim cmbPort As ComboBox = ddd.GetViewByName(pnl, "cmbPort")

b) Put the view name in its tag.
Save your 'features' file with
B4X:
File.WriteMap("cmbPort": True....)    'true for enable
Read it back with
B4X:
Dim m as Map = File.ReadMap(dir, filename)
    For Each v As B4XView In pnl.GetAllViewsRecursive
       
        If m.ContainsKey(v.Tag) Then
           
            Dim enable As Boolean = m.Get(v.Tag)
            v.Enabled=enable
           
        End If
       
    Next

You could do a simlar thing with @DonManfred suggestion of a map containing the view name and object.
 
Last edited:
Upvote 0

bdunkleysmith

Active Member
Licensed User
Longtime User
Thanks @DonManfred & @Chris2 for your responses.

Unfortunately this is a large legacy project with the forms created in SceneBuilder and so rather than the views having a "tag" they have an "id". So I can't use your code directly @Chris2.

But it formed the basis of this code which does the job:

B4X:
Sub loadLicencedFeatures
    Dim m As Map = File.ReadMap(PublicApplicationDataFolder, "Features")
    For Each v As B4XView In MainForm.RootPane.GetAllViewsRecursive
        Dim joRect As JavaObject = v
        Dim id As String = joRect.RunMethod("getId",Null)
        If m.ContainsKey(id) Then
            Dim es As String = m.Get(id)
            Dim eb As Boolean
            If es = "True" Then
                eb = True
            Else
                eb = False
            End If
            v.Enabled = eb
        End If
    Next
End Sub

The map can only hold a pair of strings and so I had to add the code to convert the string to boolean.

Thanks again for you help.
 
Upvote 0
Top