Android Question Get a unique identifier for a view ???

Yayou49

Active Member
Licensed User
Hi,

I add one views in a layout by designer or by code.
Let set as exemple, 1 buttons (can be also a Panel view or else).
I gave it a name: BT1.

B4X:
Dim BT1 as Button
bt1.initialize("BT1")
Activity.AddView(BT1, 10%x, 20%y, 10%x, 10%x )

Using the code bellow, I can retrieve my button, but my problem is:

How to retrieve the name (BT1) or at least a unique ID for this button ???
My purpose is to get a unique identifier for my view each time the apk is launched (identifer must be the same each time the apk is launched)

B4X:
For Each v As View In Activity.GetAllViewsRecursive
   log(v.????)
Next

Too involved by my project, I can't find how.
If solution is quite basic, sorry in advance !!!!.....
 

npsonic

Active Member
Licensed User
Use tags

B4X:
Dim BT1 as Button
bt1.initialize("BT1")
Activity.AddView(BT1, 10%x, 20%y, 10%x, 10%x )
bt1.Tag = "BT1"

B4X:
For Each v As View In Activity.GetAllViewsRecursive
   If v.Tag <> Null Then
        Select v.Tag
             Case "BT1"
                  Log("BT1")
             Case "BT2"
                  Log("BT2")
        End Select
    End If
Next
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Tag is alone correct way. Really it's not simple string or integer value. Tag is object.
This means that should be an agreement inside the team. A tag will be a list, first element will be used by Alex, second by Nick etc.
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
There might be a way…. Not tested
when you do "log(btn1) the log will show a unique value that is a reference to that view in that activity.
The main problem is, you can reuse any view without ever destroying it, !but!, this is the problem, if I am not mistaken, each time you change a view's parent, that "unique" reference changes too. Even when you "clone" a view, there is no assurance it will stay the same …

SO, in conclution, you will need to do your own testing!
 
Upvote 0

npsonic

Active Member
Licensed User
It might be that I don't understand what you exactly are trying to do, but why couldn't you just give some unique tag for each view and then save extra data about that view with the identifier to map.
Tag is a key in map and value can be anything you want to store about that particular view.
 
Upvote 0
Top