Kumar
Kumar

Reputation: 277

Android views as templates

I want to create an android Layout of which I want to instantiate several copies. I understand that declaring the layout in an XML will create a single instance. Is there a way to use the XML View declaration as a 'definition' rather than a 'declaration' ? Or is it possible to clone a view ?

Thanks;

Upvotes: 4

Views: 4498

Answers (4)

Robin
Robin

Reputation: 7316

Ok , you can use the method inflate(int resource, ViewGroup root) of LayoutInflater. and pass null as the second parameter.

However, You should keep in mind that LayoutInflator is quite expensive and should be used as less as possible

Upvotes: 0

Robin
Robin

Reputation: 7316

If you want to create a common template and use it in multiple screens, then declare that layout as xml and then use the LayoutInflater to add your other views to it.

I wonder if you want to use the same layout multiple no of times in same activity.

Upvotes: 0

Nathan Schwermann
Nathan Schwermann

Reputation: 31493

Here are some tips on reusing XML Views not sure if this is what you mean.. http://developer.android.com/resources/articles/layout-tricks-reuse.html

Upvotes: 0

Cheryl Simon
Cheryl Simon

Reputation: 46844

What do you mean by instantiate several copies? In the same activity? Or in different activities?

You can inflate a layout using the LayoutInflater. This will allow you to create multiple instances of the view in the same activity.

If you are create a list of information, where each list item has the same layout you can use a ListView, which will do the inflating for you.

Upvotes: 4

Related Questions