Android Question finding custom views

Jack Cole

Well-Known Member
Licensed User
Longtime User
I have some code that iterates through all the loaded views and pulls the values from the views that users can enter data into (such as a spinner, edittext, etc...). The problem comes in with adding custom views or XUI views.

Consider the following function:
B4X:
Sub get_field_data As Map
    LogColor("get_field_data",Colors.Yellow)
    Dim m As Map
    m.Initialize
    For Each v As View In pnldata.GetAllViewsRecursive
        Dim t As String
        t=v.Tag
        
        If t<>"" And t<>"null" And t<>"XUI" Then
            Log("tag="&t)
            LogColor(GetType(v),Colors.Red)
            If v Is EditText Or v Is AutoCompleteEditText Then
                Dim v2 As EditText
                v2.Initialize("")
                v2=v
                Try
                    m.Put(t.SubString2(0,t.IndexOf(" ")),v2.Text)
                Catch
                End Try
            End If
            If v Is Spinner Then
                Dim sp As Spinner
                sp.Initialize("")
                sp.Tag=t
                sp=v               
                Try
                    m.Put(t.SubString2(0,t.IndexOf(" ")),sp.SelectedIndex)
                Catch
                End Try           
            End If
            If v Is Button Then
                Dim b As Button
                b.Initialize("")
                b=v
                Try
                    m.Put(t.SubString2(0,t.IndexOf(" ")),b.Text)
                Catch
                End Try       
            End If
            If v Is B4XComboBox Then
                LogColor("Found B4xcombobox!",Colors.green)
                Dim cb As B4XComboBox
                cb=v
                Try
                    m.Put(t.SubString2(0,t.IndexOf(" ")),cb.GetItem(cb.SelectedIndex))
                Catch
                End Try
            End If
            If v Is AnotherDatePicker Then
                LogColor("Found AnotherDatePicker!",Colors.green)
                Dim adp As AnotherDatePicker
                adp=v
                Try
                    m.Put(t.SubString2(0,t.IndexOf(" ")),adp.Date)
                Catch
                End Try
            End If
        End If
    Next
    Return m
End Sub

Everything works great for traditional views, but the problem comes in when you get to the condition for B4XComboBox and AnotherDatePicker. The "Is" operator does not identify the view as being the type that is shown. Is the only answer to identify these custom views by tags?
 

sorex

Expert
Licensed User
Longtime User
yes, you can use the tag to add viewtype and id. just use regex.split to seperate it when needed
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Custom views classes are not views. Views returned from B4XView.GetAllViewsRecursive will never include the custom views instances.

1. Update to XUI Views v1.66+. Starting from this version the custom view class is set as the base view tag value.

2.
B4X:
For Each v As B4XView In Base.GetAllViewsRecursive
       If v.Tag Is B4XComboBox Then
           Dim cmb As B4XComboBox = v.Tag
           Log(cmb.SelectedIndex)
       End If
   Next
 
Upvote 0

Jack Cole

Well-Known Member
Licensed User
Longtime User
That could help in some instances. What happens when we set the tag to some value in the designer? Are you limited to either the tag set to an instance of the object or to the text set in the designer?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Good point. I forgot that the Tag property is set by the layout engine to the base view. Previously the base view was not exposed (at least not in all cases) so the tag couldn't have been extracted at all. I will add a Tag field to the classes which will hold the designer tag.
 
Upvote 0

Jack Cole

Well-Known Member
Licensed User
Longtime User
Perfect! That does what I need.

I had to explore this a bit to understand what you did. The holder layout adds the reference to the customview. Then the customview's tag is that tag you set in the designer.

B4X:
    For Each v As View In Activity.GetAllViewsRecursive
        Log(GetType(v))
        If v.Tag Is B4XComboBox Then
            LogColor("Found a combobox tag! ",Colors.Magenta)
            Dim cb=v.Tag As B4XComboBox
            LogColor("Its tag is: "&cb.Tag,Colors.green)
        End If
  Next



One last thing: It would be great to fold AnotherDatePicker into the XUI views as well.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…