leonprou
leonprou

Reputation: 4898

How to add Layout as a child

In my application I have 2 layouts. One's is root layout that changes dynamically and the second is just a form that opens many times. The form must be child of the root layout but I'm failing to perform so.

I assume that I should simply use:

main.AddView(formLayout)

but I can't figure out how get this formLayout object.

Will thank you for possible answers.

Upvotes: 0

Views: 1980

Answers (4)

leonprou
leonprou

Reputation: 4898

With the advice of cjk I wrote piece of code that actually answers my question:

setContentView(R.layout.main);
main = ((ViewGroup)findViewById(android.R.id.content));
LayoutInflater inflater = this.getLayoutInflater();
ViewGroup form=  (ViewGroup) inflater.inflate(R.layout.formLayout, null);
main.addView(form);

Thank you all

Upvotes: 1

Kriggs
Kriggs

Reputation: 3778

By get I figure you mean you want to retrieve a field in the new opened layout, you can do it by making it a new Intent and using startActivityForResult instead of startActivity.

Upvotes: 0

cjk
cjk

Reputation: 46425

Sounds like you need the LayoutInflater object Android reference.

This allows you to create an object from the xml layout in your project.

Upvotes: 2

DonGru
DonGru

Reputation: 13710

Not sure if I understand the question properly, but something like that might work:

View myView;
myView = (View) this.findViewById(R.id.formLayout);
main.addView(myView);

Upvotes: 0

Related Questions