Android Question View variable with multiple Layouts.

Tom_707

Member
Licensed User
I have a layout which is loaded onto two different panels, which are part of a single activity:

Panel1.LoadLayout("Layout1")
Panel2.LoadLayout("Layout1")

Layout1 has an edittext View. When I refer to the edittext View in code, for example:
edtView.Text = "Text"

I can only refer to the one that was loaded in the last LoadLayout, which is Panel2. How can I reference the edittext View of Panel1?

Any help will be appreciated.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Many ways to do it.
Tip: declare the panels type to be B4XView. B4XViews are easier to work with.

A simple way is to access the view based on its index:
B4X:
Dim et As B4XView = Panel1.GetView(4)
et.Text = "Text"
'or
Panel1.GetView(4).Text = "Text"
'or
Dim et As EditText = Panel1.GetView(4)

Another way:
B4X:
Panel1.LoadLayout("Layout1")
Panel1.Tag = edtView
Panel2.LoadLayout("Layout1") 
Panel2.Tag = edtView
'....

Dim et As B4XView = Panel1.Tag
et.Text = "Text"
And there are many more ways to organize your references.
 
Upvote 0

Tom_707

Member
Licensed User
Thanks so much Erel for the help. I've figured out what the problem was. I'm just used to being able to link multiple methods in a single line to reference a property, for example:

Object.Method1.Method2.Property

In B4A, it seems the only way to do this is by assigning each method to a separate variable, before you can reference the final property:

Variable1 = Object.Method1
Variable2 = Variable1.Method2
Variable3 = Variable2.Property


For example:
B4X:
Dim pnl As Panel
Dim edt As EditText

pnl = SomePanel                      (SomePanel has an EditText child View in position 0)
edt = pnl.GetView(0)
Log(edt.Text)


Just something I'll have to get used to. Or is there a better way to do this?
 
Last edited:
Upvote 0

Tom_707

Member
Licensed User
OK, no problem. I was posting 'pseudo-code' so I didn't think it was required. I'll edit it now.

[Edit] I must say, I have to agree. That looks much nicer and neater! Thanks for the help Erel.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
In B4A, it seems the only way to do this is by assigning each method to a separate variable, before you can reference the final property:
No. You haven't read my answer carefully.
See my code:
B4X:
Panel1.GetView(4).Text = "Text"

For this to work you need to switch to B4XView, as Panel.GetView returns a View object that doesn't have a Text property.
 
Upvote 0

Tom_707

Member
Licensed User
Yes, that does the trick! A normal View has a limited amount of properties available, whereas a B4XView seems to be able to access all the properties. Thanks Erel!
 
Upvote 0

Tom_707

Member
Licensed User
No. You haven't read my answer carefully.
See my code:
B4X:
Panel1.GetView(4).Text = "Text"

For this to work you need to switch to B4XView, as Panel.GetView returns a View object that doesn't have a Text property.

Hi Erel,

The B4XView does not seem to be able to access the properties of a Spinner view (or am I missing something again?).

Is there another way of achieving the equivalent of:
B4X:
B4XView.GetView(0).SelectedItem


Are there any other Views that B4XView doesn't access the properties of?
 
Last edited:
Upvote 0
Top