GetView Is problem

Jonas

Member
Licensed User
Longtime User
Im playing with GetView and followed the exemple found in the documentation. I added 5 Labels, 1 EditText and 1 Button.

Running the following code will set the color of all the components to blue, even the EditText and Button.

B4X:
For I = 0 To Activity.NumberOfViews - 1
If Activity.GetView(I) Is Label Then      
Dim lbl As Label
lbl = Activity.GetView(I)
lbl.Color = Colors.Blue
End If

But if I use this code only the labels will be changed to blue:
B4X:
For I = 0 To Activity.NumberOfViews - 1
If Activity.GetView(I) Is Label Then

If Activity.GetView(I) Is EditText Then
Continue
End If

If Activity.GetView(I) Is Button Then
Continue
End If

Dim b As Label
b = Activity.GetView(I)
b.Color = Colors.Blue
End If
Next

Is there a bug when using "Is Label" or am I missing somehting?

/J
 

specci48

Well-Known Member
Licensed User
Longtime User
There are two problems within your code.

First, the outer If statement only gets true for labels, so no execution will be done for other types. Look at this formatted version of your code:
B4X:
For I = 0 To Activity.NumberOfViews - 1
    If Activity.GetView(I) Is Label Then
        If Activity.GetView(I) Is EditText Then
            Continue
        End If
        If Activity.GetView(I) Is Button Then
            Continue
        End If
        Dim b As Label
        b = Activity.GetView(I)
        b.Color = Colors.Blue
    End If
Next

Second, if you delete the outer If startement, e.g.
B4X:
For I = 0 To Activity.NumberOfViews - 1
    If Activity.GetView(I) Is EditText Then
        Continue
    End If
    If Activity.GetView(I) Is Button Then
        Continue
    End If
    Dim b As Label
    b = Activity.GetView(I)
    b.Color = Colors.Blue
Next
you are missing the meaning of the keyword continue.
Continue stops executing the current iteration and continues with the next one.

So if you have found an EditText this if-clause gets true
B4X:
If Activity.GetView(I) Is EditText Then
    Continue
End If
and all following statements are skipped for the current iteration
B4X:
If Activity.GetView(I) Is Button Then
    Continue
End If
Dim b As Label
b = Activity.GetView(I)
b.Color = Colors.Blue


specci48
 
Upvote 0

Jonas

Member
Licensed User
Longtime User
I think you misunderstood my question.

This code don't work:
B4X:
For I = 0 To Activity.NumberOfViews - 1
If Activity.GetView(I) Is Label Then
Dim lbl As Label
lbl = Activity.GetView(I)
lbl.Color = Colors.Blue
End If

If the "If Activity.GetView(I) Is Label Then" finds eg Edittext it will execute the code anyway. Meaning I cant separate Labels from other views.

But if it was EditText I was looking for and changed to "If Activity.GetView(I) Is EditText Then" then if the view was a Label it wouldn't execute the code.
The problem only appears with Labels.

I added the extra "If Activity.GetView(I) Is EditText Then" and "If Activity.GetView(I) Is Button Then" in the second code just as a workaround because the "If Activity.GetView(I) Is Label Then" is always running its code without regarding type of view.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
The problem with Label is that it's not known as a Label in Android but as a TextView.
EditTexts and Buttons are subsets of TextView.

The routine below will only find Labels.
B4X:
For i = 0 To Activity.NumberOfViews - 1
   If Activity.GetView(i) Is Label Then
     Log(i & " Label")
   Else If Activity.GetView(i) Is Button Then
     Log(i & " Button")
   Else If Activity.GetView(i) Is EditText Then
     Log(i & " EditText")
   End If        
Next
This routine shows the correct views:
B4X:
For i = 0 To Activity.NumberOfViews - 1
   If Activity.GetView(i) Is Button Then
     Log(i & " Button")
   Else If Activity.GetView(i) Is EditText Then
     Log(i & " EditText")
   Else If Activity.GetView(i) Is Label Then
     Log(i & " Label")
   End If        
 Next
You could also use a routine like this:
B4X:
For i = 0 To Activity.NumberOfViews - 1
   Dim View1 As View
   View1 = Activity.GetView(i)
   Log(GetType(View1))
   Select GetType(View1)
   Case "android.widget.TextView"
     Log(i & " Label")
   Case "android.widget.EditText"
     Log(i & " EditText")
   Case "android.widget.Button"
     Log(i & " Button")
   End Select
 Next
@Erel, could the behaviour of the first routine be fixed ?

Best regards.
 
Upvote 0

Jonas

Member
Licensed User
Longtime User
I used Klaus third exemple and worked good for me. But "Is Label" has lost its purpose if it cant catch a Label. At least it should be noted in the documentation.

Thanks for getting me in the right direction.
 
Upvote 0
Top