m n m n - 21 days ago 5
Android Question

custom TextView not wrapped in table layout

I have created a custom textview and want to add it dynamically to a table layout:

custome textview code named custom_table_cell :

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/style_table_cell"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="Cell" >

</TextView>


My activity code :

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_flight" >

<ScrollView android:layout_height="fill_parent"
android:layout_width="fill_parent" >

<TableLayout
android:id="@+id/tblInvoiceData"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TableLayout>
</ScrollView>

</TableLayout>


and my code to add table row to table layout :

public TextView TableGetCell(Context context, String Text, int ColumnId) {
final TextView cell = (TextView) ((Activity) context).getLayoutInflater().inflate(R.layout.custom_table_cell,
null);
cell.setText(Text);

cell.setTypeface(Graphic.GetFontFaceFarsi(context));

cell.setLayoutParams(new TableRow.LayoutParams(ColumnId));

return cell;
}

public void CreateRows() {
for (final Customer d : Customers) {
TableRow tr = new TableRow(this);

tr.addView(TableGetCell(this, String.valueOf(d.Name), 0));
tr.addView(TableGetCell(this, String.valueOf(d.Family), 0));
tr.addView(TableGetCell(this, String.valueOf(d.Age), 0));

tblInvoiceData.addView(tr);
}

}


and my question : why text view not wrapped?

Answer
TableRow row = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);

TextView textView = new TextView(this);
textView.setLayoutParams(new TableRow.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT, 1.0f));
textView.setText("yourValue");
row.addView(textView);
tablelayout.addView(row);