Android Question Setting properties for each view

alon

Active Member
Licensed User
Longtime User
Hi,
I need to run on all views in a panel
and if the view is a button or label, then I need to change its font size or the typeface.

Can anyone tell me if it is possible and how

Thanks.
 
Read the answer from Erel to a similar thread opened yesterday.
 
Upvote 0
Code: Copy
for each l as label in panel
   'set label properties
next

for each b as button in panel
   'set button properties
next
 
Upvote 0
Code: Copy
for each l as label in panel
   'set label properties
next

for each b as button in panel
   'set button properties
next

This code will not work because a panel is a view and not an array or list and "for each" need to look inside a list.

So you have to add .getallviewsrecursive.

Code: Copy
    For Each v As View In panel1.GetAllViewsRecursive
        If v Is Label Then 'Label and Button will be executed
            Dim lbl As Label = v
            '....
        End If
    Next

Note that if there is a button the if you ask if the view is a label it will return true since a button is basically a label.
 
Upvote 0
Note that if there is a button the if you ask if the view is a label it will return true since a button is basically a label.
You can use GetType to know "exactly" what type it is:

Code: Copy
    Dim ViewType As String
  
    For Each V As View In Panel1.GetAllViewsRecursive
        ViewType = GetType(V)
        Log(ViewType)
        If ViewType.EndsWith("Button") Then
            Log("Is a button")
        Else If ViewType.EndsWith("TextView") Then
            Log("Is a Label")
        Else If ViewType.EndsWith("EditText") Then
            Log("Is an EditText")
        Else
        End If
    Next

[You could optimize the code, getting the text after the last dot of ViewType]
 
Last edited:
Upvote 0
Upvote 0
Back
Top