0
  1. I have a TableLayout and I need to add TableRows at runtime.
  2. I need to add an ImageView and a TextView to each TableRow and add the row to TableLayout.
  3. And the TextView added to each row should be displayed in multiple lines if the length of the text is much long.
  4. I tried setSingleLine(false), setMaxWidth(100), setMaxLines(3), nothing is working but If I add the TableRow in xml with android:singleLine="false" it shows the text in multiple lines.

Please suggest me.......

SOLVED THROUGH THIS CODE

TableRow.LayoutParams tlparams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
textView.setLayoutParams(tlparams);
textView.setText("New text: " + s);
textView.setSingleLine(false);

3 Answers 3

2

The below code will work for you.

TableRow.LayoutParams tlparams = new TableRow.LayoutParams( 
TableRow.LayoutParams.WRAP_CONTENT, 
TableRow.LayoutParams.WRAP_CONTENT); 
TextView textView = new TextView(this); 
textView.setLayoutParams(tlparams); 
textView.setText("New text: ");
textView.setMaxLines(3);
1

This is for creating Dynamic Table

TableLayout TL=(TableLayout) findViewById(R.id.table);

TableRow tr=new TableRow(this);
tr.setLayoutParams(new LayoutParams( 
   LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

TextView text_view=new TextView(this);
text_view.setWidth(100);// give how much you need
//set your text
tr.addView(text_view);

ImageView iv=new ImageView(this);
// set your image
tr.addView(iv);    

TL.addView(tr);

This is for new line in TextView.

String str="This is first line in textview1\nThis is\tsecond line in textview1";
text_view.setText(str);

or

String str1 ="<font color=#00cc00>First line is green color in textview2</font><br/>"+
              " <font color=#ff0000>Second line is red color in textview2</font>";
text_view.setText(Html.fromHtml(str1));
0

have a separate xml file containing a TableLayout and a separate xml file containing TAble row which contains an ImageView and a TextView

in your code at run time using for loop add table row into the tableLayout as follows

let table layout be as tl = (tableLayout)findViebyId(..);
for(int i =0;.....)
{
   final View child = getLayoutInflater().inflate(R.layout.inflateview, null);
    tl.addView(child);
}

here child view is layout of table row xml file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.