tags:

views:

187

answers:

2

Sorry for the Newb-ness.

I want to create a list of view elements in a LinearLayout (vertical). I created an xml layout that is a TableLayout called "category_list.xml"

<TableLayout>
  <TableRow>
   <ImageView />
   <TextView />
   <CheckBox />
  </TableRow>
</TableLayout>

I want to iterate an array, on each iteration create a new TableLayout view and add it to the LinearLayout. The peice I'm missing is creating a new TableLayout based on the above xml.

Something like

TableLayout t = new TableLayout( R.layout.category_list );

Can someone point me in the right direction? Is it better to generate the TableLayout programatically?

+2  A: 

You want to use a LayoutInflater to "inflate" the xml files. You can get LayoutInflater in an activity by using getLayoutInflater(). Here's how it works (assuming the id of your LinearLayout is "parent"):

LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
LayoutInflater inflater = getLayoutInflater();
TableLayout t = (TableLayout) inflater.inflate(R.layout.category_list, parent);
Daniel Lew
+3  A: 

Or by using the static View.inflate function

TableLAyout t = (TableLayout) View.inflate(this, R.layout.category_list, null);

Anyway, be carefull with inflating and deleting too many views in your app, as short lived objects leak memory. Consider using an ListView with and Adapter instead.

PHP_Jedi
I wanted to use a ListView originally, but I am under the impression that to use a ListView, the activity is required to extend ListActivity, which I can't because it is already extending MapActivity.
karnage