androidviewadditiontablelayouttablerow

Dynamically add TableRow to above of TableLayout


I want to dynamically add TableRow to above of TableLayout. I use blow code:

In my activity:

TableLayout table = (TableLayout)ChatActivity.this.findViewById(R.id.tblChats);
        for(int i = 1; i < 10; ++i)
        {
            TableRow row = (TableRow)LayoutInflater.from(ChatActivity.this).inflate(R.layout.chat_row, null);
            ((TextView)row.findViewById(R.id.attrib_name)).setText("A");
            ((TextView)row.findViewById(R.id.attrib_value)).setText(i + "");
            table.addView(row);
        }
        table.requestLayout();

chat_row.xml:

<?xml version="1.0" encoding="utf-8"?>

<TableRow xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView android:id="@+id/attrib_name"
        android:textStyle="bold"/>

    <TextView android:id="@+id/attrib_value"
        android:gravity="right"
        android:textStyle="normal"/>

</TableRow>

As you know these codes adds TableRow in the bottom of the TableLayout.

Is there any way to add it to the above of TableLayout?


Solution

  • According to Android Team's answer I realized I must change this line:

    table.addView(row);
    

    to this:

    table.addView(row, 0);