Canadian Coder Canadian Coder - 27 days ago 7
Android Question

Can't display table in Android

My long-term goal is to create a clickable table in an Android app that is made dynamically from a list of class objects. My short-term goal is to figure out how to get a table going at all. Right now I've defined a TableLayout inside design view and am trying to fill it dynamically/programatically.

Code:

XML File:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.nick.timelogger.itemTable"
android:background="#ffffffff">

<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="@+id/itemTable">

</TableLayout>
</RelativeLayout>


Class - OnCreate method (right now I'm just trying to add one row to get it displaying on my app):

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_table);

RelativeLayout.LayoutParams tableParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);

TableLayout itemTable = (TableLayout)findViewById(R.id.itemTable);
itemTable.setLayoutParams(tableParams);

TableRow header = new TableRow(getApplicationContext());
header.setLayoutParams(tableParams);

TextView itemName = new TextView(getApplicationContext());
itemName.setText("Activity Name");
itemName.setTextColor(Color.BLACK);
itemName.setTextSize(16f);
itemName.setTypeface(Typeface.DEFAULT);

TextView status = new TextView(getApplicationContext());
status.setText("Status");
status.setTextColor(Color.BLACK);
status.setTextSize(16f);
status.setTypeface(Typeface.DEFAULT);;

TextView elapsed = new TextView(getApplicationContext());
elapsed.setText("Elapsed Time");
elapsed.setTextColor(Color.BLACK);
elapsed.setTextSize(16f);
elapsed.setTypeface(Typeface.DEFAULT);

header.addView(itemName,(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)));
header.addView(status,(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)));
header.addView(elapsed,(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)));

itemTable.addView(header);
}


What am I missing?

Answer

I found two issues in my code:

1) First I was adding tableparams for the rowparams 2) Second I used a different form of 'header.addView', by taking out the second parameter, and that seemed to work.

It seems like Android changes so rapidly that pulling code out of old Stack answers doesn't always work quite right.