Android Question how to know if a view has a certain property?

leitor79

Active Member
Licensed User
Longtime User
Hi,

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?

Regards!
 

leitor79

Active Member
Licensed User
Longtime User
Hi AndOrNot, thanks for your answer.

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.

Thank you very much!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
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.
 
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Thank you very much, Erel.

And how about controls that are not subclasses of labels? Could it be some use of the javaobject to do this?

Thank you!
 
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Hi Erel, thank you very much for your answer.

This code, for example, gives me an error...

B4X:
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.


Thank you very much!!
 
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
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.

anywheresoftware.b4a.BALayout
android.widget.TextView
android.widget.Button
android.widget.TextView
android.widget.TextView
android.widget.CheckBox
anywheresoftware.b4a.BALayout
android.widget.EditText
android.widget.ImageView
anywheresoftware.b4a.BALayout
android.widget.EditText
android.widget.ImageView
android.widget.RadioButton
android.widget.RadioButton
android.widget.RadioButton
android.widget.RadioButton
anywheresoftware.b4a.BALayout
main.java.org.angmarch.views.NiceSpinner
main.java.org.angmarch.views.NiceSpinner

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.

Thank you very much!
 
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Hi,

Today I'm getting the same error, without changing anything there. It's the same code, libraries and etc. than yesterday when I pasted the log.

2016-09-07 15_10_02.png


Regards,
 
Upvote 0

leitor79

Active Member
Licensed User
Longtime User
Here it is a sample.

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


Thank you!
 

Attachments

  • NiceSpinnerError.zip
    8.1 KB · Views: 298
Upvote 0
Top