Android Question Customizing individual pages of ViewPager?

klarsys

Active Member
Licensed User
Longtime User
When using StdActionBar / StdViewPager, if I want to change button Text or other properties on multiple pages, how do I do that?
I see in example that If I change button text, it reflects only in last tab.
 

DonManfred

Expert
Licensed User
Longtime User
Upload a sample app which show the problem...

I guess you are doing something like:
- Create button in Designer. For ex. btnTest
- Load the layout and put it on a panel... You can set btnTest.Text at this point...
- You do the same for any amount of layouts/panels. Always with the name btnTest.

After that you want to change btnTest.Text and it only affects the last added one.

Is this the case?

If not then you should better upload a sample app code. In the IDE -> File->Export as zip and upload it here.
 
Upvote 0

klarsys

Active Member
Licensed User
Longtime User
I did with the default example itself.
It adds three panels and loads different layout on each of them.
All three layouts have a button named 'Button1', and 'Button1_Click' serves as common handler for each of them.
However, if I do Button1.Text = "TEST" in Activity_Create after all layouts are loaded, i see it affects only last layout.

Maybe, I have to change Button1.Text after loading each layout. That will work when loading, but not at run time if I want to change it at a later point of time.
Maybe, I should name each of them uniquely.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
However, if I do Button1.Text = "TEST" in Activity_Create after all layouts are loaded, i see it affects only last layout.
it is the same case like in my question above....

If you load a layout with a button named btn1 then you are creating a NEW instance of the button
If you load another layout with the same button you crete another new instance.

After doing this you can only access the last loaded btn1.

What you can do is:

Create a list and initialize it.
B4X:
dim btnlist as list
btnlist.initialize
After you loaded the layut with btn1 the first time you add a reference to btn1 to the list
B4X:
btnlist.add(btn1)

At the end you have all buttons in the list...

If you then want to change all buttons then you can iterate through your list.

B4X:
for i= 0 to btnlist.Size-1
  dim btn as button = btnlist.get(i)
  btn.text = "New Text"
next
 
Upvote 0

klarsys

Active Member
Licensed User
Longtime User
After doing this you can only access the last loaded btn1.
Oh, I get it.

I'll have to re-think my usage now. I wanted to reuse same layout in multiple (dynamic) panels and individually control their contents - as if they are individual instances. Above approach will work but will heavily complicate coding.
 
Upvote 0

klarsys

Active Member
Licensed User
Longtime User
It is not only buttons. there are multiple buttons, labels, etc.

It would have helped if a reference to btn1 applied to btn1 in currently visible layout. I can make sure that no two views (across multiple layouts) will ever have the same name. But managing multiple instances of each button-type and each label-type, etc will get complicated.

Is it possible to refer a view from complete hierarchy?
e.g. vp.Panels(currentPage).btn1.Text = "TEST"
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I can make sure that no two views (across multiple layouts) will ever have the same name.
yes. DONT load layout s with the same objectnames :)
You can not check this by code

You can for sure add all views by code and not by designer; here you have more control.
But it will be an similar solution... You need a list (or a array) to store objects in to find them more easy.

You can for sure iterate through all object of an activity or of an panel and get a recursive list of all child-objects and to find a button or whatever.
 
Last edited:
Upvote 0
Top