Android Question GetView by name

LucaMs

Expert
Licensed User
Longtime User
This is something which I have always felt the lack.

While searching here on the site if there was at least a way to match the indexes (badly explained but now it does not matter) I found this thread/Library:
XmlLayoutBuilder - Load Xml layouts

In the example, there is:
B4X:
Dim x As XmlLayoutBuilder
' ...
Panel1 = x.GetView("panel1")

In addition, Periklis Koutsogiannis has found a way to solve this problem.

How we can do it?
 

LucaMs

Expert
Licensed User
Longtime User
What is done in that example.

I would like to create "Item layouts" for scrollviews using the Designer (in particular for CheckList) and once you got the panel in code, manage the views contained by their name, because by index gets all twisted.

(I could create the layout in Eclipse but the views may be different)


P.S. not "access views based on a string" (I know I could use global maps), I wish use their names!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The tag is perfect for this. Set the name you like in the tag property of the views (in the designer).

You can then find the correct view with this code:
B4X:
Sub FindViewByTag(Parent As Panel, Name As String) As View
 For Each v As View in Parent.GetAllViewsRecursive
  If Name = v.Tag Then Return v
 Next
 Log("View not found: " & Name)
 Return Null
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
BalConverter and XmlLayoutBuilder are not really related to this question.

Remember that there could be many views with the same name. Anyway the "designer name" is currently not accessible unless you set it in the Tag.


???????

You CAN'T have many views with the same name

upload_2014-8-27_8-12-14.png


Although it is complex, I noticed that using BALConverter and JSONTree you could obtain the names, but I do not know enough about these "tools".

In addition, it is certain that it is possible to do so, since Koutogiannis did it.


Thanks
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
In addition, it is certain that it is possible to do so, since Koutogiannis did it.
Views in Java have no name property. When you create them in the designer, you give them a name because your object could not be used in the code without an identifier. PK gets this identifier with his XtraViews lib by reading the layout file. There's no new property here nor new way of doing things. That's why the PK's solution cannot be used for views created by code.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Views in Java have no name property. When you create them in the designer, you give them a name because your object could not be used in the code without an identifier. PK gets this identifier with his XtraViews lib by reading the layout file. There's no new property here nor new way of doing things. That's why the PK's solution cannot be used for views created by code.

"PK gets this identifier with his XtraViews lib by reading the layout file"

That would be enough; for this reason I think that this could be achieved by "BALConverter and JSONTree" (taking advantage of their code).

In your CheckList normally you use AddCustomItem passing a panel to it created by a function (say CreateItem).

If into the CreateItem we could load the layout from the Designer and the ItemClick event could be able to pass view data to an object of type Type or better to an instance of a class, the CheckList would become perfect.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
The tag is perfect for this. Set the name you like in the tag property of the views (in the designer).

You can load the same layout file multiple times or have multiple layout files with the same names.

hi,
loading the same layout multiply times will also give you multiply views with the same TAG.

i am also looking for a way to identify my views with the sender method.
i dont want to use the tag object since its a waste using it for a simple string. i want to use if for a type object with 10 different objects so the tag is already used for something different and i also dont want to have 20 subs so i create 20 views in the designer with the same layout file but i want to use the v.name to know what view was clicked.

is it possible?

B4X:
Sub shiftp_Click
    Dim v As View = Sender
 
    If v Is Label Then
        Dim lbl As Label = v
         Select lbl.tag
             Case "date"
                Dim dd As DateDialog
                dd.SetDate(DateTime.GetDayOfMonth(DateTime.now),DateTime.GetMonth(DateTime.now),DateTime.GetYear(DateTime.now))
                If dd.Show("נא לבחור תאריך.", "תאריך".ToUpperCase, "אישור", "ביטול", "", Null) = DialogResponse.POSITIVE Then
                    dateshift.Text = DateTime.Date(dd.DateTicks)
                End If
             Case "in"
                Dim td As TimeDialog
                td.SetTime(DateTime.GetHour(DateTime.Now),DateTime.GetMinute(DateTime.Now),True)
                If td.Show("","","אישור","ביטול","",Null) = DialogResponse.POSITIVE Then
                    inshift.Text = DateTime.Time(td.TimeTicks)
                End If
             Case "out"     
                Dim td As TimeDialog
                td.SetTime(DateTime.GetHour(DateTime.Now),DateTime.GetMinute(DateTime.Now),True)
                If td.Show("","","אישור","ביטול","",Null) = DialogResponse.POSITIVE Then
                    outshift.Text = DateTime.Time(td.TimeTicks)
                End If
            Case "cancel"
                newshiftpnl.SetVisibleAnimated(200,False)
            Case "save"
                If newshiftpnl.Tag <> "" Then 'edit
                 
                Else 'new day
                                         
                End If
            Case "delete"
                If newshiftpnl.Tag <> "" Then
                    allshift.Remove(newshiftpnl.Tag)
                End If
                newshiftpnl.SetVisibleAnimated(200,False)
         End Select
    else if v Is ImageView Then
        Dim img As ImageView = v
        Log("imageview: " & img.Tag) 
    End If
End Sub

here i am using the tag object but if i do it i have no way to use it for a type object that changes in code.
so i need a view property that is unique and the name is the only one that fits for it.

what do you guys suggest me to do?

thank you

EDIT: maybe this can help? http://stackoverflow.com/questions/10137692/how-to-get-resource-name-from-resource-id
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
i dont want to use the tag object since its a waste using it for a simple string. i want to use if for a type object with 10 different objects so the tag is already used for something different and i also dont want to have 20 subs so i create 20 views in the designer with the same layout file but i want to use the v.name to know what view was clicked.
Sorry but this time too I read fast and English is not exactly my mother tongue.

I would use a Map in the tag (one of the differences with a Type is that Maps are "dynamic")
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Sorry but this time too I read fast and English is not exactly my mother tongue.

I would use a Map in the tag (one of the differences with a Type is that Maps are "dynamic")

i create my views in designer so there is no way to use something different then a string for the tag property.
 
Upvote 0
Top