Android. Programmatically added rows are not stretching well (TableLayout) - android

I have a TableLayout declared in a XML File, it will show scores, and these scores are added via rows programmatically. The problem is that the layout without any score added looks like this (As it should be):
Correct
But when a score's row is added, it looks like this:
Wrong
I can’t understand why this is happening, I would like to see the columns well stretched and there’s no apparent reason that explains this behavior. If anyone can help me i'll be very grateful.
Thanks in advance and sorry for my english.
Here is my XML and my code:
XML:
<TableLayout xmlns...
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:stretchColumns="*"
android:layout_column="5"
android:id="#+id/scoresTable">
<TableRow
android:layout_gravity="center_horizontal"
android:id="#+id/titleScoresRow"
android:layout_height="42dp">
<TextView
android:id="#+id/scoresTitle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Best Scores"
android:layout_span="5"
android:gravity="center"
/>
</TableRow>
<TableRow
android:layout_gravity="center_horizontal"
android:id="#+id/firstRow"
android:layout_height="42dp">
<TextView
android:id="#+id/MODE"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="MODE"
android:gravity="center"
/>
<TextView
android:id="#+id/KANA"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="KANA"
android:gravity="center"
/>
<TextView
android:id="#+id/DIFF"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="DIFF"
android:gravity="center"
/>
<TextView
android:id="#+id/SCORE"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="SCORE"
android:gravity="center"
/>
<TextView
android:id="#+id/DATE"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="DATE"
android:gravity="center"
/>
</TableRow>
Activity:
TableLayout table = (TableLayout) findViewById(R.id.scoresTable);
createOrderedCSV(getApplicationContext(), 4, ":");
ArrayList<String> scores = readScores("OrderedScoresCSV.csv");
// Score rows
for (int i = scores.size() - 1; i >= 0; i--) {
String[] currentScore = scores.get(i).split(":");
TextView mode = new TextView(this);
mode.setText(currentScore[0]);
TextView kana = new TextView(this);
kana.setText(currentScore[2]);
TextView diff = new TextView(this);
diff.setText(currentScore[1]);
TextView score = new TextView(this);
score.setText(currentScore[4]);
TextView date = new TextView(this);
date.setText(currentScore[3]);
TableRow row = new TableRow(this);
row.addView(mode);
row.addView(kana);
row.addView(diff);
row.addView(score);
row.addView(date);
table.addView(row, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
}
TableRow buttonRow = new TableRow(this);
Button eraseScores = new Button(this);
eraseScores.setWidth(200);
eraseScores.setHeight(60);
eraseScores.setText("Erase Scores");
buttonRow.setGravity(Gravity.CENTER_HORIZONTAL);
buttonRow.addView(eraseScores);
table.addView(buttonRow);

It was caused by the button row that I added at the end. It was attached to the first column.

Related

dynamically added rows in tablelayout are (still) not visible

I'm experiencing the same issue as this one.
Unfortunately, the answer to that one does not work for me.
I know the rows are are added to the table, I can see the number of rows grow in logcat.
But I don't see them on my screen.
Investigated refresh (invalidate), color, dimensions, layout params, ...
... and ran out of ideas
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnAddRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add row" />
<TableLayout
android:id="#+id/tableLayoutInventory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow
android:id="#+id/tableRowTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/headerCol1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID"
android:textSize="16dp" />
<TextView
android:id="#+id/headerCol2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Titre"
android:textSize="16dp" />
</TableRow>
</TableLayout>
</LinearLayout>
Code:
public void onClick(View v) {
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(this);
tv.setText("ID, row "+rownb);
tv.setHeight(16);
tv.setTextSize(16);
tv.setTextColor(Color.RED);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
row.addView(tv);
TextView tv2 = new TextView(this);
tv2.setHeight(16);
tv2.setTextSize(16);
tv2.setTextColor(Color.RED);
tv2.setText("Title, row "+rownb);
tv2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
row.addView(tv2);
table_layout.addView(row);
table_layout.requestLayout();
table_layout.invalidate();
table_layout.postInvalidate();
Log.i ("LIMA","Add row. There are now "+table_layout.getChildCount()+" rows");
}
Remove those LayoutParamson the views you're adding in the table rows

How to get layout working programmatically

As my Android apps are growing I need to include dynamic characteristics that include growing/reducing tables.
Therefore, I have switched from XML defined to programmatic methods.
My main XML, containing the basic table layout is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<TableLayout
android:id="#+id/table_layout_abonos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="top" >
</TableLayout>
</LinearLayout>
And then I try to define the header of the table using a TableRow that contains a set of titles for each table row by means of different TextView.
I have implemented this as follows:
public void añadeCabeceraTablaAbonos() {
TableLayout layoutTablaAbonados = (TableLayout) findViewById(R.id.table_layout_abonos);
int rowHeight = getStandardPixels(40);
TableRowTitulos cabeceraTablaAbonados = new TableRowTitulos(this);
cabeceraTablaAbonados.setId(100);
/*
* android:id="#+id/cultivo_auxiliares" android:layout_width="400dp"
* android:layout_height="40dp" android:layout_marginLeft="40dp"
* android:gravity="center"
* android:text="#string/cultivo_parcela_auxiliar"
* android:textAppearance="?android:attr/textAppearanceMedium"
*
* <TableRow android:layout_marginBottom="10dp"
* android:background="#color/rojo_dupont" >
*/
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
tableRowParams.setMargins(0, 0, 0, getStandardPixels(10));
cabeceraTablaAbonados.setLayoutParams(tableRowParams);
header_abonado_cultivo = new TextView(this);
header_abonado_abono = new TextView(this);
header_abonado_fecha = new TextView(this);
header_abonado_dosis = new TextView(this);
header_abonado_dosificacion = new TextView(this);
header_abonado_metodo = new TextView(this);
header_abonado_deposito_A = new TextView(this);
header_abonado_deposito_B = new TextView(this);
header_abonado_deposito_C = new TextView(this);
header_abonado_deposito_D = new TextView(this);
header_botonera = new TextView(this);
header_abonado_cultivo.setId(101);
header_abonado_cultivo.setText("CULTIVO");
header_abonado_cultivo.setGravity(Gravity.CENTER);
header_abonado_cultivo
.setWidth(getStandardPixels(ANCHO_COLUMNA_CULTIVO_ABONO));
//header_abonado_cultivo.setHeight(rowHeight);
header_abonado_cultivo.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_abonado_abono.setId(102);
header_abonado_abono.setText("ABONO");
header_abonado_abono.setGravity(Gravity.CENTER);
header_abonado_abono
.setWidth(getStandardPixels(ANCHO_COLUMNA_ABONO_ABONO));
//header_abonado_abono.setHeight(rowHeight);
header_abonado_abono.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_abonado_fecha.setId(103);
header_abonado_fecha.setText("FECHA");
header_abonado_fecha.setGravity(Gravity.CENTER);
header_abonado_fecha
.setWidth(getStandardPixels(ANCHO_COLUMNA_FECHA_ABONO));
//header_abonado_fecha.setHeight(rowHeight);
header_abonado_fecha.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_abonado_metodo.setId(104);
header_abonado_metodo.setText("MÉTODO");
header_abonado_metodo.setGravity(Gravity.CENTER);
header_abonado_metodo
.setWidth(getStandardPixels(ANCHO_COLUMNA_METODO_ABONO));
//header_abonado_metodo.setHeight(rowHeight);
header_abonado_metodo.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_abonado_dosis.setId(105);
header_abonado_dosis.setText("DÓSIS");
header_abonado_dosis.setGravity(Gravity.CENTER);
header_abonado_dosis
.setWidth(getStandardPixels(ANCHO_COLUMNA_DOSIS_ABONO));
//header_abonado_dosis.setHeight(rowHeight);
header_abonado_dosis.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_abonado_dosificacion.setId(106);
header_abonado_dosificacion.setText("DOSIFICACIÓN");
header_abonado_dosificacion.setGravity(Gravity.CENTER);
header_abonado_dosificacion
.setWidth(getStandardPixels(ANCHO_COLUMNA_DOSIFICACION_ABONO));
//header_abonado_dosificacion.setHeight(rowHeight);
header_abonado_dosificacion.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_abonado_deposito_A.setId(107);
header_abonado_deposito_A.setText("DEP. A");
header_abonado_deposito_A.setGravity(Gravity.CENTER);
header_abonado_deposito_A
.setWidth(getStandardPixels(ANCHO_COLUMNA_DEPOSITO_ABONO));
//header_abonado_deposito_A.setHeight(rowHeight);
header_abonado_deposito_A.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_abonado_deposito_B.setId(108);
header_abonado_deposito_B.setText("DEP. B");
header_abonado_deposito_B.setGravity(Gravity.CENTER);
header_abonado_deposito_B
.setWidth(getStandardPixels(ANCHO_COLUMNA_DEPOSITO_ABONO));
//header_abonado_deposito_B.setHeight(rowHeight);
header_abonado_deposito_B.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_abonado_deposito_C.setId(109);
header_abonado_deposito_C.setText("DEP. C");
header_abonado_deposito_C.setGravity(Gravity.CENTER);
header_abonado_deposito_C
.setWidth(getStandardPixels(ANCHO_COLUMNA_DEPOSITO_ABONO));
//header_abonado_deposito_C.setHeight(rowHeight);
header_abonado_deposito_C.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_abonado_deposito_D.setId(110);
header_abonado_deposito_D.setText("DEP. D");
header_abonado_deposito_D.setGravity(Gravity.CENTER);
header_abonado_deposito_D
.setWidth(getStandardPixels(ANCHO_COLUMNA_DEPOSITO_ABONO));
//header_abonado_deposito_D.setHeight(rowHeight);
header_abonado_deposito_D.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
header_botonera.setId(111);
header_botonera.setText("");
header_botonera.setGravity(Gravity.CENTER);
//header_botonera
// .setWidth(getStandardPixels(ANCHO_COLUMNA_BOTONERA_ABONO));
//header_botonera.setHeight(rowHeight);
header_botonera.setTextAppearance(this,
android.R.style.TextAppearance_Medium);
cabeceraTablaAbonados.addView(header_abonado_cultivo);
cabeceraTablaAbonados.addView(header_abonado_abono);
cabeceraTablaAbonados.addView(header_abonado_fecha);
cabeceraTablaAbonados.addView(header_abonado_metodo);
cabeceraTablaAbonados.addView(header_abonado_dosis);
cabeceraTablaAbonados.addView(header_abonado_dosificacion);
cabeceraTablaAbonados.addView(header_abonado_deposito_A);
cabeceraTablaAbonados.addView(header_abonado_deposito_B);
cabeceraTablaAbonados.addView(header_abonado_deposito_C);
cabeceraTablaAbonados.addView(header_abonado_deposito_D);
cabeceraTablaAbonados.addView(header_botonera);
//abonadoLayout
layoutTablaAbonados.addView(cabeceraTablaAbonados);
// TableRow.LayoutParams buttonParams = (TableRow.LayoutParams)header_abonado_cultivo.getLayoutParams();
// buttonParams.setMargins(15, 10, 10, 10);
}
Nevertheless, I am not able to make:
a) The header having a specific HEIGHT. I have tried replacing:
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
with
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
100); // For 100 pixels height
But the row remains the "standard" height that just wraps content.
Also, I have a similar issue with data rows (non-header) that are composed of a series of Buttons.
These buttons text will change dinamically and will stretch vertically to adapt to such string.
Nevertheless, the neighbour buttons won't stretch accordingly and so the aspect is very ugly with some buttons being taller than other ones (I won't paste more code on this as it is very similar to the other problem).
Here is an equivalent XML that does it correctly (grows all buttons accordingly to the taller one on the row):
(FIRST TABLE ROW IS HEADER AND NEXT ARE DATA)
<ScrollView
android:id="#+id/tab6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="top" >
<TableRow
android:layout_marginBottom="10dp"
android:background="#color/rojo_dupont" >
<TextView
android:id="#+id/cultivo_auxiliares"
android:layout_width="400dp"
android:layout_height="40dp"
android:layout_marginLeft="40dp"
android:gravity="center"
android:text="#string/cultivo_parcela_auxiliar"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/nombre_plaga_label"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:gravity="center"
android:text="#string/nombre_plaga_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/nombre_insecto_label"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:gravity="center"
android:text="#string/nombre_insecto_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/fecha_suelta_label"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginLeft="40dp"
android:gravity="center"
android:text="#string/fecha_suelta_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/cantidad_insecto_label"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:gravity="center"
android:text="#string/cantidad_insecto_label"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:layout_marginBottom="5dp" >
<Button
android:id="#+id/cultivo_parcela_0"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#8ad5f0" />
<Button
android:id="#+id/nombre_plaga_0"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#8ad5f0" />
<Button
android:id="#+id/nombre_insecto_0"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:focusableInTouchMode="false"
android:gravity="center"
android:hint="" />
<Button
android:id="#+id/fecha_suelta_0"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#8ad5f0" />
<EditText
android:id="#+id/cantidad_insecto_0"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_marginLeft="40dp"
android:focusableInTouchMode="false"
android:gravity="center"
android:hint=""
android:inputType="number" />
<ImageButton
android:id="#+id/insecto_0_delete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:contentDescription="#string/delete_content_description"
android:src="#drawable/discard_dark" />
<ImageButton
android:id="#+id/insecto_0_comentario"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:contentDescription="#string/delete_content_description"
android:src="#drawable/ic_dialog_alert_holo_dark" />
</TableRow>
MORE ROWS FOLLOW WITH THE SAME SCHEMA
...
I suppose I'm doing something wrong with layouts but I cannot figure it out even after reading a lot about it.
Could someone guide me with this or provide some working code?
Thanks in advance,
Jose.
Well, it seems I found the solution !!. The main problem was that I was selecting the wrong Layout for the Views inside the row.
Conceptually, the row must have a height that is WRAP_CONTENT so it will grow dynamically with the highest View it contains. Also, each View in the row must have its hight put to MATCH_PARENT, so they will grow as the row does. This is not very intuitive for me but it is it.
So here is a portion of the code that solved the problem for me, hope this can help someone:
TableRowAbonados nuevaFila = new TableRowAbonados(this);
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin = 0;
int topMargin = 0;
int rightMargin = 0;
int bottomMargin = getStandardPixels(5);
tableRowParams.setMargins(leftMargin, topMargin, rightMargin,
bottomMargin);
nuevaFila.setLayoutParams(tableRowParams);
Button botonAbono = new Button(this);
Button botonFecha = new Button(this);
Button botonMetodo = new Button(this);
EditText celdaDosis = new EditText(this);
celdaDosis.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
nuevaFila.addView(botonAbono);
nuevaFila.addView(botonFecha);
nuevaFila.addView(botonMetodo);
nuevaFila.addView(celdaDosis);
botonAbono.getLayoutParams().height=TableRow.LayoutParams.MATCH_PARENT;
botonFecha.getLayoutParams().height=TableRow.LayoutParams.MATCH_PARENT;
botonMetodo.getLayoutParams().height=TableRow.LayoutParams.MATCH_PARENT;
celdaDosis.getLayoutParams().height=TableRow.LayoutParams.MATCH_PARENT;
layoutTablaAbonados.addView(nuevaFila);

Different view edittext in Android

When I create EditText programmatically I have no underline (in the second EditText). Like this: http://oi41.tinypic.com/2ut427d.jpg
When I create an EditText in XML everything is fine, see http://oi44.tinypic.com/nmxdnm.jpg. Where is the error?
This is my XML layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<include layout="#layout/input_title_layout" />
<TableLayout
android:id="#+id/relativeLayout12"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp" >
<TextView
style="#style/tvTitleStyle"
android:text="#string/diameter" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text=":" />
<EditText
android:id="#+id/diameterValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:digits="0123456780."
android:inputType="numberDecimal" />
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
And code to add EditText programatically:
final TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT, 1f);
final TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
rowParams.bottomMargin = 0;
TableLayout tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(tableParams);
inputLinearLayout.addView(tableLayout);
TableRow tableRowEdit = new TableRow(context);
TextView titleTv = new TextView(context);
titleTv.setTextAppearance(context, R.style.tvTitleStyle);
titleTv.setText(context.getString(context.getResources().getIdentifier(inputTvs[i] , "string", context.getPackageName())));
TextView separatorTv = new TextView(context);
separatorTv.setGravity(Gravity.LEFT);
separatorTv.setPadding(5, 0, 5, 0);
separatorTv.setText(":");
separatorTv.setTextColor(Color.BLACK);
EditText editText = new EditText(context);
editText.setId(i);
tableRowEdit.addView(titleTv);
tableRowEdit.addView(separatorTv);
tableRowEdit.addView(editText);
tableLayout.addView(tableRowEdit);
It's hard to tell exactly what the issue might be given the code you supplied.
-Have you tried setting the layout parameters of the TableRow/TextView/EditText? The TextViews might be taking up all the visible space.
-Are you sure inputLinearLayout has been inflated?

Programmatic Layout with SetPadding and SetGravity

I am trying to duplicate the following layout programmatically; however, the setGravity and setPadding don't seem to have an effect:
I'm trying to recreate the textviews within the TableRows (note that there's padding and the second text view is right aligned). Also, I'm trying to recreate additional entries because they are dynamic:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myinfo_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:scrollbars="vertical"
>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainTable"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_column="1"
android:text="Text1"
android:padding="3dip" />
<TextView
android:id="#+id/text1"
android:text="TBD"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:layout_column="1"
android:text="Text2"
android:padding="3dip" />
<TextView
android:id="#+id/text2"
android:text="TBD"
android:gravity="right"
android:padding="3dip" />
</TableRow>
. . .
Here's a code snippet to add the TextViews. It seems to work but the entries don't have padding and are not right aligned.
int n = 0;
TableLayout table = (TableLayout) findViewById(R.id.mainTable);
for (int j=0; j<2; j++) {
Log.v(LOG_TAG, "entering for loop");
n++;
TableRow tr = new TableRow(getApplicationContext());
tr.setId(100+n);
String key = "hello";
float value = (float) 1.388);
value = value/3600;
TextView tvDynamicKey = new TextView(getApplicationContext());
TextView tvDynamicUnit = new TextView(getApplicationContext());
tvDynamicUnit.setText(Float.toString(value));
tvDynamicUnit.setGravity(Gravity.RIGHT);
tvDynamicKey.setText(key + " " + n);
tvDynamicKey.setPadding(3, 3, 3, 3);
tvDynamicUnit.setPadding(3, 3, 3, 3);
tr.addView(tvDynamicKey);
tr.addView(tvDynamicUnit);
table.addView(tr);
}
}
In your layout you always specify column1 for the left element (android:layout_column="1"). But you don't do it programmatically for the dynamic creation. You can do it like this. I tested it, it works on my environment.
tvDynamicKey.setLayoutParams(new TableRow.LayoutParams(1));

TableRow not displaying all TextViews

I am creating a TableRow at runtime. The trouble I have is that I have 6 TextViews created and added to the TableRow but for some reason only 3 of the TextViews appear, the 1st 2 and the last one. I know that there is no problem adding the TextView to the TableRow because if I comment out the last 2 TextViews the fourth one still overwrites the third. Because of this it seems to be that there is a limit somewhere on the number of columns I can add to this TableRow.
Any ideas where I am going wrong?
Here is my java
public class locationlist extends Activity {
/** Called when the activity is first created. */
private locationListDbAdapter mDbHelper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locationlist);
mDbHelper = new locationListDbAdapter(this);
mDbHelper.open();
String id = mDbHelper.fetchRow(1).getString(0);
String locationText = mDbHelper.fetchRow(1).getString(1);
String localCode = mDbHelper.fetchRow(1).getString(2);
String localService = mDbHelper.fetchRow(1).getString(3);
String localComments = mDbHelper.fetchRow(1).getString(4);
String localNextes = mDbHelper.fetchRow(1).getString(5);
TextView idView = new TextView(this);
idView.setText(id);
TextView locationView = new TextView(this);
locationView.setText(locationText);
TextView codeView = new TextView(this);
codeView.setText(localCode);
TextView serviceView = new TextView(this);
codeView.setText(localService);
TextView commentsView = new TextView(this);
codeView.setText(localComments);
TextView nextesView = new TextView(this);
codeView.setText(localNextes);
TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
TableRow r2 = new TableRow(this);
r2.addView(idView);
r2.addView(locationView);
r2.addView(codeView);
r2.addView(serviceView);
r2.addView(commentsView);
r2.addView(nextesView);
tl.addView(r2);
this.setContentView(tl);
This is the xml file I am pointing it at
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="#+id/tableLayout1" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content">
<TableRow android:layout_height="wrap_content" android:id="#+id/tableRow1" android:layout_width="fill_parent">
<TextView android:id="#+id/textView11" android:layout_height="wrap_content" android:text="ID" android:layout_width="0dip" android:layout_weight="0.1"></TextView>
<TextView android:id="#+id/textView1" android:layout_height="wrap_content" android:text="Location" android:layout_width="0dip" android:layout_weight="0.4"></TextView>
<TextView android:id="#+id/textView2" android:layout_height="wrap_content" android:text="Type" android:layout_width="0dip" android:layout_weight="0.15"></TextView>
<TextView android:id="#+id/textView3" android:layout_height="wrap_content" android:text="Service" android:layout_width="0dip" android:layout_weight="0.2"></TextView>
<TextView android:id="#+id/textView4" android:layout_height="wrap_content" android:text="Comments" android:layout_weight="0.3" android:layout_width="0dip"></TextView>
<TextView android:id="#+id/textView5" android:layout_height="wrap_content" android:text="ES" android:layout_width="0dip" android:layout_weight="0.15"></TextView>
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
</TableLayout>
Why do you have the following lines? You set your codeView TextView to localCode's text, then you overwrite that three times for no discernible reason:
codeView.setText(localCode);
codeView.setText(localService);
codeView.setText(localComments);
codeView.setText(localNextes);
Your serviceView, commentsView and nextesView TextView's all appear to be blank, you never do anything like this:
serviceView.setText(localService);
commentsView.setText(localComments);
nextesView.setText(localNextes);
I would temporarily replace your database stuff with test strings so as to narrow down the error, see if it is still failing, if not, work on the things mentioned above, test again, if it is working now, replace the temporary text strings with the database stuff.

Categories

Resources