AleCat83
AleCat83

Reputation: 1493

define a recurrent set of views to use multiple time in the same layout

I'm working on an android project where I have a set of views (2 TextViews and some checkboxes in a checkbox group) to replicate many time in the same activity. Is it possible to define the layout only for one set and instantiate it many time? Also, the views are grouped in a Relative layout, is it possible to position the without the id attributes (to avoid id duplication)?

Upvotes: 0

Views: 40

Answers (2)

Stimsoni
Stimsoni

Reputation: 3156

You can do this by defining the fields you want to reuse in their own xml. you can then use the 'include' tag for where you want them to display.
http://developer.android.com/training/improving-layouts/reusing-layouts.html

You do need to define the id's to position them in the relative layout. What is your concern about replicating the id's.

The other thing worth mentioning is how to use findById() when using 'include'. You can put a id on the include tag (which is effectively the relative layout viewgroup). Find that group first (Cast to viewgroup) and then do a findbyId on that group for what ever view you are after.

Upvotes: 1

Robin Dijkhof
Robin Dijkhof

Reputation: 19278

I would use a ListView for this. Even if you got like 5 items it workd fine. If you got many more items it still works perfect. Take a look at this example.

Upvotes: 2

Related Questions