0

I'm trying to build a dynamic table in my Android application. I would like to have a 70% wide column and a 30% wide column.

If I do it with the layout xml using android:layout_weight I don't have any problem:

    <TableLayout
    android:id="@+id/TableLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="*"
    android:background="#ffffff" >

    <TableRow
        android:id="@+id/TableRow4"
        style="@style/RigaTabella"
        android:background="#d2ef7b" >

        <TextView
            android:id="@+id/TitoloSpesaTabella"
            style="@style/Titolo"
            android:layout_width="0px"
            android:layout_weight="0.7"
            android:background="#cdcdcd"
            android:text="@string/spesa" />

        <TextView
            android:id="@+id/TitoloCostoTabella"
            style="@style/Titolo"
            android:layout_width="0px"
            android:layout_weight="0.3"
            android:background="#ababab"
            android:text="@string/costo" />
    </TableRow>
</TableLayout>

This is what I obtain: https://i.stack.imgur.com/FLD91.jpg

(I used this gray background to show the real dimension of the two textView)

However if I try with this code:

TableLayout TableLayout = (TableLayout) findViewById(R.id.TableLayout);
    for (int i = 0; i < 10; i++) {
        TableRow Row = (TableRow) getLayoutInflater().inflate(R.layout.table_row_style, null);
        if (i % 2 == 0)
            Row.setBackgroundColor(Color.parseColor("#ffffff"));
        else
            Row.setBackgroundColor(Color.parseColor("#f1f1f1"));

        TextView tv = (TextView) getLayoutInflater().inflate(R.layout.testo_sx_style, null);
        tv.setText("Test Test");

        TextView tv2 = (TextView) getLayoutInflater().inflate(R.layout.testo_dx_style, null);
        tv2.setText("$ 1000");

        Row.addView(tv);
        Row.addView(tv2);

        TableLayout.addView(Row);
    }

where testo_sx_style.xml is:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:layout_weight="0.7"
    android:gravity="left"
    android:textSize="13sp"
    android:textColor="#161616"
    android:typeface="monospace"
    android:background="#cdcdcd"
/>

and where testo_dx_style.xml is:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:layout_weight="0.3"
    android:gravity="left"
    android:textSize="13sp"
    android:textColor="#161616"
    android:typeface="monospace"
    android:background="#ababab"
/>

I obtain this: https://i.stack.imgur.com/MIvwH.png

I really don't know what it's wrong. I also tried, for example, to set in testo_sx_style.xml the layout_width to 100px but it doesn't change anything. It seems that some properties aren't applied to my code.

Anyone know what is the error? Thanks in advance.

Have a good day :)

p.s. Sorry for my English

2 Answers2

1

Solved! With just this line of code:

tv.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.7f));

With this I can set the width to 0px, the height to WRAP_CONTENT and the weight to 0.7.

It works great :)

p.s. I found the solution here: Set the layout weight of a TextView programmatically

Community
  • 1
  • 1
  • Thank you, it worked for me too - I wanted to add a text(Left to Right) and image on the right. the image distance from the text was related to the text length. your line solved the issue – Hesham Yassin Feb 06 '17 at 19:56
0

You could possibly try something like this, using the weight parameter of the layout:

TableRow tr1 = new TableRow(this);
tabInfo = new TableLayout(this);
tabInfo.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 4.0f));
tr1.addView(tabInfo);
Gravitoid
  • 1,294
  • 1
  • 20
  • 20