1

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 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
2

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.