How can I implement a GetViewByName function ?

fiaful

Member
Licensed User
Longtime User
Hi there!

I would need to build a function that will return me a view, contained in a panel, knowing only the name of the view (no ID, no tags) ...

The panel is populated by a call to other loadlayout and later in a sub I can call asynchronously use the method call GetView, passing the index of the view that I intend to use.

But since the designer I insert my view by specifying the name, I also want to take just using their name!

I tried building a function in java like this:

B4X:
int Id = pBA.context.getResources().getIdentifier(Name, "id", pBA.context.getPackageName());
Log.d("B4A", "GetViewByName - Id = " + Id);
View V = container.getObject().findViewById(Id);
Log.d("B4A", "GetViewByName - V = " + V);
return V;

but the Id returned is always 0 (probably because the id is not mapped in the R class)

how could I do? how do you do it?

you could post a snippet that works?

Thanks to all in advance for any response.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Views do not really have names.
You can however create such a mapping yourself:
B4X:
Sub LoadLayoutToPanel(p As Panel, file As String)
p.LoadLayout(...)
dim m As Map
m.Initialize
m.Put("Button1", Button1)
m.Put("Button2", Button2)
...
p.Tag = m
End Sub

The result is that in the Tag property of each panel you will have a map that maps between the "names" and the views.
 
Upvote 0
Top