kankamuso
kankamuso

Reputation: 441

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.

Upvotes: 0

Views: 222

Answers (1)

kankamuso
kankamuso

Reputation: 441

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);

Upvotes: 1

Related Questions