I'm programming an Android app and I'm trying to show a table. My problem is, that the columns don't fill the screen, they just all stick to left.
I already tried using padding, but first I don't think that it's compatible with all display resolutions and second the columns were aligned differently when they had different content in them.
Then I read about weight and this was exactly what I wanted, giving every column a specific percentage of the width of the screen. But weights did lead to the cells sticking to the left again. They did react in no way to the weights I've set.
I program the view in xml templates and inflate them programmatically using the LayoutInflater. Then I add them to the corresponding TableRow. All that works perfectly fine, but they just don't react to the weights.
I would really appreciate your help.
EDIT: The templates refer to a style and look like this:
<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/scheduleClass" />
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="scheduleItem" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:paddingRight">0dp</item>
<item name="android:paddingBottom">10dp</item>
</style>
<style name="scheduleClass" parent="scheduleItem">
<item name="android:layout_weight">0.2</item>
</style>
</resources>
public void updateList(ArrayList<Lesson> lessons) {
setContentView(R.layout.schedule);
TableLayout table = (TableLayout) findViewById(R.id.table);
for(Lesson cancel : lessons) {
TableRow row = new TableRow(this);
TextView date = (TextView) this.getLayoutInflater().inflate(R.layout.scheduledatetemplate, null);
date.setText(cancel.date);
TextView classname = (TextView) this.getLayoutInflater().inflate(R.layout.scheduleclasstemplate, null);
classname.setText(cancel.classname);
TextView lesson = (TextView) this.getLayoutInflater().inflate(R.layout.schedulelessontemplate, null);
lesson.setText(cancel.lesson);
Button details = (Button) this.getLayoutInflater().inflate(R.layout.detailsbuttontemplate, null);
details.setText(R.string.details);
details.setOnClickListener(this);
row.addView(date);
row.addView(classname);
row.addView(lesson);
row.addView(details);
table.addView(row);
}
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:scrollbars="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
</TableLayout>
</ScrollView>
You need to declare android:layout_column
and android:layout_span
attributes to your TableRow
s
See http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html
See an example here: http://www.mkyong.com/android/android-tablelayout-example/
UPDATE: Ok, you'll need to set the layout_span and layout_column in each of the rows you're creating, use this addView method: http://developer.android.com/reference/android/widget/TableLayout.html#addView(android.view.View, int, android.view.ViewGroup.LayoutParams)
To supply the layout params with the appropriate column and span, to make your row span to the entire table, and have the row gravity=center to make their content in the center.
Also, the table attribute strechColumns
http://developer.android.com/reference/android/widget/TableLayout.html#attr_android:stretchColumns
Might help you, as in:
android:strechColumns="*"
or something like that.