Dynamic Content with wrap text not working

Viewed 198

I want to build a dynamic table which will have TextView with some content. It could happen that the text is long, in that case, I want my text to be wrapped. But in the code given below it does not work. I tried adding layout parameters to my TextView with weight=1 does not work.

Here is my code:

table = FindViewById<TableLayout>(Resource.Id.main_table);

TableRow tr_head = new TableRow(this);
tr_head.Id = 10;
tr_head.SetBackgroundColor(Color.Gray);
TableRow.LayoutParams paramTableRow = new TableRow.LayoutParams(LayoutParams.MatchParent,
LayoutParams.WrapContent);
 tr_head.LayoutParameters = paramTableRow;

LinearLayout.LayoutParams paramcell = new LinearLayout.LayoutParams(LayoutParams.WrapContent,LayoutParams.WrapContent,1.0f);


TextView label_Buyway = new TextView(this);
label_Buyway.Id=20;
label_Buyway.Text ="Buyway";
label_Buyway.SetTextColor(Color.Black);
label_Buyway.SetPadding(5, 5, 5, 5);
 tr_head.AddView(label_Buyway);

TextView label_qty = new TextView(this);
label_qty.Id=21;
label_qty.Text="Qty";
label_qty.SetTextColor(Color.Black); 
label_qty.SetPadding(5, 5, 5, 5); 
tr_head.AddView(label_qty); 

TextView label_price = new TextView(this);
label_price.Id = 22;
label_price.Text = "Price"; 
label_price.SetTextColor(Color.Black); 
label_price.SetPadding(5, 5, 5, 5);
tr_head.AddView(label_price);

TextView label_total = new TextView(this);
label_total.Id = 23;
label_total.Text = "Total"; 
label_total.SetTextColor(Color.Black); 
label_total.SetPadding(5, 5, 5, 5); 
tr_head.AddView(label_total);



table.AddView(tr_head, new TableLayout.LayoutParams(
     LayoutParams.MatchParent,
     LayoutParams.WrapContent));


for (int i = 0; i < 10; i++)

{
    string date = DateTime.Now.ToString();
    Double weight_kg = 1.1;
    TableRow tr = new TableRow(this);
    tr.Id=(100 + i);
    tr.LayoutParameters = paramTableRow;

    TextView labelBuyway = new TextView(this);
    labelBuyway.Id=(200 + i);
    labelBuyway.Text =date;
    labelBuyway.SetTextColor(Color.Black);
    labelBuyway.SetBackgroundResource(Resource.Drawable.table_row);
    labelBuyway.LayoutParameters = paramcell;
    tr.AddView(labelBuyway);

    TextView labelqty = new TextView(this);
    labelqty.Id=(200 + i);
    labelqty.Text=(weight_kg.ToString());

    labelqty.SetTextColor(Color.Black);
    labelqty.SetBackgroundResource(Resource.Drawable.table_row);
    tr.AddView(labelqty);

    TextView labelPrice = new TextView(this);
    labelPrice.Id = (200 + i);
    labelPrice.Text = (weight_kg.ToString());
    labelPrice.SetTextColor(Color.Black);
    labelPrice.SetBackgroundResource(Resource.Drawable.table_row);
    tr.AddView(labelPrice);

    TextView labelTotal = new TextView(this);
    labelTotal.Id = (200 + i);
    labelTotal.Text = (weight_kg.ToString());
    labelTotal.SetTextColor(Color.Black);
    labelTotal.SetBackgroundResource(Resource.Drawable.table_row);
    tr.AddView(labelTotal);


    table.AddView(tr, new TableLayout.LayoutParams(
                            LayoutParams.MatchParent,
                            LayoutParams.WrapContent));
    i++;
}

Right now the text gets cut and does not wrap. Any help would be appreciated.

2 Answers
Related