tags:

views:

330

answers:

2

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> 
A: 

The direct parent of the TextViews is a TableRow not a TableLayout, so use TableRow.LayoutParams instead of TableLayout.LayoutParams.

MatrixFrog
+1  A: 

TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT. So you just need to create default TableRow.LayoutParams, set margins and apply it to the TextView.

TableRow.LayoutParams lp = new TableRow.LayoutParams();
lp.setMargins(2, 2, 2, 2);
row.addView(tv, lp);;

bhups
That did the trick! I hadn't realized that each layout/view has it's own type of LayoutParams. So I obviously had picked the wrong one... What beats me: TableLayout as well as TableRow both inherit from LinearLayout. But TableRow.LayoutParameters apparently does not inherit from LinearLayout.LayoutParameters and so obviously they were simply ignored...
mmo