I am desperately trying to set TextView attributes of cells within a table programmatically but can't get this to work! Whenever I set layout properties, the field will simply not appear (but not give any error or exception). I boilded this down to this simple example:
package mmo.application.listpro;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class Test extends Activity
{
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout table = new TableLayout(this);
TableRow row = new TableRow(this);
for (String label: new String[] {"field1", "field2", "field3"}) {
TextView tv = new TextView(this);
tv.setText(label);
// LinearLayout.LayoutParams lp =
// new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
// LinearLayout.LayoutParams.WRAP_CONTENT);
// lp.setMargins(2, 2, 2, 2); // left/top/right/bottom
// tv.setLayoutParams(lp);
row.addView(tv);
}
table.addView(row);
setContentView(table);
}
}
This will show three fields, but when you uncomment the five commented line, then NOTHING will appear. Why is that so? Why does setting layout parameters cause my TextView's to not appear? I'm stuck! What am I missing?
Michael
PS.: here's the manifest, if some kind soul quickly wants to try this out:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mmo.application.listpro" android:versionCode="1" android:versionName="Development">
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:debuggable="true">
<activity
android:label="test"
android:name=".Test"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:targetSdkVersion="8" android:minSdkVersion="8" />
</manifest>