Android Question Unexplicable behaviour in For each v As View In Panel.GetAllViewsRecursive

welu1805

Active Member
Licensed User
Longtime User
Hi all,

I have a primitive question which makes me stupid:

In a panel (pnlInput) I have labels, checkboxes and buttons. With GetAllViewsRecursive I want to handle all views. But the Else command (***) is never processed. It seems all views are labels but they aren't.

B4X:
For Each v As View In pnlInput.GetAllViewsRecursive
            If (v Is Label) Then
                common1.ScaleText(v)

            Else   ' ***

                If v Is CheckBox Then
                   v.Left = PanelWidth - 1.5*v.Width
                Else
                    If v Is Button Then
                        v.Width = PanelWidth - 6%x
                        v.Left = 3%x
                    End If
                End If
            End If
        Next

Any idea what is wrong?
 

stevel05

Expert
Licensed User
Longtime User
It's because the are all subclasses of TextView take a look at the documentation : http://developer.android.com/reference/android/widget/CheckBox.html.

A B4a Label is a textview so they are all recognised when you check for Label.

If you change the order check for Checkbox first, then Button and then Label. it should work as you want it to.
 
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
Thanks you very very much. This saved my week start!

Now it works like expected.

But it means that IS doesn't word in all cases correctly.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Yes it works correctly, as they ARE all a form of TextView (Label), so it is correct, but you want to know which particular one it is. Therefore you need to check the lowest level first.
 
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
So if I have in a panel some EditTexts, Buttons and Labels and want only to set the TextSize of the labels the code must be:

B4X:
For Each v As View In pnlInput.GetAllViewsRecursive
   If (v Is EditText) Then
      ' DoNothing
   Else
     if (v Is Button) then
        ' DoNothing
     else
        if (v Is Label) then  
           v.TextSize = 32
        End If      
     End If
   End If
 Next

Is this correct?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Yes that looks OK, except that View doesn't have the TextSize method, you'll need to assign it to a Label variable first:

B4X:
 If (v Is Label) Then
    Dim L As Label = V
    L.TextSize = 32
End If
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Or more concisely:
B4X:
If (v Is Label) And Not(V Is EditText) And Not(V Is Button) Then
    Dim L As Label = V
    L.TextSize = 32
End If

But it does the same thing
 
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
Although I learned very much from you I must disagree to you:

B4X:
Dim v As View
Dim et As EditText
Dim Result As Boolean
   
et.Initialize("")
v = et
Result = v Is Label
Msgbox(Result, "")

Result is True and that is false!
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Although I learned very much from you I must disagree to you:

B4X:
Dim v As View
Dim et As EditText
Dim Result As Boolean
  
et.Initialize("")
v = et
Result = v Is Label
Msgbox(Result, "")

Result is True and that is false!

In what way is False???
As stated before, Label is a subclass of TextView, so the Natural result is "TRUE"
 
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
In what way is False???
As stated before, Label is a subclass of TextView, so the Natural result is "TRUE"

OK, in future I know in which sequence to use the commands "If .. Is ... Then" within
"GetAllViewsRecursive". But that is for me a workaround.

That a Label is a subclass of TextView is right, but the statement "v Is Label" with the result "True" above is wrong - for me. V in this case is a EditText and NOT a Label. The description of the command "Is" is not so helpful - for me.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
it is an android issue, and not a B4A one...
As suggested, use the Tag property as a way to differentiate the different sub-classes
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If it's critical, you could always try doing a more thorough compare:

B4X:
Sub IS2(V As View,CompareTo As String) As Boolean
    Select CompareTo.ToUpperCase
        Case "LABEL"
            If(GetType(V) = "android.widget.TextView") Then Return True
        Case "CHECKBOX"
            If(GetType(V) = "android.widget.CheckBox") Then Return True
        Case "EDITTEXT"
            If(GetType(V) = "android.widget.EditText") Then Return True
    End Select
    Return False
End Sub
 
Last edited:
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
It is not critical because I know this problem now. In another example I read all views of a panel with "GetAllViewsRecursive" and wanted to set the textsize of all labels and wondered that the textsize of the buttons also were scaled. Now I know why.

The result of IS2 is the behaviour that I expect of the command "Is" - like described.

Thanks again.
 
Upvote 0
Top