I think this:
tl.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
is wiping out the layout params you set earlier. Just use tl.addView(tr);
Question
I've looked through all the people that have asked the same thing and no one has ever answered correctly the question. For some reason I can't seem to find how to do this. I need to add a margin between each of my dynamically made TableRows. This is what I have now.
As you can see, the rows are way together, making the GUI not usable for a user. The TableLayout is inside a ScrollView. This is the code I use to create dynamically the TextView and the button. I have tried using margins but no luck so far
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
TableLayout tl = (TableLayout) findViewById(R.id.TableLayout1);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
/* Margin. */
int leftMargin = 0;
int topMargin = 200;
int rightMargin = 0;
int bottomMargin = 0;
/* Set margins and create new textview. */
tableRowParams.setMargins(leftMargin, topMargin, rightMargin,
bottomMargin);
tr.setLayoutParams(tableRowParams);
TextView encuestasTextView = new TextView(this);
encuestasTextView.setText("Esto es una prueba");
encuestasTextView.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(encuestasTextView);
Button a = new Button(this);
a.setText("Abrir encuesta");
a.setLayoutParams(new TableRow.LayoutParams(width / 3,
TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(a);
tl.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
This is the XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Encuestas" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button1"
android:layout_weight="1"
android:scrollbars="vertical" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:id="@+id/TableLayout1">
</TableLayout>
</ScrollView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="14dp"
android:layout_marginTop="14dp"
android:onClick="SyncServer"
android:text="Sync" />
</RelativeLayout>
What I'm missing?
Solution
I think this:
tl.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
is wiping out the layout params you set earlier. Just use tl.addView(tr);