I'm thinking about doing a loop to perform some action on each view as long as the view has certain property (ex: TextSize). I know how to iterate the views in an activity, I know how to check is of certain type, but can I check for properties, no matter the type?
The idea is that I have several views on my activity, of several types, I would rather not ask for each one if it's x type or something; each time I add a new view, or change one, I have to worry about modifiying a select or something like that. I'd like to have something that iterate the views and, If the view has some property (example, textsize) I do something with it.
This code will handle the text size of all views with a TextSize property:
B4X:
For Each v As View in Panel.GetAllViewsRecursive
If v Is Label Then
Dim lbl As Label = v
lbl.TextSize = ...
Else If v Is Spinner Then
Dim s As Spinner = v
s.TextSize = ...
End If
Next
EditText, Button, CheckButton, RadioButton and other views are subclasses of Label and can be treated as labels.
For Each v As View In Panel1.GetAllViewsRecursive
If v Is Label Then
Dim lbl As Label = v
lbl.TextSize = 18
Else If v Is NiceSpinner Then
Dim s As NiceSpinner = v
s.TextSize = 18
End If
Next
B4A version: 5.50
Parsing code. (0.50s)
Compiling code. (0.87s)
Compiling layouts code. (0.21s)
Generating R file. (4.28s)
Compiling debugger engine code. (19.55s)
Compiling generated Java code. Error
B4A line: 253
Else If v Is NiceSpinner Then
javac 1.8.0_73
src\perfil3.java:729: error: cannot find symbol
if (_v.getObjectOrNull() instanceof main.java.org.angmarch.views.NiceSpinner) {
^
symbol: class java
location: class main
... being the nicespinner this. So that I was asking about the javaobject.
The question regarding the javaobject is that I don't know how to use it in order to find out if an object has or has not certaing property... or I didn't understood the try-catch suggestion.
I was tempted to answer you that there is no output because the error raises while compiling the project (the error I've pasted is from 10 minutes ago). But I've tried again and the project compiles, run and even executes normally, no exception is raised and it seems to.
The idea of using javaobject to ask for each view having or not the TextSize property is to set text sizes for each control without having to worrying about maintaining some select clause in order to add view's types.
So, even if I have to remove the nicespinner from my activity in order to work, can something like this be done? (and how should it be done in that case?)
B4X:
For Each v As View In Panel1.GetAllViewsRecursive
Dim j as JavaObject
j = v
If j.RunMethod("HasProperty", "setTextSize") = True Then
j.RunMethod("SetTextSize", 18)
End If
Next