Android Question Get list of views from Activity

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

Similar to another question I've recently posted but different enough to require a new question which now has become two questions.

I wrote the following Sub to list the names of views in an Activity to a text file.

1. The sub is not writing anything to the Text file [Views.txt is created but empty]. It should at least write the heading, can anyone spot the error I have made?

2. When I "Log(v)" I get some View info but NOT the name. Is there a way to Write the name or am I up Ship Creek?

It all looks pretty simple but causing me grief.

Regards Roger


B4X:
Sub ViewsList
    Private VFunction As TextWriter
    Private WriteLineV As String

    VFunction.Initialize(File.OpenOutput(File.DirRootExternal & "/Download/", "Views.txt", False))
    
    WriteLineV = "List of Views"             'Heading
    VFunction.WriteLine(WriteLineV)       'Should write heading to Views.txt but doesn't
    
    For Each v As View In Activity.GetAllViewsRecursive       
        WriteLineV = v   
        VFunction.WriteLine(WriteLineV)   'Trying to write Views Name to Views.txt but doesn't work.
    Next
End Sub
 

Mahares

Expert
Licensed User
Longtime User
As Agraham mentioned, you need to fill in the name in the tag property and store the tag.
B4X:
Activity.LoadLayout("Layout1")  'example of layout with 1 button (btn), 2 labels(lbl1 and lbl2) and 1 panel (pnl)
    'the tag names are: btn, lbl1, lbl2, pnl
    Dim MyList As List
    MyList.Initialize
    For Each v As View In Activity.GetAllViewsRecursive
        MyList.Add(v.Tag)     
    Next
    File.WriteList (File.DirInternal, "mylist.txt", MyList)
    Dim List1 As List
    List1 = File.ReadList(File.DirInternal, "mylist.txt")
    For i = 0 To List1.Size - 1
        Log(List1.Get(i))
    Next
 
'    btn
'    lbl1
'    lbl2
'    pnl
 
Last edited:
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
A View doesn't seem carry it's name with it but you can set the Tag property to identify individual views.

agraham, Mahares

Thanks for the suggestion but two points.

1. The purpose of the Sub was to avoid manually writing a list of View Names, manually writing the names to the Tag location [over 150 views] is just as much work.
2. The Tag in many of the Views already holds text used by the App.

Regards Roger
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
2. The Tag in many of the Views already holds text used by the App.
It may not help in this case but as a Tag can hold any object so you could put a String Array or Map there with the App text and other data.
 
Upvote 0
Top