Android Question Iterating though views and accessing their properties

MitchBu

Well-Known Member
Licensed User
Longtime User
Here is what I tried to do :
B4X:
For Each v As View In Activity.GetAllViewsRecursive
        If v Is EditText And Diko.ContainsKey(v.tag) Then
                v.TextColor = 0xFF6361FF
        End If
    Next

Of course it does not work since v is a view and not an EditText. But, would it be possible somehow to cast v to EditText so I can access the view's properties ?
 

MitchBu

Well-Known Member
Licensed User
Longtime User
As often, asking a question leads to the answer.

I finally did this :
B4X:
For Each E As EditText In Activity.GetAllViewsRecursive
        If e Is EditText Then e.TextColor = 0xFF6361FF
    Next

I test e against EditText, because Labels get fetched, and trying to set the TextColor property triggered an error.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
That's very interesting. I would have expected it to throw an error if there were any views other than EditTexts in the activity as the generated Java loop code casts each view in turn to an EditText. I wonder why that cast doesn't error for your Labels :confused:
B4X:
_e = new anywheresoftware.b4a.objects.EditTextWrapper();
...
for (; index1 < groupLen1;index1++){
_e.setObject((android.widget.EditText)(group1.Get(index1)));
 
Upvote 0

MitchBu

Well-Known Member
Licensed User
Longtime User
That's very interesting. I would have expected it to throw an error if there were any views other than EditTexts in the activity as the generated Java loop code casts each view in turn to an EditText. I wonder why that cast doesn't error for your Labels :confused:
B4X:
_e = new anywheresoftware.b4a.objects.EditTextWrapper();
...
for (; index1 < groupLen1;index1++){
_e.setObject((android.widget.EditText)(group1.Get(index1)));

You were right, agraham. It did eventually trigger an error, which seems to be random.
 
Upvote 0
Top